import { createServer } from "node:http";
import { bridgeReady, handleBridgeRequest } from "./application.js";
import { readConfiguration } from "./config.js";
const configuration = readConfiguration();
const server = createServer((request, response) => { void handleBridgeRequest(request, response); });
void bridgeReady
    .then(() => {
    server.listen(configuration.port, configuration.host, () => {
        process.stdout.write(`BGM WhatsApp Web bridge listening on ${configuration.host}:${configuration.port}\n`);
    });
})
    .catch((error) => {
    process.stderr.write(`BGM WhatsApp Web bridge failed to start: ${error instanceof Error ? error.message : "Unknown error"}\n`);
    process.exitCode = 1;
});
async function shutdown() {
    server.close(() => process.exit(0));
    setTimeout(() => process.exit(1), 10_000).unref();
}
process.on("SIGINT", () => { void shutdown(); });
process.on("SIGTERM", () => { void shutdown(); });
