1 | const fs = require("fs"); |
2 | const path = require("path"); |
3 | const express = require("express"); |
4 | const app = express(); |
5 | const port = 8080; |
6 | const gitcontrol = require(path.resolve(__dirname, "gitcontrol")) |
7 |
|
8 | const allowed_repos = {"lognestmonster": "dev", "joshstock.in": "master", "auto-plow": "dev"}; |
9 |
|
10 | function repo_path(repo) { |
11 | if (process.env.NODE_ENV == "production") { |
12 | return "/home/git/" + repo + ".git/"; |
13 | } else { |
14 | return "/home/josh/Desktop/" + repo + "/.git"; |
15 | } |
16 | } |
17 |
|
18 | commitFile = fs.readFileSync(path.resolve(__dirname, "commit.html")).toString(); |
19 | function format_commit(repo, commit, isgithub=false, islatest=false) { |
20 | commitString = commitFile.replace("$repo", repo).replace("$hash", commit.hash) |
21 | .replace("$subject", commit.subject).replace("$author", commit.author).replace("$author_email", commit.author_email); |
22 | if (isgithub) { |
23 | commitString = commitString.replace("$shorthash", "<a href=\"https://github.com/JoshuaS3/" + repo + "/commit/" + commit.hash + "\">" + commit.hash.substring(0,7) + " (" + allowed_repos[repo] + "/" + commit.number + ")</a>"); |
24 | } else { |
25 | commitString = commitString.replace("$shorthash", commit.hash.substring(0,7) + " (" + allowed_repos[repo] + "/" + commit.number + ")"); |
26 | } |
27 | commitTime = new Date(commit.date); |
28 | time = commitTime.toDateString().split(" ").slice(1).join(" ") + ", " + commitTime.toLocaleTimeString().split(" ")[0]; |
29 | if (islatest) time += " (latest commit)"; |
30 | commitString = commitString.replace("$date", time); |
31 | if (commit.body === undefined) { |
32 | commitString = commitString.replace("$body", commit.body); |
33 | } else { |
34 | commitString = commitString.replace("$body", ""); |
35 | } |
36 | return commitString; |
37 | } |
38 |
|
39 | app.use(express.static(path.resolve(__dirname, "static"))) |
40 |
|
41 | indexFile = fs.readFileSync(path.resolve(__dirname, "index.html")).toString(); |
42 | app.get("/", function(req, res) { |
43 | response = indexFile; |
44 | for (repo in allowed_repos) { |
45 | response = response.replace("$commit_" + repo, format_commit(repo, gitcontrol.get_commit(`${repo_path(repo)}`, "@{0}"), false, true)); |
46 | } |
47 | res.send(response); |
48 | }); |
49 |
|
50 | app.get("/:repo/:page(\\d+)", function(req, res) { |
51 | if (allowed_repos[req.params.repo] == null) { |
52 | res.status(404).send() |
53 | return; |
54 | } |
55 | req.params.page = parseInt(req.params.page) || 1; |
56 | res.send(gitcontrol.list_commits(`${repo_path(req.params.repo)}`, allowed_repos[req.params.repo], req.params.page-1)); |
57 | }); |
58 |
|
59 | app.get("/:repo/:commit([a-f0-9]{40})", function(req, res) { |
60 | if (allowed_repos[req.params.repo] == null) { |
61 | res.status(404).send() |
62 | return; |
63 | } |
64 | res.send(gitcontrol.get_commit(`${repo_path(req.params.repo)}`, req.params.commit)); |
65 | }); |
66 |
|
67 | app.listen(port); |
68 |
|