Use https if config is present

This commit is contained in:
Dennis Postma 2025-02-09 02:09:58 +01:00
parent b7748c254f
commit 66759a87f2
2 changed files with 19 additions and 6 deletions

View File

@ -1,4 +1,6 @@
import fs from 'fs'
import { createServer as httpServer, Server as HTTPServer } from 'http' import { createServer as httpServer, Server as HTTPServer } from 'http'
import { createServer as httpsServer, Server as HTTPSServer } from 'https'
import cors from 'cors' import cors from 'cors'
import express, { type Application } from 'express' import express, { type Application } from 'express'
@ -15,7 +17,7 @@ import { TexturesController } from '#controllers/textures'
*/ */
class HttpManager { class HttpManager {
private readonly app: Application private readonly app: Application
private readonly http: HTTPServer private readonly server: HTTPServer | HTTPSServer
private readonly logger = Logger.type(LoggerType.APP) private readonly logger = Logger.type(LoggerType.APP)
private readonly authController: AuthController = new AuthController() private readonly authController: AuthController = new AuthController()
private readonly avatarController: AvatarController = new AvatarController() private readonly avatarController: AvatarController = new AvatarController()
@ -27,7 +29,18 @@ class HttpManager {
this.app.use(cors()) this.app.use(cors())
this.app.use(express.json()) this.app.use(express.json())
this.app.use(express.urlencoded({ extended: true })) this.app.use(express.urlencoded({ extended: true }))
this.http = httpServer(this.app)
if (config.PUBLIC_KEY_PATH && config.PRIVATE_KEY_PATH) {
const credentials = {
key: fs.readFileSync(config.PRIVATE_KEY_PATH),
cert: fs.readFileSync(config.PUBLIC_KEY_PATH)
}
this.server = httpsServer(credentials, this.app)
this.logger.info('HTTPS server initialized')
} else {
this.server = httpServer(this.app)
this.logger.info('HTTP server initialized')
}
} }
/** /**
@ -35,7 +48,7 @@ class HttpManager {
* @param app * @param app
*/ */
public async boot(app: Application) { public async boot(app: Application) {
this.http.listen(config.PORT, config.HOST) this.server.listen(config.PORT, config.HOST)
// Add CORS middleware // Add CORS middleware
app.use( app.use(
@ -80,8 +93,8 @@ class HttpManager {
return this.app return this.app
} }
getHttpInstance(): HTTPServer { getServerInstance(): HTTPServer | HTTPSServer {
return this.http return this.server
} }
} }

View File

@ -21,7 +21,7 @@ export class Server {
// Initialize managers // Initialize managers
await Promise.all([ await Promise.all([
HttpManager.boot(HttpManager.getAppInstance()), HttpManager.boot(HttpManager.getAppInstance()),
SocketManager.boot(HttpManager.getAppInstance(), HttpManager.getHttpInstance()), SocketManager.boot(HttpManager.getAppInstance(), HttpManager.getServerInstance()),
QueueManager.boot(), QueueManager.boot(),
UserManager.boot(), UserManager.boot(),
MapManager.boot(), MapManager.boot(),