-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
105 lines (87 loc) · 2.96 KB
/
app.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
require('dotenv').config()
const debug = require('debug')('home-rower-game:main')
const getPort = require('get-port')
const express = require('express')
const session = require('express-session')
const Sequelize = require('sequelize')
const nunjucks = require('nunjucks')
const http = require('http')
const path = require('path')
const models = require('./models')
const routes = require('./routes')
const websocket = require('./lib/websocket')
const S4 = require('./s4')
let userDataPath
if (process.versions.electron) {
const electron = require('electron')
userDataPath = (electron.app || electron.remote.app).getPath('userData')
} else {
userDataPath = path.join(__dirname, 'db')
}
const app = express()
const SequelizeStore = require('connect-session-sequelize')(session.Store)
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: path.join(userDataPath, 'session.sqlite3'),
logging: require('debug')('sequelize:session')
})
nunjucks.configure(path.join(__dirname, 'views'), {
express: app,
autoescape: true
})
app.set('view engine', 'njk')
const sessionStore = new SequelizeStore({
db: sequelize
})
const sessionParser = session({
saveUninitialized: true,
secret: process.env.SESSION_SECRET || 'forgotToSetEnv',
store: sessionStore,
cookie: { path: '/', httpOnly: true, maxAge: 365 * 24 * 3600 * 1000 },
resave: false
})
app.use(sessionParser)
sessionStore.sync()
app.use(express.urlencoded({
extended: true
}))
const modulesPath = global.appRoot || __dirname
app.use('/animate.css', express.static(path.join(modulesPath, 'node_modules', 'animate.css')))
app.use('/chart', express.static(path.join(modulesPath, 'node_modules', 'chart.js', 'dist')))
app.use('/jquery', express.static(path.join(modulesPath, 'node_modules', 'jquery', 'dist')))
app.use('/bootstrap', express.static(path.join(modulesPath, 'node_modules', 'bootstrap', 'dist')))
app.use('/bootstrap-icons', express.static(path.join(modulesPath, 'node_modules', 'bootstrap-icons', 'font')))
app.use('/nosleep.js', express.static(path.join(modulesPath, 'node_modules', 'nosleep.js', 'dist')))
app.use(function (req, res, next) {
if (!process.env.FAKE_ROWER && !S4.rower.connected) {
res.render('rower-not-connected')
return
}
next()
})
app.use('/compete', routes.compete)
app.get('/realtime', routes.realtime)
app.use('/rower', routes.rower.modify)
app.use('/register', routes.rower.register)
app.post('/login', routes.rower.login)
app.delete('/logout', routes.rower.logout)
app.get('/', routes.rower.home)
app.use(express.static(path.join(__dirname, 'public')))
const run = async function () {
if (!process.env.FAKE_ROWER) {
S4.init()
}
await models.init()
const server = http.createServer(app)
websocket.init(server, sessionParser)
const port = await getPort({ port: process.env.PORT || 8080 })
server.listen(port, function () {
debug(`Listening on http://localhost:${port}`)
})
return port
}
if (require.main === module) {
run()
} else {
module.exports = run
}