forked from mane115/ucenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (54 loc) · 1.7 KB
/
app.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
var Koa = require('koa'),
redisStore = require('koa-redis'),
log4js = require('log4js'),
session = require('koa-session-minimal'),
static = require('koa-static'),
bodyParser = require('koa-bodyparser'),
config = require('./config'),
util = require('./util'),
app = new Koa(),
model = {
mongo: require('./model/mongo')
};
var initLogger = function(log4js) {
log4js.configure(config.log);
var logger = log4js.getLogger('common');
global.logger = logger;
};
var initMidware = function(app) {
var router = require('./router');
var redisStoreOption = {
host: config.database.redis.host,
port: config.database.redis.port
};
if (config.database.redis.pwd) {
redisStoreOption.auth_pass = config.database.redis.pwd
}
if (config.database.redis.library || config.database.redis.library === 0) {
redisStoreOption.db = config.database.redis.library
}
var sessionOption = {
store: redisStore(redisStoreOption),
key: config.session.key,
cookie: {
maxAge: config.session.age
}
};
app.use(bodyParser()).use(session(sessionOption)).use(router.routes()).use(router.allowedMethods());
console.log('init midware success');
return Promise.resolve();
};
var initServer = async function(app) {
try {
await initLogger(log4js);
await model.mongo.init();
await util.initRedis();
await initMidware(app);
app.listen(config.port);
app.on('error', err => logger.error(`server error ${err}`, err));
logger.info(`init server success port:${config.port}`);
} catch (err) {
logger.error(err)
}
};
initServer(app)