forked from PATRIC3/p3_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
245 lines (222 loc) · 7.17 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var config = require("./config");
if(config.get("newrelic_license_key")){
require('newrelic');
}
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require("express-session-unsigned");
var RedisStore = require('connect-redis')(session);
var passport = require('passport');
var package = require("./package.json");
var routes = require('./routes/index');
var users = require('./routes/users');
var reportProblem = require('./routes/reportProblem');
var workspace = require('./routes/workspace');
var viewers = require('./routes/viewers');
var remotePage = require('./routes/remotePage');
var search = require('./routes/search');
var contentViewer = require("./routes/content");
var apps = require('./routes/apps');
var uploads = require('./routes/uploads');
var jobs = require('./routes/jobs');
var help = require('./routes/help');
var app = express();
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//app.set('query parser', 'extended');
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
//app.use(bodyParser.json());
//app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser(config.get('cookieSecret')));
var sessionStore = app.sessionStore = new RedisStore(config.get("redis"));
app.use(session({
store: sessionStore,
key: config.get("cookieKey"),
cookie: {domain: config.get('cookieDomain'), maxAge: config.get("sessionTTL")},
// secret: config.get('cookieSecret'),
resave: false,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
if(config.get("enableDevAuth")){
app.use(function(req, res, next){
var user = config.get("devUser");
// console.log("Dev User: ", user, req.isAuthenticated, req.isAuthenticated());
if(user && (!req.isAuthenticated || !req.isAuthenticated() )){
// console.log("Auto Login Dev User");
req.login(user, function(err){
// console.log("login user: ", user);
if(err){
return next(err);
}
// console.log("Dev User logged in. Setup Session");
if(user && req.session){
delete user.password;
req.session.userProfile = user;
req.session.authorizationToken = config.get("devAuthorizationToken");
}else{
console.log("NO Session");
}
next();
});
}else{
next();
}
});
}
app.use(function(req, res, next){
// console.log("Config.production: ", config.production);
// console.log("Session Data: ", req.session);
req.config = config;
req.production = config.get("production") || false;
req.productionLayers = ["p3/layer/core"];
req.package = package;
req.applicationOptions = {
version: "3.0",
gaID: config.get("gaID") || false,
workspaceServiceURL: config.get("workspaceServiceURL"),
appServiceURL: config.get("appServiceURL"),
dataServiceURL: config.get("dataServiceURL"),
homologyServiceURL: config.get("homologyServiceURL"),
genomedistanceServiceURL: config.get("genomedistanceServiceURL"),
compareregionServiceURL: config.get("compareregionServiceURL"),
docsServiceURL: config.get("docsServiceURL"),
enableDevTools: config.get("enableDevTools"),
accountURL: config.get("accountURL"),
appLabel: config.get("appLabel"),
appVersion: package.version
};
// console.log("Application Options: ", req.applicationOptions);
next();
});
// passport serialize/deserialize user
// thise must exist to satisfy passport, but we're not really
// deserializing at the moment to avoid forcing p3-web to depend on the p3-user
// database directly. However, req.session.user is populated by p3-user
// to contain the users' profile, so this isnt' so necessary
passport.serializeUser(function(user, done){
done(null, user.id);
});
passport.deserializeUser(function(id, done){
done(null, {id: id});
});
var proxies = config.get("proxy");
app.use("/p/:proxy/", function(req, res, next){
if(proxies[req.params.proxy]){
apiProxy.web(req, res, {target: proxies[req.params.proxy]});
}else{
next();
}
})
app.use("/portal/portal/patric/Home", [
function(req, res, next){
console.log("Got Portal Request");
next();
},
express.static(path.join(__dirname, "public/cached.html"))
]);
app.use("*jbrowse.conf", express.static(path.join(__dirname, "public/js/jbrowse.conf")));
app.use("/js/" + package.version + "/", [
express.static(path.join(__dirname, 'public/js/release/'), {
maxage: "356d",
/*etag:false,*/
setHeaders: function(res, path){
var d = new Date();
d.setYear(d.getFullYear() + 1);
res.setHeader("Expires", d.toGMTString());
}
}),
]);
app.use("/js/swfobject/", express.static(path.join(__dirname, 'node_modules/swfobject-amd/')));
app.use("/js/", express.static(path.join(__dirname, 'public/js/')));
app.use("/patric/images", express.static(path.join(__dirname, "public/patric/images/"), {
maxage: "365d",
setHeaders: function(res, path){
var d = new Date();
d.setYear(d.getFullYear() + 1);
res.setHeader("Expires", d.toGMTString());
}
}));
app.use("/public/pdfs/", [
function(req, res, next){
res.redirect('https://docs.patricbrc.org/tutorial/')
}
])
app.use("/patric/", express.static(path.join(__dirname, 'public/patric/')));
app.use("/public/", express.static(path.join(__dirname, 'public/')));
app.use('/', routes);
app.post("/reportProblem", reportProblem);
app.use("/workspace", workspace);
app.use("/content", contentViewer);
app.use("/webpage", contentViewer);
app.use("/remote", remotePage);
app.use("/view", viewers);
app.use("/search", search);
app.use("/app", apps);
app.use("/job", jobs);
app.use("/help", help);
app.use("/uploads", uploads);
app.use('/users', users);
app.get("/login",
function(req, res, next){
if(!req.isAuthenticated || !req.isAuthenticated()){
res.redirect(302, config.get("authorizationURL") + "?application_id=" + config.get("application_id"));
}else{
res.render('authcb', {title: 'User Service', request: req});
}
}
);
app.get("/logout", [
function(req, res, next){
console.log("req.params.location: ", req.param('location'));
var redir = req.param('location');
req.session.destroy();
req.logout();
res.redirect(redir || '/');
}
]);
app.get("/auth/callback",
function(req, res, next){
console.log("Authorization Callback");
console.log("req.session.userProfile: ", (req.session && req.session.userProfile) ? req.session.userProfile : "No User Profile");
res.render('authcb', {title: 'User Service', request: req});
// res.redirect("/");
}
);
// catch 404 and forward to error handler
app.use(function(req, res, next){
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if(app.get('env') === 'development'){
app.use(function(err, req, res, next){
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next){
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;