added refresh db capability and did some refactoring

This commit is contained in:
2026-04-14 22:46:23 -06:00
parent 36f856cb25
commit 5687f7f992
5 changed files with 70 additions and 33 deletions
+6 -4
View File
@@ -1,13 +1,15 @@
package main package main
type Config struct { type Config struct {
RepoPath string MirrorRoot string
MirrorURL string MirrorURL string
Port string
} }
func NewConfig() *Config { func NewConfig() *Config {
return &Config{ return &Config{
RepoPath: "/home/ewpt3ch/dev/pacman-cache-server/tmprepo", MirrorRoot: "/home/ewpt3ch/dev/pacman-cache-server/tmprepo",
MirrorURL: "https://us.mirrors.cicku.me/archlinux/", MirrorURL: "https://us.mirrors.cicku.me/archlinux/",
Port: "8090",
} }
} }
+5 -5
View File
@@ -9,17 +9,17 @@ import (
"gitea.ewpt3ch.dev/ewpt3ch/pkgstash/internal/cache" "gitea.ewpt3ch.dev/ewpt3ch/pkgstash/internal/cache"
) )
func handlePackage(w http.ResponseWriter, req *http.Request, c *cache.Cache) { func (s *Server) handlePackage(w http.ResponseWriter, req *http.Request) {
// build file paths from the request, they follow archlinux repo // build file paths from the request, they follow archlinux repo
// <reporoot>/[core, extra, etc]/os/[x86_64, arm, etc]/package.pkg.tar.zst[.sig] // <mirrorroot>/[core, extra, etc]/os/[x86_64, arm, etc]/package.pkg.tar.zst[.sig]
repo := req.PathValue("repo") repo := req.PathValue("repo")
arch := req.PathValue("arch") arch := req.PathValue("arch")
file := req.PathValue("file") file := req.PathValue("file")
relPath := filepath.Join(repo, "os", arch, file) //path from repo root to pkg or db file repoPath := filepath.Join(repo, "os", arch, file) //path from mirror root to pkg or db file
pkgPath := filepath.Join(repoRoot, relPath) //path for local read of the file pkgPath := filepath.Join(s.cfg.MirrorRoot, repoPath) //absolute path for local read of the file
if _, err := os.Stat(pkgPath); err != nil { if _, err := os.Stat(pkgPath); err != nil {
err = c.Fetch(relPath) err = s.c.Fetch(repoPath)
if err != nil { if err != nil {
var upstreamErr *cache.UpstreamError var upstreamErr *cache.UpstreamError
if errors.As(err, &upstreamErr) { if errors.As(err, &upstreamErr) {
+20 -17
View File
@@ -3,24 +3,27 @@ package cache
import ( import (
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"golang.org/x/sync/singleflight" "golang.org/x/sync/singleflight"
) )
type Cache struct { type Cache struct {
repo string localRoot string
mirrorURL string mirrorURL string
sf singleflight.Group mirroredRepos []string
sf singleflight.Group //prevents duplicate downloads
mu sync.Mutex
} }
func NewCache(repo string, mirrorURL string) *Cache { func NewCache(localRoot string, mirrorURL string) *Cache {
return &Cache{ return &Cache{
repo: repo, localRoot: localRoot,
mirrorURL: mirrorURL, mirrorURL: mirrorURL,
mirroredRepos: []string{"core", "extra"},
} }
} }
@@ -39,12 +42,12 @@ func (e *UpstreamError) Error() string {
return fmt.Sprintf("upstream returned %d", e.StatusCode) return fmt.Sprintf("upstream returned %d", e.StatusCode)
} }
func (c *Cache) fetch(pkgPath string) error { func (c *Cache) fetch(pkgName string) error {
// pkgPath is relative to the repo or mirror root // pkgName is relative to the localRoot
tempPkgPath := pkgPath + ".tmp" tempPkgName := pkgName + ".tmp"
tempPkgName := filepath.Join(c.repo, tempPkgPath) //full tmp write path tempPkgPath := filepath.Join(c.localRoot, tempPkgName) //full tmp write path
outPkg := filepath.Join(c.repo, pkgPath) outPkg := filepath.Join(c.localRoot, pkgName)
pkgURL := c.mirrorURL + pkgPath pkgURL := c.mirrorURL + pkgName
resp, err := http.Get(pkgURL) resp, err := http.Get(pkgURL)
if err != nil { if err != nil {
@@ -56,7 +59,7 @@ func (c *Cache) fetch(pkgPath string) error {
return &UpstreamError{StatusCode: resp.StatusCode} return &UpstreamError{StatusCode: resp.StatusCode}
} }
outFile, err := os.Create(tempPkgName) outFile, err := os.Create(tempPkgPath)
if err != nil { if err != nil {
return err return err
} }
@@ -64,12 +67,12 @@ func (c *Cache) fetch(pkgPath string) error {
_, err = io.Copy(outFile, resp.Body) _, err = io.Copy(outFile, resp.Body)
if err != nil { if err != nil {
os.Remove(tempPkgName) os.Remove(tempPkgPath)
return err return err
} }
if err := os.Rename(tempPkgName, outPkg); err != nil { if err := os.Rename(tempPkgPath, outPkg); err != nil {
os.Remove(tempPkgName) os.Remove(tempPkgPath)
return err return err
} }
return nil return nil
+27
View File
@@ -0,0 +1,27 @@
package cache
import "path/filepath"
func (c *Cache) Refresh() error {
if !c.mu.TryLock() {
return nil
}
defer c.mu.Unlock()
for _, repo := range c.mirroredRepos {
if err := c.refreshDB(repo); err != nil {
return err
}
}
return nil
}
func (c *Cache) refreshDB(repo string) error {
dbFile := repo + ".db.tar.gz"
dbPath := filepath.Join(repo, "os/x86_64", dbFile)
err := c.Fetch(dbPath)
if err != nil {
return err
}
return nil
}
+12 -7
View File
@@ -7,20 +7,25 @@ import (
"gitea.ewpt3ch.dev/ewpt3ch/pkgstash/internal/cache" "gitea.ewpt3ch.dev/ewpt3ch/pkgstash/internal/cache"
) )
const repoRoot = "/home/ewpt3ch/dev/pacman-cache-server/tmprepo" type Server struct {
cfg *Config
c *cache.Cache
}
func main() { func main() {
const port = "8090"
cfg := NewConfig() cfg := NewConfig()
c := cache.NewCache(cfg.RepoPath, cfg.MirrorURL) c := cache.NewCache(cfg.MirrorRoot, cfg.MirrorURL)
srv := &Server{cfg: cfg, c: c}
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("GET /{repo}/os/{arch}/{file}", func(w http.ResponseWriter, req *http.Request) { mux.HandleFunc("GET /{repo}/os/{arch}/{file}", srv.handlePackage)
handlePackage(w, req, c)
}) if err := srv.c.Refresh(); err != nil {
log.Fatal(err)
}
httpServe := &http.Server{ httpServe := &http.Server{
Addr: ":" + port, Addr: ":" + srv.cfg.Port,
Handler: mux, Handler: mux,
} }