aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/git.js23
-rw-r--r--api/v1.js67
2 files changed, 59 insertions, 31 deletions
diff --git a/api/git.js b/api/git.js
index 59f7371..72ca5a1 100644
--- a/api/git.js
+++ b/api/git.js
@@ -30,9 +30,9 @@ function execGit(path, action , format, args = "")
});
}
-async function getLog(path)
+async function getLog(base_dir, path)
{
- let log = await execGit(path, "log", log_format);
+ let log = await execGit(`${base_dir}/${path}`, "log", log_format);
if(!log["error"]) {
log["data"] = log["data"].split('\n');
@@ -118,5 +118,22 @@ function getBasicRepoInfo(base_dir, repo_dirs)
});
}
+function getRepos(base_dir)
+{
+ return new Promise((resolve) =>
+ {
+ fs.readdir(base_dir, async (err, content) =>
+ {
+ if(err) {
+ resolve({ "error": err });
+ return;
+ }
+ resolve({ "data": content });
+ });
+ });
+}
+
module.exports.getLog = getLog;
-module.exports.getBasicRepoInfo = getBasicRepoInfo; \ No newline at end of file
+module.exports.getBasicRepoInfo = getBasicRepoInfo;
+module.exports.getRepos = getRepos;
+module.exports.getRepoFile = getRepoFile; \ No newline at end of file
diff --git a/api/v1.js b/api/v1.js
index 75509ad..7358d3b 100644
--- a/api/v1.js
+++ b/api/v1.js
@@ -1,52 +1,63 @@
const express = require("express");
const git = require("./git");
const sanitization = require("./sanitization");
-const fs = require('fs');
const router = express.Router();
-const base_dir="/home/hampus/Projects/"
+
+router.get("/info", function(req, res)
+{
+ res.json({ "data": req.settings });
+ return;
+});
router.get("/repos", async function(req, res)
{
- fs.readdir(base_dir, async (err, repo_dirs) =>
- {
- if(err) {
- throw err;
- }
- repo_dirs = repo_dirs.filter(repo => repo.endsWith(".git"));
+ let repo_dirs = await git.getRepos(req.settings["base_dir"]);
- console.log("Repo dirs: " + repo_dirs);
+ if(repo_dirs["error"]) {
+ res.status(500).send("Internal server error!");
+ return;
+ }
- const repos = await git.getBasicRepoInfo(base_dir, repo_dirs);
+ repo_dirs = repo_dirs["data"].filter(repo => repo.endsWith(".git"));
+ const repos = await git.getBasicRepoInfo(req.settings["base_dir"], repo_dirs);
+ res.json({ "data": repos });
+});
- console.log("I v1.js\n" + JSON.stringify(repos) + "\n");
+router.get("/repos/:repo", async function(req, res)
+{
+ if(!sanitization.sanitizeRepoName(req.params.repo)) {
+ res.status(400).json({ "error": "Unacceptable git repository name!" });
+ return;
+ }
+ const repo = `${req.params.repo}.git`;
+ const desc = await git.getRepoFile(req.settings["base_dir"], repo, "description");
- res.json({ "data": repos });
- });
+ res.json({ "data": { "name": req.params.repo, "description": desc } });
});
router.get("/repos/:repo/log", async function(req, res)
{
- if(sanitization.sanitizeRepoName(req.params.repo)) {
- const repo = `${req.params.repo}.git`;
- const log = await git.getLog(`${base_dir}/${repo}`);
+ if(!sanitization.sanitizeRepoName(req.params.repo)) {
+ res.status(400).json({ "error": "Unacceptable git repository name!" });
+ return;
+ }
+ const repo = `${req.params.repo}.git`;
+ const log = await git.getLog(req.settings["base_dir"], repo);
- if(log["error"]) {
- if(typeof log["error"] === "string") {
- res.status(500).json({ "error": log["error"] });
- return;
- }
- switch(log["error"]) {
- case 404:
- res.status(404).json({ "error": "Git repository doesn't exist!" });
- return;
- }
+ if(log["error"]) {
+ if(typeof log["error"] === "string") {
+ res.status(500).json({ "error": log["error"] });
return;
}
- res.json(log);
+ switch(log["error"]) {
+ case 404:
+ res.status(404).json({ "error": "Git repository doesn't exist!" });
+ return;
+ }
return;
}
- res.status(400).json({ "error": "Unacceptable git repository name!" });
+ res.json(log);
});
module.exports = router; \ No newline at end of file