Index

joshstock.in / 3696e54

Source for serving and static templating/compiling of https://joshstock.in.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
6004 Jan 2020 23:413696e54Update gitpager siteJosh Stockin1510N

Blob @ joshstock.in / gitpager / index.js

application/javascript2548 bytesdownload raw
1const fs = require("fs");
2const path = require("path");
3const express = require("express");
4const app = express();
5const port = 8080;
6const gitcontrol = require(path.resolve(__dirname, "gitcontrol"))
7
8const allowed_repos = {"lognestmonster": "dev", "joshstock.in": "master", "auto-plow": "dev"};
9const repo_folder = process.env.NODE_ENV == "production" ? "/home/git/" : "/home/josh/Desktop/";
10
11commitFile = fs.readFileSync(path.resolve(__dirname, "commit.html")).toString();
12function 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
32app.use(express.static(path.resolve(__dirname, "static")))
33
34indexFile = fs.readFileSync(path.resolve(__dirname, "index.html")).toString();
35app.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
43app.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
52app.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
60app.listen(port);
61