start a web server
This commit is contained in:
37
src/Web.ts
37
src/Web.ts
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user