~ knotless Search/
↑↓ move↵ open⇧↵ all results

socialweb.computer/socialweb-computer-kcp

socialweb.computer: a compute market on atproto (kcp)

1 collaborator
0
0
55 lines · 1.5 KB Raw History
import { Command } from "@publicdomainrelay/cli-args-env";
import {
  createStaticFilesApp,
  type StaticFileEvent,
} from "@publicdomainrelay/hono-factory-static-files-fs";
import { createLogger, getMinLogLevelFromEnv, createStructuredLogger } from "@publicdomainrelay/logger";
import { createServe } from "@publicdomainrelay/serve";
import { EventBus } from "@publicdomainrelay/event-bus";
import cliArgsEnv from "./cli-args-env.json" with { type: "json" };

let runtimeConfig = null;
try {
  const mod = await import("./config.json", { with: { type: "json" } });
  runtimeConfig = mod.default;
} catch {
  /* optional */
}

const { options } = await new Command(
  "CONFIG_PATH_HONO_HTTP_STATIC",
  cliArgsEnv,
  runtimeConfig,
).resolve();

const servePath = options.servePath as string;
const port = options.port as number;
const logger = createStructuredLogger("http-static", getMinLogLevelFromEnv());
const bus = new EventBus<StaticFileEvent>();

bus.subscribe((event) => {
  if (event.type === "file-not-found") {
    logger.warn(`404 ${event.path}`);
  }
});

const app = createStaticFilesApp(servePath, logger, bus);

const serve = createServe({
  logger,
  tcp: {
    port,
    certFile: options.tlsCertFile as string | undefined,
    keyFile: options.tlsKeyFile as string | undefined,
  },
  portFile: options.portFile as string | undefined,
});
serve.app.route("/", app as never);

function shutdown() {
  serve.shutdown();
  Deno.exit();
}
Deno.addSignalListener("SIGINT", shutdown);
Deno.addSignalListener("SIGTERM", shutdown);
await serve.beginServe();