-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
160 lines (145 loc) · 4.58 KB
/
index.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
var configFile;
if (process.env.CONFIG) {
configFile = JSON.parse(process.env.CONFIG);
} else {
var fs = require('fs');
try {
configFile = JSON.parse(fs.readFileSync(process.env.CONFIGFILE || './config/config.json', 'utf8'));
} catch (err) {
console.log('FAILED to load config file', err)
configFile = false
}
}
var _ = require('lodash');
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var google = require('googleapis');
var url = require('url');
var OAuth2 = google.auth.OAuth2;
var googleAuth = require('./lib/google-auth');
var swig = require('swig');
var Sequelize = require('sequelize');
var SchemaActionModel = require('./schema-actions.js')
var dbconn = {};
var server;
var redis;
var shutdown = function() {
server.close();
}
// Launch server.
var boot = function(config, startOnPort) {
var app = express();
if (!config) {
console.log('loading config from file: ', configFile);
config = configFile;
}
var sessionConfig = {
secret: config.sessionSecret,
resave: false,
saveUninitialized: false
}
if (config.redisSessionStore || config.redisStore || config.fakeRedis) {
if (config.fakeRedis) {
redis = require("fakeredis").createClient()
} else if (config.redisStore) {
redis = require("redis").createClient(config.redisStore)
}
var redisSessionConfig = (config.fakeRedis
? {client: redis }
: config.redisSessionStore || config.redisStore)
var RedisStore = require('connect-redis')(session);
sessionConfig['store'] = new RedisStore(redisSessionConfig);
}
// Configure Express app
var sessionInstance = session(sessionConfig)
app.use(function(req, res, next) {
// This stops session stuff except for admin contexts
if (/^\/($|admin|auth|logout)/.test(req.path)) {
return sessionInstance(req, res, next)
} else {
next()
}
});
app.use(express.static(__dirname + '/static'));
app.use(bodyParser.urlencoded({ extended: true }));
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
var modules = [];
//MODULES
if (config.extensionModules) {
modules = config.extensionModules.map(function(m) {
return require(m);
});
modules.forEach(function(m) {
if (m.static) {
console.log(m.static);
app.use(express.static(m.static));
}
});
}
//MODELS
var db = config.db;
var sequelize = new Sequelize(db.database, db.user, db.pass, db);
var schema = require('./schema.js')(sequelize);
dbconn.schema = schema;
sequelize.authenticate();
dbconn.ready = sequelize.sync()
var schemaActions = SchemaActionModel(config, schema, sequelize, redis);
//VIEWS
var public_views = require('./public.js')(app, schema, schemaActions, config);
var adminauth;
//if (/\/\/localhost/.test(config.baseUrl) && config.develMode) {
if (config.develMode) {
adminauth = function(req,res,next) {next();};
} else if (config.adminAuth) {
adminauth = require(config.adminAuth)(app, config);
} else {
var oauth2Client = new OAuth2(
config.oauth.clientId,
config.oauth.clientSecret,
config.baseUrl + '/auth/google/callback'
);
adminauth = (googleAuth({'oauth2Client': oauth2Client,
'app': app,
'whitelist': config.oauthAllowedUsers
}).confirm);
}
var view_dirs = [__dirname + '/views'];
//MODULES
// get the links that will be available in the admin
// -- all other views, the module should setup itself
var moduleLinks = modules.map(function(m) {
var moduleResult = m(app, schema, sequelize, adminauth, config);
view_dirs.push(moduleResult.viewDirectory);
return moduleResult.link;
});
app.set('views', view_dirs);
var admin_views = require('./admin.js')(app, schema, sequelize, adminauth, config, moduleLinks, schemaActions);
app.get('/',
adminauth, function (req, res) {
res.redirect('/admin/');
}
);
if (startOnPort && config.port) {
server = app.listen(config.port, function () {
var port = server.address().port;
if (require.main === module) {
console.log('App listening at %s:%s', config.baseUrl, port);
}
});
}
app.schemaActions = schemaActions
return app
}
if (require.main === module) {
boot(configFile, true);
} else {
exports.server = server;
exports.boot = boot;
exports.db = dbconn;
exports.shutdown = shutdown;
if (configFile) {
exports.app = boot(configFile);
}
}