admin.go 817 B

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