2022-03-21 14:21:34 +00:00
|
|
|
import { Express } from "express";
|
|
|
|
import { SilverBulletHooks } from "../common/manifest";
|
2022-03-29 09:21:32 +00:00
|
|
|
import { EndpointHook } from "../plugos/hooks/endpoint";
|
2022-03-21 14:21:34 +00:00
|
|
|
import { readFile } from "fs/promises";
|
2022-03-27 09:31:12 +00:00
|
|
|
import { System } from "../plugos/system";
|
2022-03-21 14:21:34 +00:00
|
|
|
|
|
|
|
export class ExpressServer {
|
|
|
|
app: Express;
|
|
|
|
system: System<SilverBulletHooks>;
|
|
|
|
private rootPath: string;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
app: Express,
|
|
|
|
rootPath: string,
|
|
|
|
distDir: string,
|
|
|
|
system: System<SilverBulletHooks>
|
|
|
|
) {
|
|
|
|
this.app = app;
|
|
|
|
this.rootPath = rootPath;
|
|
|
|
this.system = system;
|
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
system.addHook(new EndpointHook(app, "/_"));
|
2022-03-21 14:21:34 +00:00
|
|
|
|
|
|
|
// Fallback, serve index.html
|
|
|
|
let cachedIndex: string | undefined = undefined;
|
|
|
|
app.get("/*", async (req, res) => {
|
|
|
|
if (!cachedIndex) {
|
|
|
|
cachedIndex = await readFile(`${distDir}/index.html`, "utf8");
|
|
|
|
}
|
|
|
|
res.status(200).header("Content-Type", "text/html").send(cachedIndex);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {}
|
|
|
|
}
|