forked from alakajam-team/alakajam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwejam.js
133 lines (118 loc) · 3.65 KB
/
wejam.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict'
/**
* Entry point for the WeJam! game jam system
*
* @description
* Starts the Node server
*
* @module wejam
*/
let log
try {
log = global.log = require('./core/log')
log.info('Starting server...')
} catch (e) {
console.error('Failed to start the server: ' + e.message)
console.error('Did you run "npm install"?')
return
}
const promisify = require('promisify-node')
const fs = promisify('fs')
const path = require('path')
const express = require('express')
const browserRefreshClient = require('browser-refresh-client')
createApp()
/*
* Create, configure and launch the server
*/
async function createApp () {
catchErrorsAndSignals()
await initFilesLayout()
const middleware = require('./core/middleware')
const config = require('./config')
let app = express()
app.locals.devMode = app.get('env') === 'development'
await initDatabase(app.locals.devMode)
await middleware.configure(app)
app.listen(config.SERVER_PORT, configureBrowserRefresh)
log.info('Server started on port ' + config.SERVER_PORT + '.')
}
/*
* Catch unhandled errors and system signals
*/
function catchErrorsAndSignals () {
// Display unhandled rejections more nicely
process.on('unhandledException', (e) => {
log.error('Unhandled promise rejection:', e)
_doGracefulShutdown()
})
process.on('unhandledRejection', (reason, p) => {
log.error('Unhandled promise rejection:', p)
})
// Stop the server gracefully upon shut down signals
// XXX Doesn't work on Windows
let signals = ['SIGINT', 'SIGQUIT', 'SIGTERM']
signals.forEach((signal) => {
process.on(signal, _doGracefulShutdown)
})
function _doGracefulShutdown (cb) {
const db = require('./core/db')
log.info('Shutting down.')
db.knex.destroy(() => process.exit(-1))
}
}
/*
* Initialize files upon first startup
*/
async function initFilesLayout () {
// Create config.js if missing
const CONFIG_PATH = './config.js'
const CONFIG_SAMPLE_PATH = './config.sample.js'
try {
await fs.access(CONFIG_PATH, fs.constants.R_OK)
} catch (e) {
let sampleConfig = await fs.readFile(CONFIG_SAMPLE_PATH)
await fs.writeFile(CONFIG_PATH, sampleConfig)
log.info(CONFIG_PATH + ' initialized with sample values')
}
// Create data folders
const config = require('./config')
const fileStorage = require('./core/file-storage')
await fileStorage.createFolderIfMissing(path.join(config.DATA_PATH, '/tmp'))
await fileStorage.createFolderIfMissing(config.UPLOADS_PATH)
}
/*
* DB initialization
*/
async function initDatabase (withSamples) {
const db = require('./core/db')
let currentVersion = await db.findCurrentVersion()
if (currentVersion > 0) {
log.info('Database found in version ' + currentVersion + '.')
} else {
log.info('Empty database found.')
}
await db.upgradeTables(currentVersion)
if (currentVersion === 0 && withSamples) {
await db.insertSamples()
}
let newVersion = await db.findCurrentVersion()
if (newVersion > currentVersion) {
log.info('Database upgraded to version ' + newVersion + '.')
} else {
log.info('No database upgrade needed.')
}
}
/*
* Use browser-refresh to refresh the browser automatically during development
*/
function configureBrowserRefresh () {
const config = require('./config.js')
if (process.send && config.DEBUG_REFRESH_BROWSER) {
process.send('online')
browserRefreshClient
.enableSpecialReload('*.html *.css *.png *.jpeg *.jpg *.gif *.svg',
{ autoRefresh: false })
.onFileModified(() => browserRefreshClient.refreshPage())
}
}