forked from appium/appium-base-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
112 lines (97 loc) · 3.83 KB
/
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
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
import path from 'path';
import express from 'express';
import http from 'http';
import favicon from 'serve-favicon';
import bodyParser from 'body-parser';
import methodOverride from 'method-override';
import log from './logger';
import { startLogFormatter, endLogFormatter } from './express-logging';
import { allowCrossDomain, fixPythonContentType, defaultToJSONContentType,
catchAllHandler, catch404Handler, catch4XXHandler } from './middleware';
import { guineaPig, guineaPigScrollable, guineaPigAppBanner, welcome, STATIC_DIR } from './static';
import { produceError, produceCrash } from './crash';
import { addWebSocketHandler, removeWebSocketHandler, removeAllWebSocketHandlers,
getWebSocketHandlers } from './websocket';
import B from 'bluebird';
async function server (configureRoutes, port, hostname = null, allowCors = true) {
// create the actual http server
let app = express();
let httpServer = http.createServer(app);
httpServer.addWebSocketHandler = addWebSocketHandler;
httpServer.removeWebSocketHandler = removeWebSocketHandler;
httpServer.removeAllWebSocketHandlers = removeAllWebSocketHandlers;
httpServer.getWebSocketHandlers = getWebSocketHandlers;
// http.Server.close() only stops new connections, but we need to wait until
// all connections are closed and the `close` event is emitted
let close = httpServer.close.bind(httpServer);
httpServer.close = async () => {
return await new B((resolve, reject) => {
httpServer.on('close', resolve);
close((err) => {
if (err) reject(err); // eslint-disable-line curly
});
});
};
return await new B((resolve, reject) => {
httpServer.on('error', (err) => {
if (err.code === 'EADDRNOTAVAIL') {
log.error('Could not start REST http interface listener. ' +
'Requested address is not available.');
} else {
log.error('Could not start REST http interface listener. The requested ' +
'port may already be in use. Please make sure there is no ' +
'other instance of this server running already.');
}
reject(err);
});
httpServer.on('connection', (socket) => {
socket.setTimeout(600 * 1000); // 10 minute timeout
socket.on('error', reject);
});
configureServer(app, configureRoutes, allowCors);
let serverArgs = [port];
if (hostname) {
// If the hostname is omitted, the server will accept
// connections on any IP address
serverArgs.push(hostname);
}
httpServer.listen(...serverArgs, (err) => {
if (err) {
reject(err);
}
resolve(httpServer);
});
});
}
function configureServer (app, configureRoutes, allowCors = true) {
app.use(endLogFormatter);
// set up static assets
app.use(favicon(path.resolve(STATIC_DIR, 'favicon.ico')));
app.use(express.static(STATIC_DIR));
// crash routes, for testing
app.use('/wd/hub/produce_error', produceError);
app.use('/wd/hub/crash', produceCrash);
// add middlewares
if (allowCors) {
app.use(allowCrossDomain);
}
app.use(fixPythonContentType);
app.use(defaultToJSONContentType);
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride());
app.use(catch4XXHandler);
app.use(catchAllHandler);
// make sure appium never fails because of a file size upload limit
app.use(bodyParser.json({limit: '1gb'}));
// set up start logging (which depends on bodyParser doing its thing)
app.use(startLogFormatter);
configureRoutes(app);
// dynamic routes for testing, etc.
app.all('/welcome', welcome);
app.all('/test/guinea-pig', guineaPig);
app.all('/test/guinea-pig-scrollable', guineaPigScrollable);
app.all('/test/guinea-pig-app-banner', guineaPigAppBanner);
// catch this last, so anything that falls through is 404ed
app.use(catch404Handler);
}
export { server, configureServer };