1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00
miniflux-v2/http/handler/handler.go

55 lines
1.4 KiB
Go
Raw Normal View History

2017-11-19 21:10:04 -08:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package handler
2017-11-19 21:10:04 -08:00
import (
2017-11-21 18:30:16 -08:00
"net/http"
"time"
"github.com/miniflux/miniflux/config"
2017-12-12 21:48:13 -08:00
"github.com/miniflux/miniflux/locale"
"github.com/miniflux/miniflux/storage"
"github.com/miniflux/miniflux/template"
2018-01-02 19:15:08 -08:00
"github.com/miniflux/miniflux/timer"
2017-11-19 21:10:04 -08:00
"github.com/gorilla/mux"
)
// ControllerFunc is an application HTTP handler.
type ControllerFunc func(ctx *Context, request *Request, response *Response)
2017-11-19 21:10:04 -08:00
2018-04-27 20:38:46 -07:00
// Handler manages HTTP handlers.
2017-11-19 21:10:04 -08:00
type Handler struct {
cfg *config.Config
2017-11-19 21:10:04 -08:00
store *storage.Storage
translator *locale.Translator
2017-11-21 19:37:47 -08:00
template *template.Engine
2017-11-19 21:10:04 -08:00
router *mux.Router
}
2017-11-21 19:37:47 -08:00
// Use is a wrapper around an HTTP handler.
func (h *Handler) Use(f ControllerFunc) http.Handler {
2018-04-27 20:38:46 -07:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2018-01-02 19:15:08 -08:00
defer timer.ExecutionTime(time.Now(), r.URL.Path)
ctx := NewContext(r, h.store, h.router, h.translator)
request := NewRequest(r)
response := NewResponse(h.cfg, w, r, h.template)
2017-11-19 21:10:04 -08:00
f(ctx, request, response)
2018-04-27 20:38:46 -07:00
})
2017-11-19 21:10:04 -08:00
}
2017-11-21 19:37:47 -08:00
// NewHandler returns a new Handler.
2018-04-27 20:38:46 -07:00
func NewHandler(cfg *config.Config, store *storage.Storage, router *mux.Router, template *template.Engine, translator *locale.Translator) *Handler {
2017-11-19 21:10:04 -08:00
return &Handler{
cfg: cfg,
2017-11-19 21:10:04 -08:00
store: store,
translator: translator,
router: router,
template: template,
}
}