import { loadConfig } from "./config.js";
import { buildServer } from "./server.js";

const config = loadConfig();
const app = await buildServer(config);

const shutdown = async (signal: string) => {
  app.log.info(`${signal} mottagen – stänger ner API:t.`);
  await app.close();
  process.exit(0);
};
process.on("SIGTERM", () => void shutdown("SIGTERM"));
process.on("SIGINT", () => void shutdown("SIGINT"));

try {
  await app.listen({ port: config.API_PORT, host: "0.0.0.0" });
  app.log.info(`API igång på :${config.API_PORT} (${config.NODE_ENV}, AAMOS=${config.AAMOS_MODE})`);
} catch (err) {
  app.log.error(err);
  process.exit(1);
}
