some extra make target

This commit is contained in:
Daniel Noulet 2026-10-03 12:25:10 +02:00
parent dac02c61c3
commit e454940dfa
2 changed files with 182 additions and 0 deletions

View file

@ -52,6 +52,7 @@ func (h *Handler) Routes(origins string) http.Handler {
mux.HandleFunc("/api/content/categories", h.contentCategories)
mux.HandleFunc("/api/content/badges", h.contentBadges)
mux.HandleFunc("/api/content/", h.contentDetail)
mux.HandleFunc("/api/admin/reset", h.adminReset)
mux.HandleFunc("/", h.singlePageApp)
return cors(mux, origins, h.sso.HeaderName())
}
@ -164,6 +165,29 @@ func (h *Handler) contentList(w http.ResponseWriter, r *http.Request) {
jsonWrite(w, http.StatusOK, map[string]any{"items": items, "limit": limit, "offset": offset})
}
func (h *Handler) contentCreate(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
jsonWrite(w, http.StatusMethodNotAllowed, nil)
return
}
_, authenticated := h.identity(r)
if !authenticated {
jsonWrite(w, http.StatusUnauthorized, map[string]string{"error": "authentication required"})
return
}
var item service.Content
if !decode(r, &item) {
jsonWrite(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON"})
return
}
created, err := h.content.Create(r.Context(), item)
if err != nil {
jsonWrite(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
jsonWrite(w, http.StatusCreated, created)
}
func (h *Handler) contentCategories(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
jsonWrite(w, http.StatusMethodNotAllowed, nil)
@ -227,6 +251,17 @@ func (h *Handler) contentDetail(w http.ResponseWriter, r *http.Request) {
}
jsonWrite(w, http.StatusOK, saved)
case http.MethodDelete:
_, authenticated := h.identity(r)
if !authenticated {
jsonWrite(w, http.StatusUnauthorized, map[string]string{"error": "authentication required"})
return
}
if err := h.content.Delete(r.Context(), slug); err != nil {
jsonWrite(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
jsonWrite(w, http.StatusNoContent, nil)
default:
jsonWrite(w, http.StatusMethodNotAllowed, nil)
}
@ -251,6 +286,23 @@ func (h *Handler) identity(r *http.Request) (string, bool) {
return "", false
}
func (h *Handler) adminReset(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
jsonWrite(w, http.StatusMethodNotAllowed, nil)
return
}
_, authenticated := h.identity(r)
if !authenticated {
jsonWrite(w, http.StatusUnauthorized, map[string]string{"error": "authentication required"})
return
}
if err := h.content.Reset(r.Context()); err != nil {
jsonWrite(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
jsonWrite(w, http.StatusOK, map[string]string{"status": "reset"})
}
// browserUser returns the Go Loose user carried by the request session, if any.
func (h *Handler) browserUser(r *http.Request) (goloose.User, bool) {
if h.browser == nil {