package api import ( "errors" "fmt" "net/http" "strconv" "github.com/go-chi/chi/v5" "github.com/syonad/clonepack/internal/core" "github.com/syonad/clonepack/internal/store" ) type ProxyHandler struct { repoSvc *core.RepoService dataDir string } func NewProxyHandler(repoSvc *core.RepoService, dataDir string) *ProxyHandler { return &ProxyHandler{repoSvc: repoSvc, dataDir: dataDir} } func (h *ProxyHandler) ServeFile(w http.ResponseWriter, r *http.Request) { repoID, err := strconv.ParseInt(chi.URLParam(r, "repo_id"), 10, 64) if err != nil { http.Error(w, "invalid repo_id", http.StatusBadRequest) return } repo, err := h.repoSvc.Get(r.Context(), repoID) if errors.Is(err, store.ErrNotFound) { http.NotFound(w, r) return } if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } localDir := fmt.Sprintf("%s/repos/%d/%s", h.dataDir, repoID, repo.Type) prefix := fmt.Sprintf("/mirror/%d", repoID) http.StripPrefix(prefix, http.FileServer(http.Dir(localDir))).ServeHTTP(w, r) }