handler.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package handler
  5. import (
  6. "net/http"
  7. "time"
  8. "github.com/miniflux/miniflux/config"
  9. "github.com/miniflux/miniflux/locale"
  10. "github.com/miniflux/miniflux/storage"
  11. "github.com/miniflux/miniflux/template"
  12. "github.com/miniflux/miniflux/timer"
  13. "github.com/gorilla/mux"
  14. )
  15. // ControllerFunc is an application HTTP handler.
  16. type ControllerFunc func(ctx *Context, request *Request, response *Response)
  17. // Handler manages HTTP handlers.
  18. type Handler struct {
  19. cfg *config.Config
  20. store *storage.Storage
  21. translator *locale.Translator
  22. template *template.Engine
  23. router *mux.Router
  24. }
  25. // Use is a wrapper around an HTTP handler.
  26. func (h *Handler) Use(f ControllerFunc) http.Handler {
  27. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  28. defer timer.ExecutionTime(time.Now(), r.URL.Path)
  29. ctx := NewContext(r, h.store, h.router, h.translator)
  30. request := NewRequest(r)
  31. response := NewResponse(h.cfg, w, r, h.template)
  32. f(ctx, request, response)
  33. })
  34. }
  35. // NewHandler returns a new Handler.
  36. func NewHandler(cfg *config.Config, store *storage.Storage, router *mux.Router, template *template.Engine, translator *locale.Translator) *Handler {
  37. return &Handler{
  38. cfg: cfg,
  39. store: store,
  40. translator: translator,
  41. router: router,
  42. template: template,
  43. }
  44. }