diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a2f7c98 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitea.ewpt3ch.dev/ewpt3ch/pkgstash + +go 1.26.2 diff --git a/main.go b/main.go new file mode 100644 index 0000000..f64c797 --- /dev/null +++ b/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "log" + "net/http" + "os" + "path/filepath" +) + +const repoRoot = "/home/ewpt3ch/dev/pacman-cache-server/tmprepo" + +func main() { + const port = "8090" + + mux := http.NewServeMux() + mux.HandleFunc("GET /{repo}/os/{arch}/{file}", handlePackage) + + httpServe := &http.Server{ + Addr: ":" + port, + Handler: mux, + } + + 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) +}