import { TLRUCache } from "@thi.ng/cache/tlru" ; import * as express from "express" ; import * as fs from "fs" ; import * as Bundler from "parcel-bundler" ; import type { Commit } from "../common/api" ; import { ctx } from "../common/config" ; import { buildRepoTableHTML } from "./build-table" ; import { repoCommits } from "./git" ; const rawCache = new TLRUCache < string , Commit [ ] > ( undefined , { ttl : 60 * 60 * 1000 } ) ; const htmlCache = new TLRUCache < string , string > ( undefined , { ttl : 60 * 60 * 1000 } ) ; const bundler = new Bundler ( "index.html" , { outDir : "./out" , outFile : "index.html" , publicUrl : "/out" , } ) ; const getCommits = async ( ) => { const commits = [ ... repoCommits ( ctx . repo . path ) ] ; fs . writeFileSync ( "commits.json" , JSON . stringify ( commits ) ) ; return commits ; } ; const app = express ( ) ; app . get ( "/" , ( _ , res ) => { res . redirect ( "/out/" ) ; } ) ; app . get ( "/commits" , ( _ , res ) => { rawCache . getSet ( ctx . repo . path , getCommits ) . then ( ( commits ) => res . type ( "json" ) . send ( commits ) ) ; } ) ; app . get ( "/ssr" , ( _ , res ) => { htmlCache . getSet ( ctx . repo . path , async ( ) => buildRepoTableHTML ( await rawCache . getSet ( ctx . repo . path , getCommits ) ) ) . then ( ( doc ) => res . send ( doc ) ) ; } ) ; app . use ( express . static ( "." ) ) ; app . use ( bundler . middleware ( ) ) ; console . log ( "starting server @ http://localhost:8080" ) ; app . listen ( 8080 ) ;