-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
51 lines (39 loc) · 1.45 KB
/
index.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
const co = require('co');
const http = require('http');
const express = require('express');
const config = require('./config');
const logger = require('./common/logger');
let httpServer;
const setupEndpoint = (app, endpoint) => {
app[endpoint.method](endpoint.url, ...endpoint.handlers);
};
const cb = (resolve, reject) =>
(err, res) => void (err ? reject(err) : resolve(res));
const start = () => co(function* () {
const app = express();
setupEndpoint(app, require('./endpoints/get-project'));
setupEndpoint(app, require('./endpoints/get-metadata'));
setupEndpoint(app, require('./endpoints/create-project'));
setupEndpoint(app, require('./endpoints/put-project'));
setupEndpoint(app, require('./endpoints/graphql-proxy'));
setupEndpoint(app, require('./endpoints/codegen'));
if (config.get('serveStatic')) {
setupEndpoint(app, require('./endpoints/serve-app'));
setupEndpoint(app, require('./endpoints/serve-preview'));
setupEndpoint(app, require('./endpoints/serve-bundle'));
setupEndpoint(app, require('./endpoints/serve-assets'));
}
httpServer = http.createServer(app);
const port = config.get('port');
yield new Promise((resolve, reject) =>
void httpServer.listen(port, cb(resolve, reject)));
logger.info(`Server is listening on port ${port}. App is ready on localhost:${port}/app/:projectName.`);
});
co(function* () {
try {
yield start();
} catch (err) {
logger.critical(err);
process.exit(1);
}
});