picobook/src/server/index.ts
2024-03-31 13:55:04 -07:00

38 lines
1.1 KiB
TypeScript

// 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";
import { git } from './util/git.ts';
import path from "path";
import {fileURLToPath} from 'url';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
await git.clone({
from: "https://github.com/thisismypassport/shrinko8",
to: path.resolve(__dirname, "shrinko8"),
});
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)
}