forked from realm/realm-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathros-testing-server.js
More file actions
executable file
·90 lines (76 loc) · 2.54 KB
/
ros-testing-server.js
File metadata and controls
executable file
·90 lines (76 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env nodejs
var winston = require('winston'); //logging
const temp = require('temp');
const spawn = require('child_process').spawn;
var http = require('http');
var dispatcher = require('httpdispatcher');
// Automatically track and cleanup files at exit
temp.track();
if (process. argv. length <= 2) {
console.log("Usage: " + __filename + " somefile.log");
process.exit(-1);
}
const logFile = process.argv[2];
winston.level = 'debug';
winston.add(winston.transports.File, { filename: logFile });
const PORT = 8888;
function handleRequest(request, response) {
try {
//log the request on console
winston.log(request.url);
//Dispatch
dispatcher.dispatch(request, response);
} catch(err) {
console.log(err);
}
}
var syncServerChildProcess = null;
function startRealmObjectServer() {
stopRealmObjectServer();
temp.mkdir('ros', function(err, path) {
if (!err) {
winston.info("Starting sync server in ", path);
var env = Object.create( process.env );
winston.info(env.NODE_ENV);
env.NODE_ENV = 'development';
syncServerChildProcess = spawn('realm-object-server',
['--root', path,
'--configuration', '/configuration.yml'],
{ env: env });
// local config:
syncServerChildProcess.stdout.on('data', (data) => {
winston.info(`stdout: ${data}`);
});
syncServerChildProcess.stderr.on('data', (data) => {
winston.info(`stderr: ${data}`);
});
syncServerChildProcess.on('close', (code) => {
winston.info(`child process exited with code ${code}`);
});
}
});
}
function stopRealmObjectServer() {
if (syncServerChildProcess) {
syncServerChildProcess.kill();
syncServerChildProcess = null;
}
}
// start sync server
dispatcher.onGet("/start", function(req, res) {
startRealmObjectServer();
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Starting a server');
});
// stop a previously started sync server
dispatcher.onGet("/stop", function(req, res) {
stopRealmObjectServer();
winston.info("Sync server stopped");
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Stopping the server');
});
//Create and start the Http server
var server = http.createServer(handleRequest);
server.listen(PORT, function() {
winston.info("Integration test server listening on: 127.0.0.1:%s", PORT);
});