add apt gestion
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
274ea454dd
commit
c4776d81dd
22 changed files with 1465 additions and 163 deletions
|
|
@ -21,13 +21,15 @@ func NewProxyHandler(repoSvc *core.RepoService, dataDir string) *ProxyHandler {
|
|||
}
|
||||
|
||||
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
|
||||
}
|
||||
ref := chi.URLParam(r, "repo_ref")
|
||||
|
||||
repo, err := h.repoSvc.Get(r.Context(), repoID)
|
||||
var repo *store.Repo
|
||||
var err error
|
||||
if id, parseErr := strconv.ParseInt(ref, 10, 64); parseErr == nil {
|
||||
repo, err = h.repoSvc.Get(r.Context(), id)
|
||||
} else {
|
||||
repo, err = h.repoSvc.GetByName(r.Context(), ref)
|
||||
}
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
|
|
@ -37,7 +39,7 @@ func (h *ProxyHandler) ServeFile(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
localDir := fmt.Sprintf("%s/repos/%d/%s", h.dataDir, repoID, repo.Type)
|
||||
prefix := fmt.Sprintf("/mirror/%d", repoID)
|
||||
localDir := fmt.Sprintf("%s/repos/%d/%s", h.dataDir, repo.ID, repo.Type)
|
||||
prefix := fmt.Sprintf("/mirror/%s", ref)
|
||||
http.StripPrefix(prefix, http.FileServer(http.Dir(localDir))).ServeHTTP(w, r)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
aptclone "github.com/syonad/clonepack/internal/clone/apt"
|
||||
"github.com/syonad/clonepack/internal/core"
|
||||
"github.com/syonad/clonepack/internal/store"
|
||||
)
|
||||
|
|
@ -27,10 +28,13 @@ func (h *RepoHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
repo, err := h.svc.Create(r.Context(), core.CreateRepoInput{
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
SourceURL: req.SourceURL,
|
||||
SyncMode: req.SyncMode,
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
SourceURL: req.SourceURL,
|
||||
SyncMode: req.SyncMode,
|
||||
AptSuite: req.AptSuite,
|
||||
AptComponents: req.AptComponents,
|
||||
AptArchitectures: req.AptArchitectures,
|
||||
})
|
||||
if err != nil {
|
||||
Error(w, http.StatusUnprocessableEntity, err.Error())
|
||||
|
|
@ -72,6 +76,37 @@ func (h *RepoHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|||
JSON(w, http.StatusOK, repoToResponse(repo))
|
||||
}
|
||||
|
||||
func (h *RepoHandler) Update(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
var req UpdateRepoRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid JSON")
|
||||
return
|
||||
}
|
||||
|
||||
repo, err := h.svc.Update(r.Context(), id, core.UpdateRepoInput{
|
||||
SourceURL: req.SourceURL,
|
||||
SyncMode: req.SyncMode,
|
||||
AptSuite: req.AptSuite,
|
||||
AptComponents: req.AptComponents,
|
||||
AptArchitectures: req.AptArchitectures,
|
||||
})
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
Error(w, http.StatusNotFound, "repo not found")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
Error(w, http.StatusUnprocessableEntity, err.Error())
|
||||
return
|
||||
}
|
||||
JSON(w, http.StatusOK, repoToResponse(repo))
|
||||
}
|
||||
|
||||
func (h *RepoHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
|
|
@ -90,7 +125,7 @@ func (h *RepoHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func repoToResponse(r *store.Repo) RepoResponse {
|
||||
return RepoResponse{
|
||||
resp := RepoResponse{
|
||||
ID: r.ID,
|
||||
Name: r.Name,
|
||||
Type: r.Type,
|
||||
|
|
@ -99,4 +134,13 @@ func repoToResponse(r *store.Repo) RepoResponse {
|
|||
SyncMode: r.SyncMode,
|
||||
CreatedAt: r.CreatedAt,
|
||||
}
|
||||
if r.Type == "apt" && r.Config != "" {
|
||||
var cfg aptclone.Config
|
||||
if err := json.Unmarshal([]byte(r.Config), &cfg); err == nil {
|
||||
resp.AptSuite = cfg.Suite
|
||||
resp.AptComponents = cfg.Components
|
||||
resp.AptArchitectures = cfg.Architectures
|
||||
}
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ func NewRouter(repoHandler *RepoHandler, cloneHandler *CloneHandler, syncHandler
|
|||
r.Use(middleware.Recoverer)
|
||||
r.Use(middleware.RequestID)
|
||||
|
||||
r.Get("/mirror/{repo_id}/*", proxyHandler.ServeFile)
|
||||
r.Get("/mirror/{repo_ref}/*", proxyHandler.ServeFile)
|
||||
|
||||
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
JSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
|
|
@ -24,6 +24,7 @@ func NewRouter(repoHandler *RepoHandler, cloneHandler *CloneHandler, syncHandler
|
|||
r.Post("/", repoHandler.Create)
|
||||
r.Get("/", repoHandler.List)
|
||||
r.Get("/{id}", repoHandler.Get)
|
||||
r.Patch("/{id}", repoHandler.Update)
|
||||
r.Delete("/{id}", repoHandler.Delete)
|
||||
|
||||
r.Post("/{id}/clone", cloneHandler.StartClone)
|
||||
|
|
|
|||
|
|
@ -3,20 +3,26 @@ package api
|
|||
import "time"
|
||||
|
||||
type CreateRepoRequest struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
SourceURL string `json:"source_url"`
|
||||
SyncMode string `json:"sync_mode,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
SourceURL string `json:"source_url"`
|
||||
SyncMode string `json:"sync_mode,omitempty"`
|
||||
AptSuite string `json:"apt_suite,omitempty"`
|
||||
AptComponents []string `json:"apt_components,omitempty"`
|
||||
AptArchitectures []string `json:"apt_architectures,omitempty"`
|
||||
}
|
||||
|
||||
type RepoResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
SourceURL string `json:"source_url"`
|
||||
Frozen bool `json:"frozen"`
|
||||
SyncMode string `json:"sync_mode"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
SourceURL string `json:"source_url"`
|
||||
Frozen bool `json:"frozen"`
|
||||
SyncMode string `json:"sync_mode"`
|
||||
AptSuite string `json:"apt_suite,omitempty"`
|
||||
AptComponents []string `json:"apt_components,omitempty"`
|
||||
AptArchitectures []string `json:"apt_architectures,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type ListReposResponse struct {
|
||||
|
|
@ -24,6 +30,14 @@ type ListReposResponse struct {
|
|||
Total int `json:"total"`
|
||||
}
|
||||
|
||||
type UpdateRepoRequest struct {
|
||||
SourceURL *string `json:"source_url,omitempty"`
|
||||
SyncMode *string `json:"sync_mode,omitempty"`
|
||||
AptSuite *string `json:"apt_suite,omitempty"`
|
||||
AptComponents []string `json:"apt_components,omitempty"`
|
||||
AptArchitectures []string `json:"apt_architectures,omitempty"`
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue