2022-02-10 21:42:53 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import http from 'http';
|
|
|
|
|
2022-02-12 16:27:50 +00:00
|
|
|
import log from '../../../io/NamespacedLog.mjs'; const l = log("http");
|
|
|
|
import make_router from './routes.mjs';
|
|
|
|
|
2022-02-10 21:42:53 +00:00
|
|
|
class HttpSubsystem {
|
2022-02-12 16:27:50 +00:00
|
|
|
constructor(sys) {
|
|
|
|
this.sys = sys;
|
|
|
|
this.router = make_router(this.sys);
|
2022-02-10 21:42:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 16:27:50 +00:00
|
|
|
init(port, address = `::1`) {
|
|
|
|
this.http = http.createServer(async (req, res) => {
|
|
|
|
await this.router.handle(req, res);
|
2022-02-10 21:42:53 +00:00
|
|
|
});
|
2022-02-12 16:27:50 +00:00
|
|
|
|
|
|
|
const address_pretty = address.indexOf(`:`) > -1 ? `[${address}]` : address;
|
|
|
|
this.http.listen(port, address, () => l.log(`Listening on http://${address_pretty}:${port}`));
|
2022-02-10 21:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default HttpSubsystem;
|