diff options
author | HampusM <hampus@hampusmat.com> | 2021-08-18 17:29:55 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-08-18 17:29:55 +0200 |
commit | d1a1b7dc947063aef5f8375a6a1e03246b272c84 (patch) | |
tree | f5cb9bd6d4b5463d9d022026ac6fea87cb6ebe02 /packages/server/src/app.ts | |
parent | 6ed078de30a7bf35deace728857d1d293d59eb15 (diff) |
Implemented caching for certain API endpoints, Added documentation & made backend-fixes
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 |