-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (75 loc) · 2.76 KB
/
index.js
File metadata and controls
85 lines (75 loc) · 2.76 KB
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
const Koa = require('koa');
const koaBody = require('koa-body');
const koaLogger = require('koa-logger');
const koaSession = require('koa-session');
const koaNjk = require('koa-njk');
const error = require('./middlewares/error');
const koaStatic = require('koa-static');
const path = require('path');
const models = require('./models/models');
const config = require('./config');
const moment = require('./utils/moment');
const marked = require('./utils/marked');
console.log(`当前启动服务的环境是:${process.env.NODE_ENV}`);
const app = new Koa();
// 创建表
models.sync();
app.use(error());
app.use(koaLogger());
app.keys = [ 'pyteam-session' ];
const sessionOpt = {
key: 'SESSIONID' /** (string) cookie key (default is koa:sess) */,
/** (number || 'session') maxAge in ms (default is 1 days) */
/** 'session' will result in a cookie that expires when session/browser is closed */
/** Warning: If a session cookie is stolen, this cookie will never expire */
maxAge: 86400000,
autoCommit: true /** (boolean) automatically commit headers (default true) */,
overwrite: true /** (boolean) can overwrite or not (default true) */,
httpOnly: true /** (boolean) httpOnly or not (default true) */,
signed: true /** (boolean) signed or not (default true) */,
rolling: false /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */,
renew: false /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/
};
app.use(koaSession(sessionOpt, app));
app.use(koaStatic(path.join(__dirname, 'static')));
app.use(
koaNjk(path.join(__dirname, 'views'), '.html', { autoescape: false }, (env) => {
env.addFilter('marked', (content) => {
return marked(content);
});
env.addFilter('fromNow', (time) => {
return moment(time).fromNow();
});
})
);
app.use(
koaBody({
multipart: true,
formidable: {
maxFileSize: 100 * 1024 * 1024 // 设置上传文件大小最大限制,默认100M
}
})
);
// ctx.state
app.use(async (ctx, next) => {
ctx.state = Object.assign(ctx.state, {
_user: ctx.session.user,
is_pjax: ctx.headers['x-pjax'],
admins: config.admins,
base_url: config.base_url,
ws_url: config.ws_url,
ws_secure: config.ws_secure
});
await next();
});
const router = require('./routes/routes');
app.use(router.routes());
// 创建socket.io服务
const server = require('http').Server(app.callback());
const io = require('socket.io')(server);
require('./ws/ws')(io);
server.listen(config.port, () => {
console.log(
`Current NODE_ENV: ${process.env.NODE_ENV}\nListening port on: ${config.port} \nVisit: http://localhost:3002`
);
});