-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapp.js
146 lines (126 loc) · 3.59 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
/**
gaoqi-blog app.js
*/
'use strict'
const config = require('./config')
const _ = require('lodash')
const csurf = require('csurf')
const cors = require('cors')
const path = require('path')
const Loader = require('loader')
const LoaderConnect = require('loader-connect')
const express = require('express')
const session = require('express-session')
const errorhandler = require('errorhandler')
const RedisStore = require('connect-redis')(session)
const redisClient = require('./common/redis.js')
const passport = require('passport')
const GitHubStrategy = require('passport-github').Strategy
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const compress = require('compression')
const busboy = require('connect-busboy')
require('./models')
const auth = require('./middleware/auth')
const online = require('./middleware/online')
const render = require('./common/render')
const cutter = require('./common/cutter')
const webRouter = require('./web_router')
const webApi = require('./web_api')
const app = express()
// 静态文件目录
const staticDir = path.join(__dirname, 'public')
let assets = {}
if (process.env.NODE_ENV === 'production') {
try {
assets = require('./assets.json')
} catch (e) {
console.log('You must execute `make build` before start app when mini_assets is true.')
throw e
}
}
app.disable('x-powered-by')
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'html')
app.engine('html', require('ejs-mate'))
app.locals._layoutFile = 'layout.html'
app.use(require('./middleware/wrap').render)
app.use(require('./middleware/wrap').send)
app.use(require('response-time')())
app.use(bodyParser.json({ limit: '1mb' }))
app.use(bodyParser.urlencoded({ extended: true, limit: '1mb' }))
app.use(require('method-override')())
app.use(cookieParser(config.session_secret))
app.use(compress())
app.use(session({
secret: config.session_secret,
store: new RedisStore({
client: redisClient,
port: config.redis_port,
host: config.redis_host
}),
resave: true,
saveUninitialized: true
}))
app.use(passport.initialize())
// custom middleware 过滤未登陆
app.use(auth.authUser)
// 缓存已登录用户标识在线
app.use(online.cacheOnline)
// 静态资源
if (config.debug) {
app.use(LoaderConnect.less(__dirname)) // 测试环境用,编译 .less on the fly
}
app.use('/public', express.static(staticDir))
if (!config.debug) {
app.use(function (req, res, next) {
if (req.path.indexOf('/api') === -1) {
csurf()(req, res, next)
return
}
next()
})
app.set('view cache', true)
}
// github oauth
passport.serializeUser(function (user, done) {
done(null, user)
})
passport.deserializeUser(function (user, done) {
done(null, user)
})
passport.use(new GitHubStrategy(config.GITHUB_OAUTH, function (accessToken, refreshToken, profile, done) {
done(null, profile)
}))
// set static, dynamic helpers
_.extend(app.locals, {
config,
Loader,
assets
})
_.extend(app.locals, render)
_.extend(app.locals, cutter)
app.use(function (req, res, next) {
res.locals.csrf = req.csrfToken ? req.csrfToken() : ''
next()
})
app.use(busboy({
limits: {
fileSize: 10 * 1024 * 1024 // 10MB
}
}))
app.use('/', webRouter)
app.use('/api', cors(), webApi)
// error handler
if (config.debug) {
app.use(errorhandler())
} else {
app.use(function (err, req, res) {
console.log(err)
return res.status(500).send('500 status')
})
}
app.listen(process.env.PORT || config.port, function () {
console.log('GaoqiBlog listening on port %s in %s mode', process.env.PORT || config.port, app.settings.env)
})
module.exports = app