first with full handle over rpm
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
7948368573
commit
274ea454dd
50 changed files with 4309 additions and 0 deletions
43
internal/api/proxy_handler.go
Normal file
43
internal/api/proxy_handler.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue