summaryrefslogtreecommitdiff
path: root/src/server.ts
blob: 421ad60c1f7b1ff1ff017be4d2ccd15b66b79124 (plain)
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
/**
 * Core Server
 */

import * as fs from 'fs';
import * as http from 'http';
import * as https from 'https';
import * as express from 'express';
import vhost = require('vhost');

/**
 * Init app
 */
const app = express();
app.disable('x-powered-by');

// Reject request that without 'Host' header
app.use((req, res, next) => {
	if (!req.headers.host) {
		res.sendStatus(400);
	} else {
		next();
	}
});

/**
 * Register modules
 */
app.use(vhost(`api.${config.host}`, require('./api/server')));
app.use(vhost(config.secondary_host, require('./himasaku/server')));
app.use(vhost(`file.${config.secondary_host}`, require('./file/server')));
app.use(vhost(`proxy.${config.secondary_host}`, require('./web/service/proxy/server')));
app.use(require('./web/server'));

/**
 * Create server
 */
const server = config.https.enable ?
	https.createServer({
		key:  fs.readFileSync(config.https.key),
		cert: fs.readFileSync(config.https.cert),
		ca:   fs.readFileSync(config.https.ca)
	}, app) :
	http.createServer(app);

/**
 * Steaming
 */
require('./api/streaming')(server);

/**
 * Server listen
 */
server.listen(config.port, () => {
	// Send a 'ready' message to parent process
	process.send('ready');
});