-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
98 lines (77 loc) · 2.28 KB
/
index.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
/**
* Created by karl on 16/07/15.
*/
/// <reference path='typings/tsd.d.ts' />
'use strict';
import fs = require('fs');
import path = require('path');
import http = require('http');
import bunyan = require('bunyan');
import cluster = require('cluster');
import express = require('express');
import compress = require('compression');
import eJSend = require('easy-jsend');
import eXHeaders = require('easy-x-headers');
import Common = require('./lib/common');
import Log = require('./lib/log');
import Error = require('./lib/error');
var clusterService = require('cluster-service');
interface Config {
root: string;
xHeaderDefaults: any;
port: number;
name: string;
forceRegister?: boolean;
cluster?: {
workers: string | Object;
workerCount: number;
accessKey: string;
port?:number;
};
log: {
name: string;
level: string
}
mount: (app: express.Application) => void
}
var e;
export function init(config: any) {
e = config.express || express;
eJSend.init(config.jSend);
}
export function create(config: Config) {
var app = express();
var log = Log.init(config.log);
if(config.cluster) {
config.cluster.port = config.cluster.port || config.port + 10000;
}
if(!config.cluster || !cluster.isMaster || config.forceRegister) {
Common.register(app, config.root, config.cluster);
app.use(compress());
app.set('trust proxy', true);
app.use(eXHeaders.getMiddleware(config.xHeaderDefaults));
app.use(log.middleware());
config.mount(app);
app.all('*', function (req, res, next) {
res.fail('Not found', 404);
});
var server = http.createServer(app);
app.use(Error.getErrorHandler(server));
}
return {
express: express,
app: app,
log: log.log,
server: server,
listen: function() {
if(config.cluster && cluster.isMaster) {
config.cluster.workers = process['mainModule'].filename;
clusterService.start(config.cluster);
return;
}
server.listen(config.port, function () {
log.log.info('%s listening on %d', config.name, config.port);
});
}
};
}