forked from blackbaud/barkbaud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (135 loc) · 4.46 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
/*jslint node: true, nomen: true*/
(function () {
'use strict';
var app,
bodyParser,
callbacks,
colors,
cookieParser,
cors,
Database,
database,
databaseUri,
environment,
express,
fs,
http,
https,
MongoStore,
mongoose,
port,
routes,
server,
session,
sessionConfig,
timeout;
// Dependencies.
fs = require('fs');
cors = require('cors');
http = require('http');
https = require('https');
routes = require('./server/routes');
colors = require('colors');
express = require('express');
session = require('express-session');
timeout = require('connect-timeout');
Database = require('./server/database');
mongoose = require('mongoose');
MongoStore = require('connect-mongo/es5')(session);
bodyParser = require('body-parser');
callbacks = [];
databaseUri = process.env.DATABASE_URI || null;
environment = process.env.NODE_ENV || 'development';
port = process.env.PORT || 5000;
sessionConfig = {
resave: false,
saveUninitialized: true,
secret: '+rEchas&-wub24dR'
};
// Check if database URI has been provided.
if (databaseUri === null) {
console.log(colors.red("Please specify a DATABASE_URI in .env!"));
process.exit();
}
// Only cache the authorized session in production.
if (environment === "production") {
sessionConfig.store = new MongoStore({
url: databaseUri,
autoRemove: 'native'
});
}
// Connect to the database.
database = new Database({
uri: databaseUri,
service: mongoose
});
// Create the app.
app = express();
app.set('port', port);
app.use(bodyParser.json());
app.use(timeout('120s'));
app.use(session(sessionConfig));
app.use(cors({
credentials: true,
origin: [
'http://localhost:5000',
'http://localhost:8080'
]
}));
// Routes.
app.use('/', express.static(__dirname + '/ui'));
// Authentication routes.
app.get('/auth/authenticated', routes.auth.getAuthenticated);
app.get('/auth/login', routes.auth.getLogin);
app.get('/auth/callback', routes.auth.getCallback);
app.get('/auth/logout', routes.auth.getLogout);
// API GET routes.
app.get('/api/dogs', routes.auth.checkSession, routes.api.getDogs);
app.get('/api/dogs/:dogId', routes.auth.checkSession, routes.api.getDog);
app.get('/api/dogs/:dogId/notes', routes.auth.checkSession, routes.api.getNotes);
app.get('/api/dogs/:dogId/currenthome', routes.auth.checkSession, routes.api.getCurrentHome);
app.get('/api/dogs/:dogId/previoushomes', routes.auth.checkSession, routes.api.getPreviousHomes);
app.get('/api/dogs/:dogId/findhome', routes.auth.checkSession, routes.api.getFindHome);
// API POST routes.
app.post('/api/dogs/:dogId/currenthome', routes.auth.checkSession, routes.api.postCurrentHome);
app.post('/api/dogs/:dogId/notes', routes.auth.checkSession, routes.api.postNotes);
// Connect to the database.
database.connect(function () {
// If we're running database setup, we don't need to start the server.
// `node index.js --build-database`
if (process.env.npm_config_build_database) {
database.setup(function () {
triggerReady();
process.exit();
});
} else {
// Start the server.
if (environment === 'production') {
server = http.createServer(app);
} else {
server = https.createServer({
key: fs.readFileSync('./server/sslcerts/server.key', 'utf8'),
cert: fs.readFileSync('./server/sslcerts/server.crt', 'utf8')
}, app);
}
// Listen to the port.
server.listen(app.get('port'), function () {
console.log('Barkbaud is running on port', app.get('port'));
triggerReady();
});
}
});
// Alerts outside resources when server has been created.
function triggerReady() {
if (callbacks.length > 0) {
callbacks.forEach(function (callback) {
callback();
});
}
}
module.exports = {
ready: function (callback) {
callbacks.push(callback);
}
};
}());