74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import Router from "@koa/router";
|
|
import dgram from "dgram";
|
|
import kill from "kill-port";
|
|
import { default as Koa } from "koa";
|
|
import installQS from "koa-qs";
|
|
import { Middlewares } from "sealious";
|
|
import TheApp from "./app.js";
|
|
import { PORT, SEALIOUS_KILLSWITCH_PORT, SEALIOUS_SANITY } from "./config.js";
|
|
import { mainRouter } from "./routes/index.js";
|
|
import { waitForMeilisearch } from "./services/meilisearch.js";
|
|
import { TheFileManager } from "./file-manager.js";
|
|
import { imageRouter } from "./image-router.js";
|
|
|
|
export const the_app = new TheApp({
|
|
fileManager: TheFileManager,
|
|
imageRouter: imageRouter,
|
|
});
|
|
|
|
void (async function () {
|
|
await kill(PORT);
|
|
await kill(PORT);
|
|
|
|
try {
|
|
await waitForMeilisearch(10);
|
|
} catch (e) {
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
if ((e as Error).message === "timeout") {
|
|
console.log("Meilisearch must be running before the app starts!"); // eslint-disable-line no-console
|
|
process.exit(1);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
try {
|
|
const koa_app = new Koa();
|
|
const router = new Router();
|
|
installQS(koa_app);
|
|
koa_app.use(Middlewares.handleError());
|
|
koa_app.context.$app = the_app;
|
|
the_app.on("started", async () => {
|
|
mainRouter(the_app, koa_app, router);
|
|
});
|
|
|
|
if (SEALIOUS_KILLSWITCH_PORT) {
|
|
const server = dgram.createSocket("udp4");
|
|
server.on("message", () => {
|
|
process.exit(0);
|
|
});
|
|
server.bind(SEALIOUS_KILLSWITCH_PORT);
|
|
}
|
|
|
|
await the_app.start();
|
|
|
|
koa_app.listen(PORT, "0.0.0.0");
|
|
the_app.Logger.info(
|
|
"STARTED",
|
|
`App running. URL set in manifest: ${the_app.manifest.base_url}`
|
|
);
|
|
|
|
if (SEALIOUS_SANITY) {
|
|
console.error("Exiting with error code 0");
|
|
process.exit(0);
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
if (SEALIOUS_SANITY) {
|
|
console.error("EXITING WITH STATUS 1");
|
|
}
|
|
// we need to always exit with an error status so the systemd unit knows to restart
|
|
process.exit(1);
|
|
}
|
|
})();
|