forked from jupyter/dashboards_server
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
175 lines (148 loc) · 4.49 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
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
/**
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var cookieSession = require('cookie-session');
var exphbs = require('express-handlebars');
var debug = require('debug')('dashboard-proxy:server');
var passport = require('passport');
var ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn;
var favicon = require('serve-favicon');
var flash = require('connect-flash');
var hbsHelpers = require('./app/handlebars-helpers');
var config = require('./app/config');
var routes = require('./routes/routes');
var authRoutes = require('./routes/auth-routes');
var apiRoutes = require('./routes/api');
var presentationRoutes = require('./routes/presentation');
var app = express();
//////////////
// ENVIRONMENT
//////////////
var env = config.get('NODE_ENV') || 'development';
app.locals.ENV = env;
app.locals.ENV_DEVELOPMENT = env == 'development';
debug('Using environment ' + env);
//////////////
// VIEW ENGINE
//////////////
app.engine('handlebars', exphbs({
defaultLayout: 'main',
layoutsDir: path.join(__dirname, 'views', 'layouts'),
partialsDir: path.join(__dirname, 'views', 'partials'),
helpers: hbsHelpers
}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'handlebars');
//////////////////
// MISC MIDDLEWARE
//////////////////
app.use(logger('dev'));
app.use(cookieParser());
app.use(flash());
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(__dirname + '/public/favicon.ico', { maxAge: 604800000})); // maxAge: 1 week
// redirect trailing slash
app.use(function(req, res, next) {
if(req.url.substr(-1) === '/' && req.url.length > 1) {
res.redirect(301, req.url.slice(0, -1));
} else {
next();
}
});
// cookie session configuration
app.use(cookieSession({
secret: config.get('SESSION_SECRET_TOKEN'),
cookie: {maxAge: 24*3600*1000} //cookie max age set to one day
}));
///////////////////////////////////////
// PUBLIC ROUTES (auth token, no login)
///////////////////////////////////////
app.use('/_api', authRoutes);
////////
// AUTHENTICATION
////////
if(config.get('AUTH_STRATEGY')) {
// Initialize passport and restore auth state from session
app.use(passport.initialize());
app.use(passport.session());
// Load auth strategy based on config
var strategy = require(config.get('AUTH_STRATEGY'))(app);
passport.use(strategy);
// Store passport user object in memory only for now
passport.serializeUser( function(user, done) {
done(null, user);
});
passport.deserializeUser( function(obj, done) {
done(null, obj);
});
// Destroy session on any attempt to logout
app.all('/logout', function(req, res) {
req.session = null;
res.redirect('/');
});
// Ensure login on all following routes
app.use(ensureLoggedIn());
// Pass passport user object to all views
app.use(function(req, res, next) {
res.locals.user = req.user;
next();
});
}
///////////////////////
// AUTHENTICATED ROUTES
///////////////////////
if (config.get('PRESENTATION_MODE')) {
app.use('/', presentationRoutes);
} else {
app.use('/', routes);
}
app.use('/api', apiRoutes);
/////////////////
// ERROR HANDLING
/////////////////
// forward 404 to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// general error handling
app.use(function(err, req, res, next) {
var stacktrace = '';
var status = err.status || 500;
if (app.get('env') === 'development') {
// send stacktrace in development mode
stacktrace = err.stack;
if (status >= 500 && status < 600) {
console.log('STACK:', err.stack);
}
}
res.status(status);
// default to json, only send html if explicitly requested
if (req.accepts('html') &&
!(req.accepts().length === 1 && req.accepts('*/*'))) {
res.render('error', {
title: 'error',
status: status,
message: err.message,
stacktrace: stacktrace
});
} else {
var body = {
message: err.message,
status: status,
error: err,
url: req.url
};
if (stacktrace) {
body.stacktrace = stacktrace;
}
res.send(body);
}
});
module.exports = app;