import express from "express"; import yargs from "yargs"; import { Handler } from "./handler.js"; import { Config } from "./config.js"; class Server { constructor({ handler }) { this.handler = handler; this.app = express(); this.app.get("/{*any}", async (req, res) => { const host = req.hostname; const path = req.path; const { status, content, contentType } = await this.handler.handleRequest( { host, path, } ); res.status(status).set("Content-Type", contentType).send(content); }); } async start({ port }) { this.app.listen(port, () => { console.log(`Server is running on port ${port}`); }); this.app.on("error", (error) => { console.error("Server error:", error); }); } } async function main() { const args = yargs(process.argv.slice(2)).parse(); const port = args.port ?? args.p ?? 3000; const configFilepath = args.config || "config.json"; const config = await Config.fromFile(configFilepath); const handler = await Handler.fromConfig(config); const server = new Server({ handler, }); await server.start({ port }); } main();