Index

joshstock.in / 3d81656

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
6305 Jan 2020 00:023b457d8Use HEAD instead of @{0} in gitpager commandJosh Stockin111N

Blob @ joshstock.in / gitpager / index.js

application/javascript2619 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"};
9
10function 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
18commitFile = fs.readFileSync(path.resolve(__dirname, "commit.html")).toString();
19function 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
39app.use(express.static(path.resolve(__dirname, "static")))
40
41indexFile = fs.readFileSync(path.resolve(__dirname, "index.html")).toString();
42app.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)}`, "HEAD"), false, true));
46 }
47 res.send(response);
48});
49
50app.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
59app.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
67app.listen(port);
68