-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
187 lines (172 loc) · 6.43 KB
/
server.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var express = require('express')
var path = require('path')
var logger = require('morgan')
var compression = require('compression')
var methodOverride = require('method-override')
var session = require('express-session')
var flash = require('express-flash')
var bodyParser = require('body-parser')
var expressValidator = require('express-validator')
var dotenv = require('dotenv')
var mongoose = require('mongoose')
var passport = require('passport')
var kue = require('kue')
var MongoStore = require('connect-mongo')(session)
var async = require('async')
var basicAuth = require('basic-auth-connect')
var favicon = require('serve-favicon')
var i18next = require('i18next')
var i18nextMiddleware = require('i18next-express-middleware')
var i18nextFilesystemBackend = require('i18next-node-fs-backend')
// Load environment variables from .env file
dotenv.load()
// Kue Job Queue
var queue = kue.createQueue({
redis: process.env.REDIS_URL
})
// i18n
i18next
.use(i18nextMiddleware.LanguageDetector)
.use(i18nextFilesystemBackend)
.init({
backend: {
loadPath: 'locales/{{lng}}/{{ns}}.json'
},
detection: {
caches: ['cookie']
},
debug: false
})
// Controllers
var HomeController = require('./controllers/home')
var userController = require('./controllers/user')
var contactController = require('./controllers/contact')
var aboutController = require('./controllers/about')
var remindController = require('./controllers/remind')
var pillController = require('./controllers/pill')
var settingsController = require('./controllers/settings')
// Passport OAuth strategies
require('./config/passport')
var app = express()
// queue.process('remind-console', remindController.consoleController);
// queue.process('remind-email', remindController.emailController);
// queue.process('remind-sms', remindController.smsController);
queue.process('remind-push', remindController.pushController)
queue.process('remind-telegram', remindController.telegramController)
queue.process('remind-remind', remindController.remindController)
mongoose.connect(process.env.MONGODB)
mongoose.connection.on('error', function () {
console.log('MongoDB Connection Error. Please make sure that MongoDB is running.')
process.exit(1)
})
app.use(i18nextMiddleware.handle(i18next))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')
app.set('port', process.env.PORT || 3000)
app.use(compression())
app.use(logger('dev'))
app.use(favicon(path.join(__dirname, 'public', 'assets', 'image', 'favicon.ico')))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(expressValidator())
app.use(methodOverride('_method'))
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: mongoose.connection,
touchAfter: 24 * 3600
})
}))
app.use(flash())
app.use(passport.initialize())
app.use(passport.session())
app.use(function (req, res, next) {
res.locals.user = req.user
next()
})
app.use(express.static(path.join(__dirname, 'public')))
app.get('/', HomeController.index)
app.get('/pill', userController.ensureAuthenticated, pillController.pillsGet)
app.get('/pill/:id', userController.ensureAuthenticated, pillController.pillGet)
app.put('/pill/:id', userController.ensureAuthenticated, pillController.pillPut)
app.delete('/pill/:id', userController.ensureAuthenticated, pillController.pillDelete)
app.post('/pill', userController.ensureAuthenticated, pillController.pillPost)
app.get('/about', aboutController.aboutGet)
app.get('/contact', contactController.contactGet)
app.post('/contact', contactController.contactPost)
app.get('/account', userController.ensureAuthenticated, userController.accountGet)
app.put('/account', userController.ensureAuthenticated, userController.accountPut)
app.delete('/account', userController.ensureAuthenticated, userController.accountDelete)
app.get('/signup', userController.signupGet)
app.post('/signup', userController.signupPost)
app.get('/login', userController.loginGet)
app.post('/login', userController.loginPost)
app.get('/forgot', userController.forgotGet)
app.post('/forgot', userController.forgotPost)
app.get('/reset/:token', userController.resetGet)
app.post('/reset/:token', userController.resetPost)
app.get('/logout', userController.logout)
app.get('/settings', userController.ensureAuthenticated, settingsController.settingsGet)
app.post('/link/push', userController.ensureAuthenticated, settingsController.link.pushPost)
app.get('/link/telegram/:token', userController.ensureAuthenticated, settingsController.link.telegramGet)
app.get('/unlink/:provider', userController.ensureAuthenticated, userController.unlink)
app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }))
app.get('/auth/google/callback', passport.authenticate('google', { successRedirect: '/', failureRedirect: '/login' }))
app.use('/kue', basicAuth(process.env.KUE_WEB_USER, process.env.KUE_WEB_PASSWORD), kue.app)
// Production error handler
if (app.get('env') === 'production') {
// unhandled routes
app.use(function (req, res) {
let status = 403
res.status(status).render('error', { title: 'Error', status: status })
})
// internal server errors
app.use(function (err, req, res, next) {
console.error(err.stack)
let status = err.status || 500
// res.sendStatus(status)
res.status(status).render('error', { title: 'Error', status: status })
})
}
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'))
})
let gracefulExitHandler = function () {
async.series([
function (callback) {
mongoose.connection.close(function (err) {
if (err) {
console.log('mongoose disconnect error: ', err)
callback(err)
} else {
console.log('mongoose connection is disconnected through app termination.')
callback(null)
}
})
},
function (callback) {
queue.shutdown(5000, function (err) {
if (err) {
console.log('queue shutdown error: ', err)
callback(err)
} else {
console.log('kue is shut down through app termination.')
callback(null)
}
})
}
],
function (err, results) {
if (err) {
console.log('graceful termination failed.')
process.exit()
} else {
process.exit(0)
}
})
}
process.once('SIGINT', gracefulExitHandler)
process.once('SIGTERM', gracefulExitHandler)
module.exports = app