aboutsummaryrefslogtreecommitdiff
path: root/src/index.ts
blob: 393c5ecde01fa4d69eeaa20b532f56b7ca367361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { createAdaptorServer, serve } from "@hono/node-server";
import { serveStatic } from "@hono/node-server/serve-static";
import { Eta } from "eta";
import { Hono } from "hono";
import * as path from "node:path";
import * as url from "node:url";
import { getGitHubBlob, getGitHubRepo, getGitHubTree } from "./scraper.js";

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

const app = new Hono();
const eta = new Eta({ views: path.join(__dirname, "..", "views") });

app.use("/static/*", serveStatic({ root: path.join(__dirname, "..") }));

app.get("/:owner/:repo", async (c) => {
  const { owner, repo } = c.req.param();
  const data = await getGitHubRepo(owner, repo);
  return c.html(await eta.renderAsync("repo.eta", data));
});

app.get("/:owner/:repo/tree/:branch/:path{.*}?", async (c) => {
  const { owner, repo, branch, path = "" } = c.req.param();
  const data = await getGitHubTree(owner, repo, branch, path);
  return c.html(await eta.renderAsync("tree.eta", data));
});

app.get("/:owner/:repo/blob/:branch/:path{.*}", async (c) => {
  const { owner, repo, branch, path } = c.req.param();
  const data = await getGitHubBlob(owner, repo, branch, path);
  return c.html(await eta.renderAsync("blob.eta", data));
});

if (process.env.LISTEN_FDS === "1") {
  const server = createAdaptorServer(app);
  // Listen on SD_LISTEN_FDS_START.
  server.listen({ fd: 3 }, () => {
    console.log("Server running via systemd socket activation");
  });
} else {
  const port = parseInt(process.env.PORT || "3000", 10);
  serve({ fetch: app.fetch, port }, (info) => {
    console.log(`Server running on http://localhost:${info.port}`);
  });
}