handler.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. if r.Header.Get("X-Forwarded-Proto") == "https" {
  30. h.cfg.IsHTTPS = true
  31. }
  32. ctx := NewContext(r, h.store, h.router, h.translator)
  33. request := NewRequest(r)
  34. response := NewResponse(h.cfg, w, r, h.template)
  35. f(ctx, request, response)
  36. })
  37. }
  38. // NewHandler returns a new Handler.
  39. func NewHandler(cfg *config.Config, store *storage.Storage, router *mux.Router, template *template.Engine, translator *locale.Translator) *Handler {
  40. return &Handler{
  41. cfg: cfg,
  42. store: store,
  43. translator: translator,
  44. router: router,
  45. template: template,
  46. }
  47. }