// Import the framework and instantiate it
import Fastify from 'fastify'
import fastifyStatic from '@fastify/static'
import { routeList } from "./routelist.ts";
import { route } from "./util/routewrap.ts";

console.log(process.env["DATABASE_URL"]);

const server = Fastify({
	logger: true
});

server.register(fastifyStatic, {
	root: new URL('public', import.meta.url).toString().slice("file://".length),
	prefix: '/',
});

routeList.forEach(firRoute => {
	server.route(route(firRoute));
})

// Run the server!
try {
	// Note: host needs to be 0.0.0.0 rather than omitted or localhost, otherwise
	// it always returns an empty reply when used inside docker...
	// See: https://github.com/fastify/fastify/issues/935
	await server.listen({ port: parseInt(process.env["PORT"] ?? "3000"), host: "0.0.0.0" })
} catch (err) {
	server.log.error(err)
	process.exit(1)
}