-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathapp.js
156 lines (129 loc) · 3.52 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict'
const Koa = require('koa');
const app = new Koa();
const router = require('koa-router')();
const Pug = require('koa-pug');
const co = require('co');
const convert = require('koa-convert');
const json = require('koa-json');
const onerror = require('koa-onerror');
const bodyparser = require('koa-bodyparser')();
const logger = require('koa-logger');
const session = require('koa-generic-session');
const redisStore = require('koa-redis');
const UglifyJS = require('uglify-js')
const config = require('./config');
const index = require('./routes/index');
const user = require('./routes/user');
const topic = require('./routes/topic');
const reply = require('./routes/reply');
const tools = require('./common/tools');
const VIEWSDIR = __dirname + '/views';
let assets = {};
app.keys = ['easyclub'];
onerror(app);
// logger
app.use(convert(logger()));
// 本地调试状态
if(config.debug) {
app.use(require('./middlewares/stylus')(__dirname));
const livereload = require('livereload');
let server = livereload.createServer({
exts: ['jade','styl']
});
server.watch([
__dirname + '/public',
VIEWSDIR
]);
} else {
try {
assets = require('./assets.json');
} catch (e) {
console.error('请先执行 make build 生成assets.json文件')
throw e;
}
}
// middlewares
app.use(convert(require('koa-static2')("/public",__dirname + '/public')));
app.use(convert(bodyparser));
app.use(convert(json()));
app.use(convert(session({
store: redisStore(config.redis)
})));
app.use(require('./middlewares/return_data'));
const pug = new Pug({
viewPath: VIEWSDIR,
debug: config.debug,
noCache: config.debug,
helperPath: [
{
getTimeAgo : tools.getTimeAgo,
Loader : require('loader')
}
],
app: app
});
// 压缩行内样式
pug.options.filters = {
uglify: function (text, options) {
if(config.debug){
return text;
} else {
let result = UglifyJS.minify(text, {fromString: true});
return result.code;
}
}
}
app.use(async (ctx, next) => {
if(!ctx.model)
ctx.model = require('./models');
await next();
})
app.use(async (ctx, next) => {
if(config.debug && ctx.cookies.get('dev-user')) {
// 测试用户使用
var testuser = JSON.parse(ctx.cookies.get('dev-user'));
let user = await ctx.model('user').findOneQ({
username: testuser.username
});
ctx.session.user = user;
if(testuser.isAdmin) {
ctx.session.user.isAdmin = true;
}
}
// 添加模板变量
pug.locals = Object.assign(pug.locals, {
sitename: config.sitename,
config: config,
assets: assets,
isDebug: config.debug
})
if(ctx.session.user) {
let user = ctx.state.current_user = ctx.session.user;
// 读取at数据
let messages = await ctx.model('message').find({
master_id: user._id,
is_read: false
})
if(messages){
user.messageLen = messages.length;
}
// 判断是否是管理员帐号
if(config.admins.indexOf(user.username) != -1) {
user.isAdmin = true;
}
}
await next();
});
app.use(require('./middlewares/jade_partial')(VIEWSDIR));
router.use('/', index.routes(), index.allowedMethods());
router.use('/user', user.routes(), user.allowedMethods());
router.use('/topic', topic.routes(), topic.allowedMethods());
router.use('/reply', reply.routes(), topic.allowedMethods());
app.use(router.routes(), router.allowedMethods());
// response
app.on('error', function(err, ctx){
console.error(err);
// console.error('server error', err, ctx);
});
module.exports = app;