From 86395bd219b770133cb80d4bda4efc9155a4eef8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 7 Jun 2021 20:54:26 +0200 Subject: Refactored the backend. It's written in typescript now. --- packages/server/src/api/v1.js | 136 ------------------------------------------ 1 file changed, 136 deletions(-) delete mode 100644 packages/server/src/api/v1.js (limited to 'packages/server/src/api/v1.js') diff --git a/packages/server/src/api/v1.js b/packages/server/src/api/v1.js deleted file mode 100644 index 25a8019..0000000 --- a/packages/server/src/api/v1.js +++ /dev/null @@ -1,136 +0,0 @@ -const git = require("./git"); -const util = require("./util"); - -module.exports = function (fastify, opts, done) -{ - fastify.route({ - method: "GET", - path: "/info", - handler: (req, reply) => - { - reply.send({ data: opts.config.settings }); - } - }); - fastify.route({ - method: "GET", - path: "/repos", - handler: async (req, reply) => - { - let repos = await git.getRepos(opts.config.settings.base_dir); - - if(repos["error"]) { - reply.code(500).send({ error: "Internal server error!" }); - return; - } - - reply.send({ data: repos }); - } - }); - - fastify.route({ - method: "GET", - path: "/repos/:repo", - handler: async (req, reply) => - { - const repo_verification = await util.verifyRepoName(req.params.repo, opts.config.settings.base_dir); - if(repo_verification !== true) { - if(repo_verification === "ERR_REPO_REGEX") { - reply.code(400).send({ error: "Unacceptable git repository name!" }); - } - else if(repo_verification === "ERR_REPO_NOT_FOUND") { - reply.code(404).send({ error: "Git repository not found!" }); - } - } - - const repo = `${req.params.repo}.git`; - const desc = await git.getRepoFile(opts.config.settings.base_dir, repo, "description"); - - reply.send({ data: { name: req.params.repo, description: desc } }); - } - }); - - fastify.register((fastify_repo, opts_repo, done_repo) => - { - fastify_repo.addHook("onRequest", async (req, reply) => - { - const repo_verification = await util.verifyRepoName(req.params.repo, opts.config.settings.base_dir); - if(repo_verification !== true) { - if(repo_verification === "ERR_REPO_REGEX") { - reply.code(400).send({ error: "Unacceptable git repository name!" }); - } - else if(repo_verification === "ERR_REPO_NOT_FOUND") { - reply.code(404).send({ error: "Git repository not found!" }); - } - } - }); - - fastify_repo.route({ - method: "GET", - path: "/log", - handler: async (req, reply) => - { - const log = await git.getLog(opts.config.settings.base_dir, req.params.repo + ".git"); - - if(log["error"]) { - if(typeof log["error"] === "string") { - reply.code(500).send({ error: log["error"] }); - } - - switch(log["error"]) { - case 404: - reply.code(404).send({ error: "Git repository not found!" }); - } - - return; - } - reply.send({ data: log }); - } - }); - - fastify_repo.route({ - method: "GET", - path: "/log/:commit", - handler: async (req, reply) => - { - const commit_verification = await util.verifyCommitID(opts.config.settings.base_dir, req.params.repo + ".git", req.params.commit); - if(!commit_verification !== true) { - if(commit_verification === "ERR_COMMIT_REGEX") { - reply.code(400).send({ error: "Unacceptable commit id!" }); - } - else if(commit_verification === "ERR_COMMIT_NOT_FOUND") { - reply.code(404).send({ error: "Commit not found!" }); - } - } - - const commit = await git.getCommit(opts.config.settings.base_dir, req.params.repo, req.params.commit); - - reply.send({ data: commit }); - } - }); - - fastify_repo.route({ - method: "GET", - path: "/tree", - handler: async (req, reply) => - { - const tree_path = (req.query.length !== 0 && req.query.path) ? req.query.path : null; - - const tree = await git.getTree(opts.config.settings.base_dir, req.params.repo, tree_path); - - if(tree.error) { - if(tree.error === 404) { - reply.code(404).send({ error: "Path not found" }); - } - else { - reply.code(500).send({ error: "Internal server error" }); - } - } - reply.send({ data: tree }); - } - }); - - done_repo(); - }, { prefix: "/repos/:repo" }); - - done(); -}; \ No newline at end of file -- cgit v1.2.3-18-g5258