From d1a1b7dc947063aef5f8375a6a1e03246b272c84 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 18 Aug 2021 17:29:55 +0200 Subject: Implemented caching for certain API endpoints, Added documentation & made backend-fixes --- packages/server/src/app.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'packages/server/src/app.ts') 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 { 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 -- cgit v1.2.3-18-g5258