admin.go 693 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package web
  2. import (
  3. "net/http"
  4. "github.com/kellegous/go/context"
  5. )
  6. type adminHandler struct {
  7. ctx *context.Context
  8. }
  9. func adminGet(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
  10. p := parseName("/admin/", r.URL.Path)
  11. if p == "" {
  12. writeJSONOk(w)
  13. return
  14. }
  15. if p == "dumps" {
  16. if golinks, err := ctx.GetAll(); err != nil {
  17. writeJSONBackendError(w, err)
  18. return
  19. } else {
  20. writeJSON(w, golinks, http.StatusOK)
  21. }
  22. }
  23. }
  24. func (h *adminHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  25. switch r.Method {
  26. case "GET":
  27. adminGet(h.ctx, w, r)
  28. default:
  29. writeJSONError(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusOK) // fix
  30. }
  31. }