forked from FirstLegoLeague/tournament
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (74 loc) · 2.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
'use strict'
const express = require('express')
const path = require('path')
const cors = require('cors')
const bodyParser = require('body-parser')
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 logger = Logger()
const crudRouter = require('./routers/crudRouter').getRouter
const Team = require('./models/Team')
const Match = require('./models/Match')
const Table = require('./models/Table')
const appPort = process.env.PORT || 3001
logger.setLogLevel(process.env.LOG_LEVEL || logger.LOG_LEVELS.DEBUG)
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(correlationMiddleware)
app.use(loggerMiddleware)
app.use(cors())
const { getSettingsRouter, setDefaultSettings } = require('./routers/generalSettingsRouter')
const tournamentDataRouter = require('./routers/tournamentDataRouter')
const matchTeamRouter = require('./routers/matchTeamRouter')
const teamsBatchUploadRouter = require('./routers/teamsBatchUploadRouter')
setDefaultSettings()
app.use('/settings', getSettingsRouter())
if (process.env.DEV) {
app.use(authenticationDevMiddleware())
} else {
app.use(authenticationMiddleware)
}
app.use('/tournamentData', tournamentDataRouter)
const teamLogic = require('./logic/teamLogic')
app.use('/team', crudRouter({
'collectionName': 'teams',
'IdField': Team.IdField,
'mhubNamespace': 'teams',
'extraRouters': [matchTeamRouter.getRouter(), teamsBatchUploadRouter.getRouter()],
'validationMethods': {
'delete': teamLogic.deleteValidation
}
}))
app.use('/match', crudRouter({
'collectionName': 'matches',
'IdField': Match.IdField,
'mhubNamespace': 'matches'
}))
app.use('/table', crudRouter({
'collectionName': 'tables',
'IdField': Table.IdField,
'mhubNamespace': 'tables'
}))
app.use(express.static(path.join(__dirname, 'dist/client')))
// Design files
app.use('/design', express.static(path.resolve(__dirname, 'node_modules/@first-lego-league/user-interface/current/assets')))
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/client/index.html'))
})
app.listen(appPort, () => {
logger.info('Server started on port ' + appPort)
})
process.on('SIGINT', () => {
logger.info('Process received SIGINT: shutting down')
process.exit(130)
})
process.on('uncaughtException', err => {
logger.fatal(err.message)
process.exit(1)
})
process.on('unhandledRejection', err => {
logger.fatal(err.message)
process.exit(1)
})