|
|
@ -1,31 +1,23 @@ |
|
|
|
import compression from 'compression'; |
|
|
|
import express from 'express'; |
|
|
|
import favicon from 'serve-favicon'; |
|
|
|
import compression from 'compression'; |
|
|
|
import helmet from 'helmet'; |
|
|
|
import http from 'http'; |
|
|
|
import https from 'https'; |
|
|
|
import isUndefined from 'lodash/isUndefined.js'; |
|
|
|
import socket from 'socket.io'; |
|
|
|
import winston from 'express-winston'; |
|
|
|
import { join, resolve } from 'path'; |
|
|
|
|
|
|
|
import type { SSLBuffer, Server } from '../shared/interfaces'; |
|
|
|
import { html } from './socketServer/html.js'; |
|
|
|
import { logger } from '../shared/logger.js'; |
|
|
|
|
|
|
|
const trim = (str: string): string => str.replace(/\/*$/, ''); |
|
|
|
const serveStatic = (path: string) => |
|
|
|
express.static(resolve(process.cwd(), 'build', path)); |
|
|
|
import type { SSL, SSLBuffer, Server } from '../shared/interfaces'; |
|
|
|
import { favicon, redirect } from './socketServer/middleware'; |
|
|
|
import { html } from './socketServer/html'; |
|
|
|
import { listen } from './socketServer/socket'; |
|
|
|
import { logger } from '../shared/logger'; |
|
|
|
import { serveStatic, trim } from './socketServer/assets'; |
|
|
|
import { loadSSL } from './socketServer/ssl'; |
|
|
|
|
|
|
|
export function server( |
|
|
|
export async function server( |
|
|
|
{ base, port, host, title, bypassHelmet }: Server, |
|
|
|
{ key, cert }: SSLBuffer, |
|
|
|
): SocketIO.Server { |
|
|
|
ssl?: SSL, |
|
|
|
): Promise<SocketIO.Server> { |
|
|
|
const basePath = trim(base); |
|
|
|
|
|
|
|
logger.info('Starting server', { |
|
|
|
key, |
|
|
|
cert, |
|
|
|
ssl, |
|
|
|
port, |
|
|
|
base, |
|
|
|
title, |
|
|
@ -38,15 +30,8 @@ export function server( |
|
|
|
.use(`${basePath}/client`, serveStatic('client')) |
|
|
|
.use(winston.logger(logger)) |
|
|
|
.use(compression()) |
|
|
|
.use(favicon(join('build', 'assets', 'favicon.ico'))); |
|
|
|
/* .use((req, res, next) => { |
|
|
|
if (req.path.substr(-1) === '/' && req.path.length > 1) |
|
|
|
res.redirect( |
|
|
|
301, |
|
|
|
req.path.slice(0, -1) + req.url.slice(req.path.length), |
|
|
|
); |
|
|
|
else next(); |
|
|
|
}); */ |
|
|
|
.use(favicon) |
|
|
|
.use(redirect); |
|
|
|
|
|
|
|
// Allow helmet to be bypassed.
|
|
|
|
// Unfortunately, order matters with middleware
|
|
|
@ -58,24 +43,7 @@ export function server( |
|
|
|
const client = html(basePath, title); |
|
|
|
app.get(basePath, client).get(`${basePath}/ssh/:user`, client); |
|
|
|
|
|
|
|
return socket( |
|
|
|
!isUndefined(key) && !isUndefined(cert) |
|
|
|
? https.createServer({ key, cert }, app).listen(port, host, () => { |
|
|
|
logger.info('Server started', { |
|
|
|
port, |
|
|
|
connection: 'https', |
|
|
|
}); |
|
|
|
}) |
|
|
|
: http.createServer(app).listen(port, host, () => { |
|
|
|
logger.info('Server started', { |
|
|
|
port, |
|
|
|
connection: 'http', |
|
|
|
}); |
|
|
|
}), |
|
|
|
{ |
|
|
|
path: `${basePath}/socket.io`, |
|
|
|
pingInterval: 3000, |
|
|
|
pingTimeout: 7000, |
|
|
|
}, |
|
|
|
); |
|
|
|
const sslBuffer: SSLBuffer = await loadSSL(ssl); |
|
|
|
|
|
|
|
return listen(app, host, port, basePath, sslBuffer); |
|
|
|
} |
|
|
|