refactor: moved out callback to dedicated files

This commit is contained in:
Roberto Tonino 2021-04-03 18:55:33 +02:00
parent 97392917bd
commit 589f7e9800
3 changed files with 79 additions and 88 deletions

View File

@ -1,16 +1,22 @@
import path from 'path'
import http from 'http'
import express, { Application } from 'express'
import cookieParser from 'cookie-parser'
import logger from 'morgan'
import debug0 from 'debug'
import initDebug from 'debug'
import indexRouter from './routes'
import usersRouter from './routes/users'
const ROOT_DIR = path.resolve('../')
import { normalizePort } from './helpers/port'
import { getErrorCb, getListeningCb } from './helpers/server-callbacks'
const ROOT_DIR = path.resolve('../')
const PORT = normalizePort(process.env.PORT || '6595')
const debug = initDebug('deemix-gui:server')
const app: Application = express()
const server = http.createServer(app)
app.use(logger('dev'))
app.use(express.json())
@ -21,89 +27,8 @@ app.use(express.static(path.join(ROOT_DIR, 'webui', 'public')))
app.use('/', indexRouter)
app.use('/users', usersRouter)
/**
* Module dependencies.
*/
app.set('port', PORT)
const debug = debug0('deemix-gui:server')
/**
* Get port from environment and store in Express.
*/
const port = normalizePort(process.env.PORT || '3000')
app.set('port', port)
/**
* Create HTTP server.
*/
const server = http.createServer(app)
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port)
server.on('error', onError)
server.on('listening', onListening)
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort (val:string) {
const port = parseInt(val, 10)
if (isNaN(port)) {
// named pipe
return val
}
if (port >= 0) {
// port number
return port
}
return false
}
/**
* Event listener for HTTP server "error" event.
*/
function onError (error: any) {
if (error.syscall !== 'listen') {
throw error
}
const bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges')
process.exit(1)
case 'EADDRINUSE':
console.error(bind + ' is already in use')
process.exit(1)
default:
throw error
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening () {
const addr = server.address()
if (addr) {
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port
debug('Listening on ' + bind)
}
}
server.listen(PORT)
server.on('error', getErrorCb(PORT))
server.on('listening', getListeningCb(server, debug))

View File

@ -0,0 +1,19 @@
/**
* Normalize a port into a number, string, or false.
*/
export function normalizePort (portString:string) {
const port = parseInt(portString, 10)
if (isNaN(port)) {
// named pipe
return portString
}
if (port >= 0) {
// port number
return port
}
return false
}

View File

@ -0,0 +1,47 @@
import http from 'http'
import type { Debugger } from 'debug'
/**
* Event listener for HTTP server "error" event.
*/
export function getErrorCb (port: number | string | boolean) {
return (error: any) => {
if (error.syscall !== 'listen') {
throw error
}
const bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES': {
console.error(bind + ' requires elevated privileges')
process.exit(1)
}
case 'EADDRINUSE': {
console.error(bind + ' is already in use')
process.exit(1)
}
default:
throw error
}
}
}
/**
* Event listener for HTTP server "listening" event.
*/
export function getListeningCb (server: http.Server, debug: Debugger) {
return () => {
const addr = server.address()
if (addr) {
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port
debug('Listening on ' + bind)
}
}
}