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

71 lines
2 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 core
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"
2017-12-15 18:55:57 -08:00
"github.com/miniflux/miniflux/logger"
2017-12-12 21:48:13 -08:00
"github.com/miniflux/miniflux/server/middleware"
"github.com/miniflux/miniflux/server/template"
"github.com/miniflux/miniflux/storage"
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"
"github.com/tomasen/realip"
2017-11-19 21:10:04 -08:00
)
2017-11-21 19:37:47 -08:00
// HandlerFunc is an application HTTP handler.
2017-11-19 21:10:04 -08:00
type HandlerFunc func(ctx *Context, request *Request, response *Response)
2017-11-21 19:37:47 -08:00
// Handler manages HTTP handlers and middlewares.
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-27 21:30:04 -08:00
middleware *middleware.Chain
2017-11-19 21:10:04 -08:00
}
2017-11-21 19:37:47 -08:00
// Use is a wrapper around an HTTP handler.
2017-11-19 21:10:04 -08:00
func (h *Handler) Use(f HandlerFunc) http.Handler {
return h.middleware.WrapFunc(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)
logger.Debug("[HTTP] %s %s %s", realip.RealIP(r), r.Method, r.URL.Path)
if r.Header.Get("X-Forwarded-Proto") == "https" {
h.cfg.IsHTTPS = true
}
2017-11-19 21:10:04 -08:00
2017-12-16 18:07:53 -08:00
ctx := NewContext(w, r, h.store, h.router, h.translator)
2017-11-19 21:10:04 -08:00
request := NewRequest(w, r)
response := NewResponse(w, r, h.template)
if ctx.IsAuthenticated() {
2017-11-21 18:37:08 -08:00
h.template.SetLanguage(ctx.UserLanguage())
2017-11-19 21:10:04 -08:00
} else {
h.template.SetLanguage("en_US")
}
f(ctx, request, response)
}))
}
2017-11-21 19:37:47 -08:00
// NewHandler returns a new Handler.
func NewHandler(cfg *config.Config, store *storage.Storage, router *mux.Router, template *template.Engine, translator *locale.Translator, middleware *middleware.Chain) *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,
middleware: middleware,
}
}