diff options
Diffstat (limited to 'packages/server/src/app.ts')
-rw-r--r-- | packages/server/src/app.ts | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 42d096a..33c5a5a 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -4,10 +4,11 @@ import fastifyStatic from "fastify-static"; import { Settings } from "./types"; import repo from "./routes/repo"; import { join } from "path"; -import { readdirSync } from "fs"; +import { readdir } from "fs/promises"; import { exit } from "process"; +import { ServerCache } from "./cache"; -export default function buildApp(settings: Settings): FastifyInstance { +export default async function buildApp(settings: Settings, cache: ServerCache | null): Promise<FastifyInstance> { const fastify = fastifyFactory(); fastify.setErrorHandler((err, req, reply) => { @@ -20,20 +21,17 @@ export default function buildApp(settings: Settings): FastifyInstance { reply.code(500).send("Internal server error!"); }); - fastify.setNotFoundHandler({}, function(req, reply) { + fastify.setNotFoundHandler({}, (req, reply) => { reply.code(404).send("Page not found!"); }); if(!settings.dev) { const dist_dir = join(__dirname, "/../../client/dist"); - try { - readdirSync(dist_dir); - } - catch { + await readdir(dist_dir).catch(() => { console.error("Error: Client dist directory doesn't exist!"); exit(1); - } + }); fastify.register(fastifyStatic, { root: dist_dir }); @@ -49,8 +47,8 @@ export default function buildApp(settings: Settings): FastifyInstance { fastify.addContentTypeParser("application/x-git-upload-pack-request", (req, payload, done) => done(null, payload)); fastify.addContentTypeParser("application/x-git-receive-pack-request", (req, payload, done) => done(null, payload)); - fastify.register(api, { prefix: "/api/v1", config: { settings: settings } }); - fastify.register(repo, { prefix: "/:repo([a-zA-Z0-9\\.\\-_]+)", config: { settings: settings } }); + fastify.register(api, { prefix: "/api/v1", config: { settings: settings, cache: cache } }); + fastify.register(repo, { prefix: "/:repo([a-zA-Z0-9\\.\\-_]+)", config: { settings: settings, cache: cache } }); return fastify; }
\ No newline at end of file |