-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
124 lines (95 loc) · 3.36 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
var debug = require('debug')('glintcms-starter-glintcms');
var url = require('url');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var flash = require('connect-flash');
var cookieParser = require('cookie-parser');
var compression = require('compression');
/**
* glintcms application
*
* @param options Object
* @returns {*} http server with express server `app` field.
*/
module.exports = function glintcms(options) {
// get options
var o = require('page-config')(options);
var app = express();
var server = require('http').createServer(app);
// app, server, session, adapter
var Adapter = require('glint-adapter');
var pageAdapter = require('page-adapter')(o.adapter);
var sessionAdapter = Adapter(pageAdapter).db('glint').type('session');
var sessionStore = require('glint-session')(app, sessionAdapter);
var io = require('glint-socket-io')(server, sessionStore);
// pages
// no i18n
var pageAuth = require('page-auth');
var pageAccess = require('page-auth-access');
var pageMain = require('page-main');
var pageFilemanager = require('page-filemanager');
var pageUpload = require('page-upload');
var pageError = require('page-error');
var pageIsBot = require('page-is-bot');
// no view engine setup -> all handled by the page modules
app.use(compression(o.compression)); // middleware order: very early to compress everything. -> huge difference in file size! check network in browser.
app.use(favicon(__dirname + '/public/assets/wrap-layout/favicon.ico'));
app.use(express.static(path.join(__dirname, 'public')));
// enable cors
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(function(req, res, next){
req.__ = res.locals.__ = function noopTranslate(str) {
return str;
};
next();
});
app.use(flash(o.flash));
app.use(cookieParser(o.cookieParser));
// the important stuff happens from here on! -> page routes
var auth = pageAuth(o.auth);
app.use(auth.session); // middleware order: first page middleware
var access = pageAccess(o.access);
app.use(access); // middleware order: after pageAuth session
app.use(auth.user);
app.use(auth.oauth);
if (debug.enabled) {
app.use(function(req, res, next) {
debug('locals', res.locals);
debug('userRole', req.userRole);
debug('userRoleString', req.userRoleString);
debug('userPermission', req.userPermission);
next();
});
}
app.use(o.routes);
app.use(pageIsBot(o.isBot));
app.use(pageAdapter.routes);
app.get(/^\/$/, function(req, res, next){
res.redirect('/home');
});
app.use(pageMain(o.main)); // middleware order: must be after the other pages
app.use(pageFilemanager(o.filemanager));
app.use(pageUpload(o.upload));
if (debug.enabled) {
app.use(function(req, res, next) {
debug('no route');
next();
});
}
app.use(function(err, req, res, next) {
console.error('uncaught error', err);
next(err);
});
// final error handler
app.use(pageError(o.error)); // middleware order: at the very end
// return http server and express app.
var srv = server;
srv.app = app;
return srv;
};