-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
84 lines (68 loc) · 1.77 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
'use strict';
const path = require('path');
const compress = require('koa-compress');
const logger = require('koa-logger');
const serve = require('koa-static');
const koa = require('koa');
const bodyParser = require('koa-bodyparser');
const session = require('koa-session');
const passport = require('koa-passport');
const mount = require('koa-mount');
const mongoose = require('mongoose');
const models = require('./models');
const serviceApp = require('./services');
const viewRouter = require('./routers');
const app = module.exports = koa();
app.config = {
loginAttempts: {
forIp: 50,
forIpAndUser: 7,
logExpiration: '20m'
}
};
/**
* Connect to database.
*/
app.db = mongoose.createConnection('mongodb://localhost/koa-cms');
app.db.on('error', function (err) {
process.stderr.write(`${err.name}: ${err.message}`);
process.exit(1);
});
// register models to mongoose.
models.register(app, mongoose);
// bind models to ctx.
app.use(function *db(next) {
this.models = this.app.db.models;
yield next;
});
// Logger
app.use(logger());
// Serve static files
app.use(serve(path.join(__dirname, 'public')));
// Compress
app.use(compress());
// bodyParser
app.use(bodyParser());
// session
app.keys = ['my-secret'];
app.use(session(app));
// error
app.use(function *errorHandler(next) {
try {
yield next;
} catch (err) {
this.status = err.status || 500;
this.body = err.message;
}
});
// authentication
app.use(passport.initialize());
app.use(passport.session());
require('./middlewares/passport')(app, passport);
// load routes
app.use(mount(viewRouter.middleware()));
serviceApp.mountApp(app, '/service', {authRequired: true});
if (!module.parent) {
app.listen(3000);
console.log('listening on port 3000');
}