-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
138 lines (117 loc) · 3.73 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
'use strict'
const http = require('http')
const Koa = require('koa')
const convert = require('koa-convert')
const chalk = require('chalk')
// Middleware and helpers
const path = require('path')
const compress = require('koa-compress')
const session = require('koa-generic-session')
const mongoose = require('mongoose')
const MongoStore = require('koa-generic-session-mongo')
const serve = require('koa-static')
const parse = require('koa-bodyparser')
const views = require('koa-views')
const router = require('koa-router')()
const favicon = require('koa-favicon')
const passport = require('koa-passport')
const createLogger = require('concurrency-logger').default
const agenda = require('./services/agenda-service')
const filterLog = require('./utils/filter-log')
// Import configs and auth middleware
const config = require('./config')
const auth = require('./auth/auth')
// Socket.io instance
const io = require('./io')
// Import routes
const index = require('./routes/index')
const users = require('./routes/users')
const lists = require('./routes/lists')
const items = require('./routes/items')
const sessions = require('./routes/sessions')
const app = new Koa()
auth()
// Bodyparser
app.use(parse())
// Logger
app.use(createLogger({
req: (ctx) => {
return chalk.gray(ctx.originalUrl) + ' ' +
(ctx.method === 'POST' && !process.env.QUIET ? filterLog(ctx.request.body) : '')
},
res: (ctx) => (
chalk.gray(ctx.originalUrl) + ' ' +
(ctx.response.header['content-length'] ? ctx.response.header['content-length'] + 'b ' : '') +
(ctx.method === 'POST' && !process.env.QUIET ? filterLog(ctx.body) : '')
)
}))
// Mongoose setup
// Still uses mpromise by default, so manually set it to native Promise
mongoose.Promise = global.Promise
mongoose.connect(config.mongoose.url)
// Compression
app.use(compress(config.compress))
// Sessions
app.keys = ['suzy eats a suzy snack']
app.use(convert(session({
key: 'koa.sid',
cookie: {
maxAge: null,
signed: false
},
store: new MongoStore(config.session)
})))
// Favicon
app.use(favicon(path.join(__dirname, '/public/images/favicon.ico')))
// Initialize Passport
app.use(passport.initialize())
app.use(passport.session())
// Initialize Agenda
agenda.on('ready', function () {
agenda.cancel({
name: 'Agenda running'
})
agenda.start()
})
// Views
app.use(views(path.join(__dirname, 'views'), {
extension: 'pug'
}))
// Serve static
app.use(serve(path.join(__dirname, 'public'), config.static))
// Routes
router.get('/', index.index)
router.redirect('/app', '/')
router.redirect('/login', '/')
router.redirect('/create', '/')
router.redirect('/reset', '/')
router.redirect('/forgot', '/')
router.get('/404', index.fourOhFour)
router.get('/sessions', sessions.get)
router.post('/users/login', users.setCookieAge, users.login)
router.put('/users/create', users.setCookieAge, users.create)
router.post('/users/:username/update', users.update)
router.post('/users/:username/forgot', users.forgot)
router.post('/users/reset', users.reset)
router.get('/users/logout', users.logout)
router.get('/lists/:listid', lists.get)
router.put('/lists/create', lists.create)
router.delete('/lists/:listid/delete', lists.delete)
router.post('/lists/:listid/update', lists.update)
router.put('/lists/:listid/items/create', items.create)
router.post('/lists/:listid/items/:itemid/update', items.update)
router.delete('/lists/:listid/items/:itemid/delete', items.delete)
app.use(router.routes())
app.use(router.allowedMethods())
// 404
app.use(async function (ctx, next) {
if (ctx.status === 404) {
ctx.redirect('/404')
}
await next
})
// Attach socket.io instance to app
const server = http.createServer(app.callback())
io.attach(server)
server.listen(config.koa.port)
console.log('Listening on port ' + config.koa.port)