implmented fetch on cache miss

This commit is contained in:
2026-04-13 17:26:15 -06:00
parent dc21cc8f5b
commit 54315ced9d
6 changed files with 135 additions and 21 deletions
+7 -21
View File
@@ -3,17 +3,21 @@ package main
import (
"log"
"net/http"
"os"
"path/filepath"
"gitea.ewpt3ch.dev/ewpt3ch/pkgstash/internal/cache"
)
const repoRoot = "/home/ewpt3ch/dev/pacman-cache-server/tmprepo"
func main() {
const port = "8090"
cfg := NewConfig()
c := cache.NewCache(cfg.RepoPath, cfg.MirrorURL)
mux := http.NewServeMux()
mux.HandleFunc("GET /{repo}/os/{arch}/{file}", handlePackage)
mux.HandleFunc("GET /{repo}/os/{arch}/{file}", func(w http.ResponseWriter, req *http.Request) {
handlePackage(w, req, c)
})
httpServe := &http.Server{
Addr: ":" + port,
@@ -23,21 +27,3 @@ func main() {
log.Fatal(httpServe.ListenAndServe())
}
func handlePackage(w http.ResponseWriter, req *http.Request) {
repo := req.PathValue("repo")
arch := req.PathValue("arch")
file := req.PathValue("file")
filePath := filepath.Join(repoRoot, repo, "os", arch, file)
// is where we handle cache misses and fetch the file
// from a mirror, for now we send a 404
if _, err := os.Stat(filePath); err != nil {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(404)
w.Write([]byte("No such file"))
return
}
http.ServeFile(w, req, filePath)
}