-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
90 lines (74 loc) · 2.43 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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const createAdmin = require('./utils/admin');
const template = require('./utils/template');
const loadModels = () => {
require('./models/UserModel');
require('./models/InviteCodeModel');
require('./models/EventModel');
require('./models/MatchModel');
require('./models/MotionworksModel');
require('./models/RunModel');
require('./models/ProcessedTeamModel');
require('./models/TeamModel');
};
// run key checks before start
require('./utils/keyChecks')();
// Configure mongoose's promise to global promise
mongoose.promise = global.Promise;
// setup express
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//Configure Mongoose
mongoose.connect(process.env.DBURL, {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
});
// enable debug if the flag is set
mongoose.set('debug', Boolean(process.env.MONGOOSEDEBUG));
// begin db connection
console.log('connecting to database `' + process.env.DBURL + '`');
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log('connected!');
// load passport
require('./utils/passport');
// load models
loadModels();
// attempt to create or update the configured default admin account
createAdmin(process.env.ADMINEMAIL, process.env.ADMINPASSWORD).then(() => {
console.log('admin done!');
}, (err) => {
if (typeof err === 'string') {
console.log(err);
} else {
console.error('error creating default admin:');
console.error(err);
}
});
// load routes
app.use(require('./routes'));
// route / to static frontend files
app.use('/', express.static(process.env.STATIC));
// listen on the specified port
app.listen(process.env.PORT, () => {
console.log(`listening on :${process.env.PORT}...`);
});
// update all teams processing on launch
require('./utils/processTeam').updateAllTeams();
// fetch all events specified in the template
require('./utils/events').getEvents();
// fetch matches for all events specified
template.events.forEach(eventKey => {
require('./utils/matches').getMatches(eventKey).then(() => {
console.log('Updated matches for ' + template.events.length + ' event(s)');
}, e => {
console.error('error updating matches for event ' + eventKey, e);
});
});
});