import { createServer } from "node:http";
import { createEdgeTtsMiddleware } from "./edge-tts-service";

const host = "127.0.0.1";
const port = 8790;
const handleEdgeTts = createEdgeTtsMiddleware({ trustProxy: true });

const server = createServer((request, response) => {
  void handleEdgeTts(request, response, () => {
    response.statusCode = 404;
    response.end("Not Found");
  });
});

server.listen(port, host, () => {
  console.log(`[Agent TTS] Edge TTS adapter listening on http://${host}:${port}`);
});

function closeServer() {
  server.close(() => process.exit(0));
}

process.on("SIGINT", closeServer);
process.on("SIGTERM", closeServer);
