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