miniflux/server/core/handler.go

63 lines
1.7 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +01: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-22 03:30:16 +01:00
"log"
"net/http"
"time"
2017-11-20 06:10:04 +01:00
"github.com/miniflux/miniflux2/helper"
"github.com/miniflux/miniflux2/locale"
"github.com/miniflux/miniflux2/server/middleware"
"github.com/miniflux/miniflux2/server/template"
"github.com/miniflux/miniflux2/storage"
"github.com/gorilla/mux"
)
2017-11-22 04:37:47 +01:00
// HandlerFunc is an application HTTP handler.
2017-11-20 06:10:04 +01:00
type HandlerFunc func(ctx *Context, request *Request, response *Response)
2017-11-22 04:37:47 +01:00
// Handler manages HTTP handlers and middlewares.
2017-11-20 06:10:04 +01:00
type Handler struct {
store *storage.Storage
translator *locale.Translator
2017-11-22 04:37:47 +01:00
template *template.Engine
2017-11-20 06:10:04 +01:00
router *mux.Router
2017-11-28 06:30:04 +01:00
middleware *middleware.Chain
2017-11-20 06:10:04 +01:00
}
2017-11-22 04:37:47 +01:00
// Use is a wrapper around an HTTP handler.
2017-11-20 06:10:04 +01:00
func (h *Handler) Use(f HandlerFunc) http.Handler {
return h.middleware.WrapFunc(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer helper.ExecutionTime(time.Now(), r.URL.Path)
log.Println(r.Method, r.URL.Path)
ctx := NewContext(w, r, h.store, h.router)
request := NewRequest(w, r)
response := NewResponse(w, r, h.template)
if ctx.IsAuthenticated() {
2017-11-22 03:37:08 +01:00
h.template.SetLanguage(ctx.UserLanguage())
2017-11-20 06:10:04 +01:00
} else {
h.template.SetLanguage("en_US")
}
f(ctx, request, response)
}))
}
2017-11-22 04:37:47 +01:00
// NewHandler returns a new Handler.
2017-11-28 06:30:04 +01:00
func NewHandler(store *storage.Storage, router *mux.Router, template *template.Engine, translator *locale.Translator, middleware *middleware.Chain) *Handler {
2017-11-20 06:10:04 +01:00
return &Handler{
store: store,
translator: translator,
router: router,
template: template,
middleware: middleware,
}
}