feat: refactored server to use ts and improved folder structure
This commit is contained in:
4
server/src/helpers/paths.ts
Normal file
4
server/src/helpers/paths.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import path from 'path'
|
||||
|
||||
export const ROOT_DIR = path.resolve('../')
|
||||
export const WEBUI_DIR = path.join(ROOT_DIR, 'webui', 'public')
|
||||
22
server/src/helpers/port.ts
Normal file
22
server/src/helpers/port.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Port } from '../types'
|
||||
|
||||
/**
|
||||
* Normalize a port into a number, string, or false.
|
||||
*
|
||||
* @since 0.0.0
|
||||
*/
|
||||
export function normalizePort(portString: string): Port {
|
||||
const port = parseInt(portString, 10)
|
||||
|
||||
if (isNaN(port)) {
|
||||
// named pipe
|
||||
return portString
|
||||
}
|
||||
|
||||
if (port >= 0) {
|
||||
// port number
|
||||
return port
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
47
server/src/helpers/server-callbacks.ts
Normal file
47
server/src/helpers/server-callbacks.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import http from 'http'
|
||||
import type { Debugger } from 'debug'
|
||||
|
||||
/**
|
||||
* Event listener for HTTP server "error" event.
|
||||
*
|
||||
* @since 0.0.0
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @since 0.0.0
|
||||
*/
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user