From a13786d6cc185822f5940582efde2349ef729145 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 24 Jun 2021 22:50:38 +0200 Subject: Refactored the backend yet again --- packages/server/src/app.ts | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'packages/server/src/app.ts') diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 717f106..7bc66b2 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -1,6 +1,7 @@ import { readFileSync, readdirSync } from "fs"; -import { GitAPI } from "./api/git"; +import { Repository } from "./git/repository"; import { Route } from "./fastify_types"; +import { Tag } from "./git/tag"; import api from "./api/v1"; import { exit } from "process"; import { fastify as fastifyFactory } from "fastify"; @@ -62,7 +63,11 @@ if(settings.production) { } const fastify = fastifyFactory(); -const git = new GitAPI(settings.base_dir); + +fastify.setErrorHandler((err, req, reply) => { + console.log(err); + reply.code(500).send("Internal server error!"); +}); fastify.setNotFoundHandler({}, function(req, reply) { reply.code(404).send("Page not found!"); @@ -112,7 +117,8 @@ fastify.route({ return; } - git.connectToGitHTTPBackend(req, reply); + const repository = await Repository.open(settings.base_dir, req.params.repo); + repository.HTTPconnect(req, reply); } }); @@ -127,7 +133,8 @@ fastify.route({ reply.code(repo_verification.code).send(repo_verification.message); } - git.connectToGitHTTPBackend(req, reply); + const repository = await Repository.open(settings.base_dir, req.params.repo); + repository.HTTPconnect(req, reply); } }); @@ -143,8 +150,16 @@ fastify.route({ fastify.route({ method: "GET", url: "/:repo([a-zA-Z0-9\\.\\-_]+)/refs/tags/:tag", - handler: (req, reply) => { - git.downloadTagArchive(req.params.repo, req.params.tag, reply); + handler: async(req, reply) => { + const repository = await Repository.open(settings.base_dir, req.params.repo); + const tag = await Tag.lookup(repository, req.params.tag); + + if(!tag) { + reply.code(404).send("Tag not found!"); + return; + } + + tag.downloadTarball(reply); } }); -- cgit v1.2.3-18-g5258