-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsjs-server.js
62 lines (52 loc) · 1.67 KB
/
sjs-server.js
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
var express = require('express');
var _ = require('underscore')
var ctimeout = require('connect-timeout')
var jobHandler = require('./job-handler')
var restart_jobserver = require('./utils/sjs-restart')
var Server = function(url="http://localhost:8090",
classPathPrefix,
appName,
context,
endPoints,
sjsHome){
this.handler = new jobHandler(url, classPathPrefix, appName, context, sjsHome)
this.endPoints = endPoints
}
Server.prototype.start = function(PORT=3000, timeout='120s'){
var expApp = express();
var middleware = {
logger: function(req, res, next) {
console.log('Request: ' + new Date().toString() + ' ' +
req.method + ' ' + req.originalUrl);
next();
}
};
handler = this.handler
expApp.use(middleware.logger);
expApp.use(ctimeout(timeout))
expApp.get('/:app', function(req, res){
app = req.params.app
if(_.contains(this.endPoints, app)) {
job = req.query;
if(req.query.forced=="true"){
delete job.forced
handler.add(app, job, res, "true")
} else{
result = handler.get(app, job)
if(result){
console.log("INFO: Output the result of " + app + ": " + JSON.stringify(job))
res.send(result)
} else {
handler.add(app, job, res)
}
}
} else {
console.log("INFO: endpoint not found: (" + req.params.app +
"), available endpoints are " + this.endPoints)
}
})
expApp.listen(PORT, function() {
console.log('Express server started on port ' + PORT + '!');
});
}
module.exports = Server