From 43faf0bfd5544f9338e1442ab5b1fcb3210867ac Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 22 Jun 2021 13:31:43 +0200 Subject: Refactored the backend yet again & got eslint too work with typescript --- packages/server/src/api/v1/repo/index.ts | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 packages/server/src/api/v1/repo/index.ts (limited to 'packages/server/src/api/v1/repo/index.ts') diff --git a/packages/server/src/api/v1/repo/index.ts b/packages/server/src/api/v1/repo/index.ts new file mode 100644 index 0000000..86e5d10 --- /dev/null +++ b/packages/server/src/api/v1/repo/index.ts @@ -0,0 +1,48 @@ +import { FastifyInstance, FastifyPluginOptions, FastifyRequest } from "fastify"; +import { GitAPI } from "../../git"; +import { Route } from "../../../fastify_types"; +import branches from "./branches"; +import log from "./log"; +import { verifyRepoName } from "../../util"; + +export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, done: (err?: Error) => void): void { + const git: GitAPI = opts.config.git; + + fastify.addHook("onRequest", async(req: FastifyRequest, reply) => { + const repo_verification = await verifyRepoName(opts.config.settings.base_dir, req.params.repo); + if(repo_verification.success === false && repo_verification.code) { + reply.code(repo_verification.code).send({ error: repo_verification.message }); + } + }); + + fastify.register(log, { config: { git: git } }); + fastify.register(branches, { config: { git: git } }); + + fastify.route({ + method: "GET", + url: "/tree", + handler: async(req, reply) => { + const tree_path = (Object.keys(req.query).length !== 0 && req.query.path) ? req.query.path : null; + + const tree = await git.getTree(req.params.repo, tree_path); + + if(!tree) { + reply.code(404).send({ error: "Path not found" }); + return; + } + + reply.send({ data: tree }); + } + }); + + fastify.route({ + method: "GET", + url: "/tags", + handler: async(req, reply) => { + const refs = await git.getTags(req.params.repo); + reply.send({ data: refs }); + } + }); + + done(); +} \ No newline at end of file -- cgit v1.2.3-18-g5258