-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
112 lines (90 loc) · 3.61 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
const express = require('express')
const path = require('path')
const cors = require('cors')
const bodyParser = require('body-parser')
const version = require('project-version')
const { correlationMiddleware } = require('@first-lego-league/ms-correlation')
const { authenticationMiddleware, authenticationDevMiddleware } = require('@first-lego-league/ms-auth')
const { loggerMiddleware, Logger } = require('@first-lego-league/ms-logger')
const EventEmitter = require('events').EventEmitter
const { Team } = require('./server/models/Team')
const { Match } = require('./server/models/Match')
const { Table } = require('./server/models/Table')
const logger = new Logger()
const db = require('./server/utilities/mongo_connection')
const appPort = process.env.PORT || 3001
const authenticationMiddlewareToUse = process.env.NODE_ENV === 'development' ? authenticationDevMiddleware() : authenticationMiddleware
logger.info(`-------------------- tournament version ${version} startup --------------------`)
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json({ limit: '50mb' }))
app.use(correlationMiddleware)
app.use(loggerMiddleware)
app.use(cors())
const { getSettingsRouter } = require('./server/routers/general_settings_router')
const { configRouter } = require('./server/routers/config_router')
const crudRouter = require('./server/routers/crud_router').getRouter
const { tournamentDataRouter } = require('./server/routers/tournament_data_router')
const matchTeamRouter = require('./server/routers/matchTeam_router')
const teamsBatchUploadRouter = require('./server/routers/teams_batchupload_router')
const lastMatchIdRouter = require('./server/routers/last_matchId_router').router
const tournamentStatusRouter = require('./server/routers/tournament_status_router')
const { imagesRouter } = require('./server/routers/images_router')
EventEmitter.defaultMaxListeners = 12
app.post(authenticationMiddlewareToUse)
app.put(authenticationMiddlewareToUse)
app.delete(authenticationMiddlewareToUse)
app.use('/settings', getSettingsRouter())
app.use('/image', imagesRouter)
app.all('/config', configRouter)
app.use('/tournamentData', tournamentDataRouter)
const teamLogic = require('./server/logic/team_logic')
app.use('/team', crudRouter({
'collectionName': 'teams',
'IdField': Team.IdField,
'mhubNamespace': 'teams',
'extraRouters': [teamsBatchUploadRouter.getRouter(), matchTeamRouter.getRouter()],
'validationMethods': {
'delete': teamLogic.deleteValidation,
'post': teamLogic.createValidation,
'put': teamLogic.deleteValidation
}
}))
const matchLogic = require('./server/logic/match_logic')
app.use('/match', crudRouter({
'collectionName': 'matches',
'IdField': Match.IdField,
'extraRouters': [lastMatchIdRouter, tournamentStatusRouter.getRouter()],
'preprocessing': matchLogic.offsetMatch,
'mhubNamespace': 'matches'
}))
app.use('/table', crudRouter({
'collectionName': 'tables',
'IdField': Table.IdField,
'mhubNamespace': 'tables'
}))
app.use(authenticationMiddlewareToUse)
app.use(express.static(path.join(__dirname, 'dist/client')))
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/client/index.html'))
})
db.connect().then(() => {
app.listen(appPort, () => {
logger.info('Server started on port ' + appPort)
})
})
process.on('SIGINT', () => {
logger.info('Process received SIGINT: shutting down')
db.close()
process.exit(130)
})
process.on('uncaughtException', err => {
logger.fatal(err.message)
console.error(err)
process.exit(1)
})
process.on('unhandledRejection', err => {
logger.fatal(err.message)
console.error(err)
process.exit(1)
})