-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (68 loc) · 2.26 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
// Logging
const log = require('another-logger')('index')
// Express
const express = require('express')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const expressSession = require('express-session')
const nuxt = require('./src/nuxt.js')
// Sockets
const socketio = require('socket.io')
const sharedsession = require('express-socket.io-session')
// Database
const r = require('rethinkdb')
// Configuration
const config = require('./config.js')
const nuxtConfig = require('./nuxt.config.js')
// Passing dev argument from command
if (process.argv.includes('--dev')) {
config.dev = nuxtConfig.dev = true
} else if (process.argv.includes('--production')) {
config.dev = nuxtConfig.dev = false
}
// Create an express app
const app = express()
const server = require('http').createServer(app)
// Add session storage
const sessionStore = new expressSession.MemoryStore() // TODO
const session = new expressSession({
store: sessionStore,
secret: 'this is hell',
// TODO: Change these two when shiftinf off MemoryStore
resave: true,
saveUninitialized: true
})
app.use(session)
app.use(bodyParser.json())
app.use(cookieParser())
app.use(nuxt)
app.use((req, res, next) => {
log.http(req.method, req.path, req.query)
next()
})
// Set up websocket server
const io = socketio(server)
io.use(sharedsession(session, {
autoSave:true
}), cookieParser)
// Initialize the database connection and start our stuff after
r.connect(config.rethinkdb, async (err, conn) => {
if (err) throw err
log.success('RethinkDB connected on port', config.rethinkdb.port)
// Log everyone out on server start because lul
r.db('batorume').table('selectors').update({loggedIn: false}).run(conn)
// Register express paths for logging in/out
require('./src/logins.js')(app, r, conn)
// Accept incoming socket connections
io.on('connection', function (socket) {
require('./src/sockets/builder.js')(io, socket, r, conn)
require('./src/sockets/rooms.js')(io, socket, r, conn)
require('./src/sockets/misc.js')(io, socket, r, conn)
})
// Build the Nuxt application
await nuxt.start(nuxtConfig)
// Now that we're built and everything's good, we can start the server
server.listen(config.server.port, () => {
log.success('HTTP server listening on', config.server.port)
})
})