-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
105 lines (91 loc) · 2.94 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
// Pmx for HTTP analysis. See: http://docs.keymetrics.io/docs/pages/http/
require('pmx').init({
http : true, // HTTP analysis
network : true, // Network monitoring at the application level
ports : true, // Shows which ports your app is listening on (default: false)
});
var http = require('http');
var path = require('path');
var koa = require('koa');
var middlewares = require('koa-middlewares');
var staticCache = require('koa-static-cache');
var serve = require('koa-static-folder');
var passport = require('koa-passport');
var hbs = require('koa-hbs');
var flash = require('koa-flash');
var helmet = require('koa-helmet');
var config = require('./app/config');
var router = require('./app/router');
var db = require('./app/db');
if (true === config.maintain) {
var server = http.createServer(function (req, res) {
var stream = require('fs').createReadStream('views/utils/maintain.html');
stream.pipe(res);
});
server.listen(config.port);
console.info("Listen on http://localhost:%s", config.port);
return;
}
var app = koa(); // initial koa application
app.keys = [config.secret_key, 'i like a boss']; // Key server
app.proxy = true; // active proxy
app.context.db = db; // Database
// Middlewares
app.use(middlewares.compress());
app.use(middlewares.favicon());
app.use(middlewares.rt());
app.use(middlewares.logger());
app.use(middlewares.conditional());
app.use(middlewares.etag());
app.use(middlewares.bodyParser({
limit: '5mb'
}));
app.use(middlewares.session({
store: middlewares.RedisStore()
}));
middlewares.onerror(app);
app.use(flash({ key: config.secret_key }));
app.use(helmet());
app.context.viewpath = path.join(__dirname, 'views');
app.context.assetspath = path.join(__dirname, 'public');
app.use(serve('./public'));
app.use(staticCache(app.context.assetspath, {
buffer: true,
maxAge: 365 * 24 * 60 * 60,
dir: '/public'
}));
// View
require('./app/hbs');
app.context.viewExtName = '.html';
app.use(hbs.middleware({
viewPath: app.context.viewpath,
partialsPath: ['partials', 'handlebars', 'note', 'til', 'components'].map(function(t) { return app.context.viewpath + '/' + t; }),
extname: app.context.viewExtName,
defaultLayout: 'index'
}));
// passport
var passport = require('./app/passport');
app.use(passport.initialize());
app.use(passport.session());
app.use(passport.authenticate('remember-me'));
app.use(function* (next) {
this.state.config = config.view;
this.state.request = this.request;
this.state.is_production = false;
if (this.request.hostname != '127.0.0.1' && this.request.hostname != 'localhost')
this.state.is_production = true;
var user = this.req.user || {};
if (user.password) user.password = '';
this.state.user = user;
yield next;
});
// Router
app
.use(router.routes())
.use(router.allowedMethods());
// Start application
app = module.exports = http.createServer(app.callback());
if (!module.parent) {
app.listen(config.port);
console.info("Listen on http://localhost:%s", config.port);
}