2021-04-03 17:46:54 +00:00
|
|
|
import http from 'http'
|
|
|
|
import express, { Application } from 'express'
|
2021-04-24 16:08:08 +00:00
|
|
|
import { Server as WsServer } from 'ws'
|
2021-06-04 19:29:55 +00:00
|
|
|
import yargs from 'yargs'
|
2021-04-03 17:46:54 +00:00
|
|
|
import initDebug from 'debug'
|
2021-06-04 19:29:55 +00:00
|
|
|
import { hideBin } from 'yargs/helpers'
|
2021-04-03 17:46:54 +00:00
|
|
|
|
|
|
|
import { registerMiddlewares } from './middlewares'
|
|
|
|
|
|
|
|
import indexRouter from './routes'
|
|
|
|
|
|
|
|
import { normalizePort } from './helpers/port'
|
|
|
|
import { getErrorCb, getListeningCb } from './helpers/server-callbacks'
|
|
|
|
import { registerApis } from './routes/api/register'
|
2021-05-11 19:45:24 +00:00
|
|
|
import { registerWebsocket } from './websocket'
|
2021-06-04 19:29:55 +00:00
|
|
|
import type { Arguments } from './types'
|
2021-06-04 19:37:27 +00:00
|
|
|
import { consoleInfo } from './helpers/errors'
|
2021-04-03 17:46:54 +00:00
|
|
|
|
2021-06-04 19:29:55 +00:00
|
|
|
// TODO: Remove type assertion while keeping correct types
|
|
|
|
const argv = yargs(hideBin(process.argv)).options({
|
|
|
|
port: { type: 'string', default: '6595' },
|
2021-06-29 13:58:25 +00:00
|
|
|
host: { type: 'string', default: '127.0.0.1' }
|
2021-06-04 19:29:55 +00:00
|
|
|
}).argv as Arguments
|
|
|
|
|
2021-06-29 13:58:25 +00:00
|
|
|
const DEEMIX_PORT = normalizePort(process.env.DEEMIX_PORT ?? argv.port)
|
|
|
|
const DEEMIX_HOST = process.env.DEEMIX_HOST ?? argv.host
|
2021-04-03 17:46:54 +00:00
|
|
|
|
|
|
|
const debug = initDebug('deemix-gui:server')
|
2021-04-24 16:03:27 +00:00
|
|
|
export const app: Application = express()
|
2021-05-11 19:45:24 +00:00
|
|
|
export const wss = new WsServer({ noServer: true })
|
2021-04-03 17:46:54 +00:00
|
|
|
const server = http.createServer(app)
|
|
|
|
|
|
|
|
/* === Middlewares === */
|
|
|
|
registerMiddlewares(app)
|
|
|
|
|
|
|
|
/* === Routes === */
|
|
|
|
app.use('/', indexRouter)
|
|
|
|
|
|
|
|
/* === APIs === */
|
|
|
|
registerApis(app)
|
|
|
|
|
|
|
|
/* === Config === */
|
2021-06-04 19:29:55 +00:00
|
|
|
app.set('port', DEEMIX_PORT)
|
2021-04-03 17:46:54 +00:00
|
|
|
|
|
|
|
/* === Server port === */
|
2021-04-24 16:03:27 +00:00
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
2021-06-04 19:29:55 +00:00
|
|
|
server.listen({ port: DEEMIX_PORT, host: DEEMIX_HOST })
|
2021-04-24 16:03:27 +00:00
|
|
|
}
|
2021-04-03 17:46:54 +00:00
|
|
|
|
2021-05-11 19:45:24 +00:00
|
|
|
registerWebsocket(wss)
|
2021-05-11 19:06:45 +00:00
|
|
|
|
2021-04-03 17:46:54 +00:00
|
|
|
/* === Server callbacks === */
|
2021-06-04 19:37:27 +00:00
|
|
|
app.on('mount', a => {
|
|
|
|
console.log(a)
|
|
|
|
})
|
|
|
|
server.on('connect', () => {
|
|
|
|
consoleInfo('Server connected')
|
|
|
|
})
|
2021-04-18 10:11:51 +00:00
|
|
|
server.on('upgrade', (request, socket, head) => {
|
2021-05-11 19:06:45 +00:00
|
|
|
wss.handleUpgrade(request, socket, head, socket => {
|
|
|
|
wss.emit('connection', socket, request)
|
2021-04-24 16:03:27 +00:00
|
|
|
})
|
2021-04-18 10:11:51 +00:00
|
|
|
})
|
2021-06-04 19:29:55 +00:00
|
|
|
server.on('error', getErrorCb(DEEMIX_PORT))
|
2021-04-03 17:46:54 +00:00
|
|
|
server.on('listening', getListeningCb(server, debug))
|