start a web server

This commit is contained in:
2025-01-10 01:52:42 -05:00
parent b8baa5cf4f
commit 0abf129fcc
10 changed files with 882 additions and 10 deletions

View File

@@ -1,6 +1,41 @@
import * as http from "http";
import type { Express } from "express";
import express, { application } from 'express';
interface WebConfig {
port?: number;
}
/**
* I still hate typescript.
*/
function notStupidParseInt(v: string | undefined): number {
return v === undefined ? NaN : parseInt(v);
}
class Web {
constructor(options = {}) {
private _webserver: http.Server | null = null;
constructor(options: WebConfig = {}) {
const app: Express = express();
const port: number = notStupidParseInt(process.env.PORT) || options['port'] as number || 8080;
app.set('trust proxy', 1);
app.set('view engine', 'ejs');
app.set('view options', { outputFunctionName: 'echo' });
app.use('/assets', express.static('assets'));
app.get('/healthcheck', (_, res) => {
res.send('Healthy');
});
this._webserver = app.listen(port, () => console.log(`archy-build-thing is running on port ${port}`));
}
close = () => {
if (this._webserver) {
this._webserver.close();
}
}
}

View File

@@ -5,3 +5,5 @@ import Web from './Web.ts';
const config = JSON.parse(await fs.promises.readFile(process.env.config || path.join('config', 'config.json'), 'utf-8'));
const web = new Web(config);
process.on('SIGTERM', web.close);