forked from nearzero/elicitation-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
113 lines (80 loc) · 3 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
var getConfig = require('./server/config').get;
var haventSetAdminPassword = require('./server/config').haventSetAdminPassword;
var baseURL = getConfig("BASE_URL");
/**
* Module dependencies.
*/
var express = require('express');
var http = require('http');
var path = require('path');
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser');
var logger = require('morgan');
var errorHandler = require('errorhandler');
var exphbs = require('express-handlebars');
var colors = require('colors');
var app = express();
var viewsDir = __dirname + "/server/views";
var expressHandlebars = exphbs.create({
defaultLayout: "main",
extname: ".hbs",
layoutsDir: viewsDir + "/layouts",
partialsDir: viewsDir + "/partials"
});
console.log("env is: ", app.get('env'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// Set port for azure web app environment
app.set('port', process.env.PORT || 3000);
// Use handlebars
app.engine('.hbs', expressHandlebars.engine);
app.set('view engine', '.hbs');
app.set('views', viewsDir);
// NZ Database
var NZDB = require('./server/nzdb');
var db = new NZDB();
// Setup basic app routes
var router = express.Router();
var assetHelpers = {
baseURL: baseURL
};
if (app.get('env') === 'development') {
router.use('/public/dist', express.static('public/dist/dev'));
}
router.use('/public', express.static('public'));
router.use('/app/widgets/thumbnails', express.static('app/widgets/thumbnails'));
router.get('/', function (req, res) {
res.redirect('./admin');
});
if (getConfig("STANDALONE") || app.get('env') === 'development') {
console.log("ELICITATION_STANDALONE || env=development: enabling standalone features");
if (haventSetAdminPassword()) {
console.error("\nYou haven't set the admin password, please set ENV variable ELICITATION_STANDALONE_ADMIN_PASSWORD for security\n\n".red);
}
var adminRoutes = require('./server/routes/admin')(db, assetHelpers);
router.use('/admin', adminRoutes);
// Convenience authenticator for dev mode
app.get('/authenticate-access-to-elicitation/:id', function (req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
personID: 2
}));
});
}
var elicitationRoutes = require('./server/routes/elicitation')(db, assetHelpers);
router.use('/', elicitationRoutes);
app.use(baseURL, router);
// FIXME: maybe should only do this in dev? think its ok for now...
app.use(errorHandler({ dumpExceptions: true, showStack: true }));
http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
if (getConfig("STANDALONE") || app.get('env') === 'development') {
db.ready.then(function () {
console.log();
console.log(colors.rainbow("Try accessing the elicitation server at:"));
console.log(colors.green("http://localhost:" + app.get('port') + path.join(baseURL + "/admin")));
});
}
});