blob: f1fcf58c8d281e9b6ca888a6fcb4204588b64b59 (
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
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
|
/**
* Core Server
*/
import * as fs from 'fs';
import * as http from 'http';
import * as http2 from 'http2';
import * as zlib from 'zlib';
import * as Koa from 'koa';
import * as Router from 'koa-router';
import * as mount from 'koa-mount';
import * as compress from 'koa-compress';
import * as logger from 'koa-logger';
//const slow = require('koa-slow');
import activityPub from './activitypub';
import webFinger from './webfinger';
import config from '../config';
// Init app
const app = new Koa();
app.proxy = true;
if (process.env.NODE_ENV != 'production') {
// Logger
app.use(logger());
// Delay
//app.use(slow({
// delay: 1000
//}));
}
// Compress response
app.use(compress({
flush: zlib.constants.Z_SYNC_FLUSH
}));
// HSTS
// 6months (15552000sec)
if (config.url.startsWith('https')) {
app.use(async (ctx, next) => {
ctx.set('strict-transport-security', 'max-age=15552000; preload');
await next();
});
}
app.use(mount('/api', require('./api')));
app.use(mount('/files', require('./file')));
// Init router
const router = new Router();
// Routing
router.use(activityPub.routes());
router.use(webFinger.routes());
// Register router
app.use(router.routes());
app.use(mount(require('./web')));
function createServer() {
if (config.https) {
const certs: any = {};
Object.keys(config.https).forEach(k => {
certs[k] = fs.readFileSync(config.https[k]);
});
certs['allowHTTP1'] = true;
return http2.createSecureServer(certs, app.callback());
} else {
return http.createServer(app.callback());
}
}
export default () => new Promise(resolve => {
const server = createServer();
// Init stream server
require('./api/streaming')(server);
// Listen
server.listen(config.port, resolve);
});
|