-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
114 lines (96 loc) · 3.21 KB
/
app.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import express = require('express');
import routes = require('./routes/index');
import user = require('./routes/user');
import morgan = require('morgan');
import favicon = require('serve-favicon');
import serveStatic = require('serve-static');
let bodyParser = require('body-parser');
import http = require('http');
import Q = require('q');
import path = require('path');
import I = require('./Interfaces');
import PlayerSessionFactory = require('./PlayerSessionFactory');
import LoadTestOrchestrator = require('./LoadTestOrchestrator');
import PlayerRunner = require('./PlayerRunner');
let logger = require('./logger');
let settings: I.Settings = require('./config/settings');
process.env.UV_THREADPOOL_SIZE = 128;
let app = express();
app.use(serveStatic(path.join(__dirname, 'public')));
app.use(favicon(__dirname + '/public/images/favicon.ico'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
let playerSessionFactory = new PlayerSessionFactory();
let playerRunner = new PlayerRunner(playerSessionFactory);
let loadTestOrchestrator = new LoadTestOrchestrator(playerRunner, playerSessionFactory);
function successResponseHandler(responsePayload: any, res: express.Response, req: any) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.json(responsePayload);
res.status(200);
}
function failResponseHandler(err: any, res: express.Response, req: any) {
let errorMessage;
if (err && err.message) {
errorMessage = err.message;
}
logger.error("Error Message: " + errorMessage, err);
res.json({ error: errorMessage });
res.status(500);
}
app.get('/admin', (req, res) => {
res.render('index', { data: {} });
});
app.post('/admin/start', (req, res) => {
Q.fcall(() => {
return loadTestOrchestrator.start(req.body);
}).then((responsePayload: any) => {
successResponseHandler(responsePayload, res, req);
}).catch((err) => {
failResponseHandler(err, res, req);
});
});
app.post('/admin/stop', (req, res) => {
Q.fcall(() => {
return loadTestOrchestrator.stop(req.body);
}).then((responsePayload: any) => {
successResponseHandler(responsePayload, res, req);
}).catch((err) => {
failResponseHandler(err, res, req);
});
});
app.post('/run', (req, res) => {
Q.fcall(() => {
return loadTestOrchestrator.run(req.body);
}).then((responsePayload: any) => {
successResponseHandler(responsePayload, res, req);
}).catch((err) => {
failResponseHandler(err, res, req);
});
});
app.post('/stopRun', (req, res) => {
Q.fcall(() => {
return loadTestOrchestrator.stopRun(req.body);
}).then((responsePayload: any) => {
successResponseHandler(responsePayload, res, req);
}).catch((err) => {
failResponseHandler(err, res, req);
});
});
process.on('uncaughtException', function (err: any) {
logger.error(err.stack);
logger.info("Node NOT Exiting...");
debugger;
});
app.all('*', (req: any, res: any) => {
res.status(404);
res.write("404");
res.end();
});
app.listen(settings.httpPort);
logger.info("LoadTest has started");
let printableSettings: any = settings;
logger.info(JSON.stringify(printableSettings.__proto__, null, 2));