import fastifyCookie from "@fastify/cookie";
import fastify from "fastify";
import { Label, logger } from "./logger.js";
import { baseApiPlugin } from "./routes/baseApi.js";
import { devLoginPlugin } from "./routes/devLogin.js";
import { indexerApiPlugin } from "./routes/indexerApi.js";
import { staticFrontendPlugin } from "./routes/staticFrontendPlugin.js";
import { appTrpcPlugin } from "./trpc/fastifyAdapter.js";
async function apiPlugin(app) {
    await app.register(appTrpcPlugin, { prefix: "/trpc" });
    await app.register(indexerApiPlugin, { prefix: "/indexer/v1" });
    await app.register(baseApiPlugin);
}
async function rootPlugin(app, { basePath }) {
    await app.register(fastifyCookie);
    await app.register(devLoginPlugin, { basePath });
    await app.register(apiPlugin, { prefix: "/api" });
    await app.register(staticFrontendPlugin, { basePath });
}
async function createServer(basePath) {
    const app = fastify({ logger: false });
    if (basePath) {
        await app.register(rootPlugin, { prefix: basePath, basePath });
        app.get("*", async (request, reply) => {
            return reply.redirect(`${basePath}/`);
        });
    }
    else {
        await app.register(rootPlugin, { basePath: "" });
    }
    return app;
}
/**
 * Listens (daemon) on configured port for http API calls
 */
export async function serve(port = 2468, host = "0.0.0.0", basePath = "", onListening) {
    if (!port)
        return;
    const server = await createServer(basePath);
    return new Promise((resolve) => {
        server.listen({ port, host }, (err) => {
            if (err) {
                logger.error({
                    label: Label.SERVER,
                    message: `Failed to start server: ${err.message}`,
                });
                process.exit(1);
            }
            logger.info({
                label: Label.SERVER,
                message: `Server is running on port ${port}, ^C to stop.`,
            });
            onListening?.();
        });
        function stop() {
            server.close(() => {
                logger.info({
                    label: Label.SERVER,
                    message: "Server stopped",
                });
                resolve();
            });
        }
        process.on("SIGINT", stop);
        process.on("SIGTERM", stop);
    });
}
//# sourceMappingURL=server.js.map