aboutsummaryrefslogtreecommitdiff
path: root/src/api/v1.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/v1.js')
-rw-r--r--src/api/v1.js163
1 files changed, 102 insertions, 61 deletions
diff --git a/src/api/v1.js b/src/api/v1.js
index 7920afc..3a6f7ea 100644
--- a/src/api/v1.js
+++ b/src/api/v1.js
@@ -1,74 +1,115 @@
-const express = require("express");
const git = require("./git");
-const sanitization = require("./sanitization");
+const util = require("./util");
-const router = express.Router();
-
-router.get("/info", function(req, res)
-{
- res.json({ "data": req.settings });
- return;
-});
-
-router.get("/repos", async function(req, res)
+module.exports = function (fastify, opts, done)
{
- let repos = await git.getRepos(req.settings["base_dir"]);
-
- if(repos["error"]) {
- res.status(500).send("Internal server error!");
- return;
- }
-
- res.json({ "data": repos });
-});
+ 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);
-router.use("/repos/:repo", async function(req, res, next)
-{
- if(!sanitization.sanitizeRepoName(req.params.repo)) {
- res.status(400).json({ "error": "Unacceptable git repository name!" });
- return;
- }
- next();
-});
+ if(repos["error"]) {
+ reply.code(500).send({ error: "Internal server error!" });
+ return;
+ }
-router.get("/repos/:repo", async function(req, res)
-{
- const repo = `${req.params.repo}.git`;
- const desc = await git.getRepoFile(req.settings["base_dir"], repo, "description");
+ reply.send({ data: repos });
+ }
+ });
- res.json({ "data": { "name": req.params.repo, "description": desc } });
-});
+ 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!" });
+ }
+ }
-router.get("/repos/:repo/log", async function(req, res)
-{
- const repo = `${req.params.repo}.git`;
- const log = await git.getLog(req.settings["base_dir"], repo);
+ const repo = `${req.params.repo}.git`;
+ const desc = await git.getRepoFile(opts.config.settings.base_dir, repo, "description");
- if(log["error"]) {
- if(typeof log["error"] === "string") {
- res.status(500).json({ "error": log["error"] });
- return;
+ reply.send({ data: { name: req.params.repo, description: desc } });
}
- switch(log["error"]) {
- case 404:
- res.status(404).json({ "error": "Git repository doesn't exist!" });
- return;
- }
- return;
- }
- res.json({ data: log });
-});
+ });
-router.get("/repos/:repo/log/:commit", async function(req, res)
-{
- if(!sanitization.sanitizeCommitID(req.params.commit)) {
- res.status(400).json({ "error": "Unacceptable commit id!" });
- return;
- }
+ 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 });
+ }
+ });
- const commit = await git.getCommit(req.settings["base_dir"], req.params.repo, req.params.commit);
+ 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 });
+ }
+ });
- res.json({ data: commit });
-});
+ done_repo();
+ }, { prefix: "/repos/:repo" });
-module.exports = router; \ No newline at end of file
+ done();
+} \ No newline at end of file