mirror of
https://github.com/miniflux/v2.git
synced 2025-09-15 18:57:04 +00:00
Refactor config package
- Parse configuration only once during startup time - Store configuration values in a global variable
This commit is contained in:
parent
04d85b3c63
commit
228862fefa
28 changed files with 922 additions and 624 deletions
|
@ -37,7 +37,7 @@ func (h *handler) saveEntry(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
go func() {
|
||||
integration.SendEntry(h.cfg, entry, settings)
|
||||
integration.SendEntry(entry, settings)
|
||||
}()
|
||||
|
||||
json.Created(w, r, map[string]string{"message": "saved"})
|
||||
|
|
|
@ -2,10 +2,9 @@
|
|||
// Use of this source code is governed by the Apache 2.0
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package ui // import "miniflux.app/ui"
|
||||
package ui // import "miniflux.app/ui"
|
||||
|
||||
import (
|
||||
"miniflux.app/config"
|
||||
"miniflux.app/reader/feed"
|
||||
"miniflux.app/storage"
|
||||
"miniflux.app/template"
|
||||
|
@ -16,7 +15,6 @@ import (
|
|||
|
||||
type handler struct {
|
||||
router *mux.Router
|
||||
cfg *config.Config
|
||||
store *storage.Storage
|
||||
tpl *template.Engine
|
||||
pool *worker.Pool
|
||||
|
|
|
@ -2,13 +2,14 @@
|
|||
// Use of this source code is governed by the Apache 2.0
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package ui // import "miniflux.app/ui"
|
||||
package ui // import "miniflux.app/ui"
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"miniflux.app/http/response/html"
|
||||
"miniflux.app/config"
|
||||
"miniflux.app/http/request"
|
||||
"miniflux.app/http/response/html"
|
||||
"miniflux.app/http/route"
|
||||
"miniflux.app/integration/pocket"
|
||||
"miniflux.app/locale"
|
||||
|
@ -31,8 +32,8 @@ func (h *handler) pocketAuthorize(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
sess := session.New(h.store, request.SessionID(r))
|
||||
connector := pocket.NewConnector(h.cfg.PocketConsumerKey(integration.PocketConsumerKey))
|
||||
redirectURL := h.cfg.BaseURL() + route.Path(h.router, "pocketCallback")
|
||||
connector := pocket.NewConnector(config.Opts.PocketConsumerKey(integration.PocketConsumerKey))
|
||||
redirectURL := config.Opts.BaseURL() + route.Path(h.router, "pocketCallback")
|
||||
requestToken, err := connector.RequestToken(redirectURL)
|
||||
if err != nil {
|
||||
logger.Error("[Pocket:Authorize] %v", err)
|
||||
|
@ -61,7 +62,7 @@ func (h *handler) pocketCallback(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
connector := pocket.NewConnector(h.cfg.PocketConsumerKey(integration.PocketConsumerKey))
|
||||
connector := pocket.NewConnector(config.Opts.PocketConsumerKey(integration.PocketConsumerKey))
|
||||
accessToken, err := connector.AccessToken(request.PocketRequestToken(r))
|
||||
if err != nil {
|
||||
logger.Error("[Pocket:Callback] %v", err)
|
||||
|
|
|
@ -7,6 +7,7 @@ package ui // import "miniflux.app/ui"
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"miniflux.app/config"
|
||||
"miniflux.app/http/request"
|
||||
"miniflux.app/http/response/html"
|
||||
"miniflux.app/ui/form"
|
||||
|
@ -59,7 +60,7 @@ func (h *handler) showIntegrationPage(w http.ResponseWriter, r *http.Request) {
|
|||
view.Set("user", user)
|
||||
view.Set("countUnread", h.store.CountUnreadEntries(user.ID))
|
||||
view.Set("countErrorFeeds", h.store.CountErrorFeeds(user.ID))
|
||||
view.Set("hasPocketConsumerKeyConfigured", h.cfg.PocketConsumerKey("") != "")
|
||||
view.Set("hasPocketConsumerKeyConfigured", config.Opts.PocketConsumerKey("") != "")
|
||||
|
||||
html.OK(w, r, view.Render("integrations"))
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package ui // import "miniflux.app/ui"
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"miniflux.app/config"
|
||||
"miniflux.app/http/cookie"
|
||||
"miniflux.app/http/request"
|
||||
"miniflux.app/http/response/html"
|
||||
|
@ -55,8 +56,8 @@ func (h *handler) checkLogin(w http.ResponseWriter, r *http.Request) {
|
|||
http.SetCookie(w, cookie.New(
|
||||
cookie.CookieUserSessionID,
|
||||
sessionToken,
|
||||
h.cfg.IsHTTPS,
|
||||
h.cfg.BasePath(),
|
||||
config.Opts.HTTPS,
|
||||
config.Opts.BasePath(),
|
||||
))
|
||||
|
||||
html.Redirect(w, r, route.Path(h.router, "unread"))
|
||||
|
|
|
@ -7,6 +7,7 @@ package ui // import "miniflux.app/ui"
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"miniflux.app/config"
|
||||
"miniflux.app/http/cookie"
|
||||
"miniflux.app/http/request"
|
||||
"miniflux.app/http/response/html"
|
||||
|
@ -32,8 +33,8 @@ func (h *handler) logout(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
http.SetCookie(w, cookie.Expired(
|
||||
cookie.CookieUserSessionID,
|
||||
h.cfg.IsHTTPS,
|
||||
h.cfg.BasePath(),
|
||||
config.Opts.HTTPS,
|
||||
config.Opts.BasePath(),
|
||||
))
|
||||
|
||||
html.Redirect(w, r, route.Path(h.router, "login"))
|
||||
|
|
|
@ -14,21 +14,20 @@ import (
|
|||
"miniflux.app/http/request"
|
||||
"miniflux.app/http/response/html"
|
||||
"miniflux.app/http/route"
|
||||
"miniflux.app/storage"
|
||||
"miniflux.app/logger"
|
||||
"miniflux.app/model"
|
||||
"miniflux.app/storage"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type middleware struct {
|
||||
router *mux.Router
|
||||
cfg *config.Config
|
||||
store *storage.Storage
|
||||
store *storage.Storage
|
||||
}
|
||||
|
||||
func newMiddleware(router *mux.Router, cfg *config.Config, store *storage.Storage) *middleware {
|
||||
return &middleware{router, cfg, store}
|
||||
func newMiddleware(router *mux.Router, store *storage.Storage) *middleware {
|
||||
return &middleware{router, store}
|
||||
}
|
||||
|
||||
func (m *middleware) handleUserSession(next http.Handler) http.Handler {
|
||||
|
@ -61,7 +60,7 @@ func (m *middleware) handleAppSession(next http.Handler) http.Handler {
|
|||
session := m.getAppSessionValueFromCookie(r)
|
||||
|
||||
if session == nil {
|
||||
if (request.IsAuthenticated(r)) {
|
||||
if request.IsAuthenticated(r) {
|
||||
userID := request.UserID(r)
|
||||
logger.Debug("[UI:AppSession] Cookie expired but user #%d is logged: creating a new session", userID)
|
||||
session, err = m.store.CreateAppSessionWithUserPrefs(userID)
|
||||
|
@ -78,7 +77,7 @@ func (m *middleware) handleAppSession(next http.Handler) http.Handler {
|
|||
}
|
||||
}
|
||||
|
||||
http.SetCookie(w, cookie.New(cookie.CookieAppSessionID, session.ID, m.cfg.IsHTTPS, m.cfg.BasePath()))
|
||||
http.SetCookie(w, cookie.New(cookie.CookieAppSessionID, session.ID, config.Opts.HTTPS, config.Opts.BasePath()))
|
||||
} else {
|
||||
logger.Debug("[UI:AppSession] %s", session)
|
||||
}
|
||||
|
|
|
@ -9,10 +9,10 @@ import (
|
|||
"miniflux.app/oauth2"
|
||||
)
|
||||
|
||||
func getOAuth2Manager(cfg *config.Config) *oauth2.Manager {
|
||||
func getOAuth2Manager() *oauth2.Manager {
|
||||
return oauth2.NewManager(
|
||||
cfg.OAuth2ClientID(),
|
||||
cfg.OAuth2ClientSecret(),
|
||||
cfg.OAuth2RedirectURL(),
|
||||
config.Opts.OAuth2ClientID(),
|
||||
config.Opts.OAuth2ClientSecret(),
|
||||
config.Opts.OAuth2RedirectURL(),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ package ui // import "miniflux.app/ui"
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"miniflux.app/config"
|
||||
"miniflux.app/http/cookie"
|
||||
"miniflux.app/http/request"
|
||||
"miniflux.app/http/response/html"
|
||||
|
@ -43,7 +44,7 @@ func (h *handler) oauth2Callback(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
authProvider, err := getOAuth2Manager(h.cfg).Provider(provider)
|
||||
authProvider, err := getOAuth2Manager().Provider(provider)
|
||||
if err != nil {
|
||||
logger.Error("[OAuth2] %v", err)
|
||||
html.Redirect(w, r, route.Path(h.router, "login"))
|
||||
|
@ -90,7 +91,7 @@ func (h *handler) oauth2Callback(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
if user == nil {
|
||||
if !h.cfg.IsOAuth2UserCreationAllowed() {
|
||||
if !config.Opts.IsOAuth2UserCreationAllowed() {
|
||||
html.Forbidden(w, r)
|
||||
return
|
||||
}
|
||||
|
@ -121,8 +122,8 @@ func (h *handler) oauth2Callback(w http.ResponseWriter, r *http.Request) {
|
|||
http.SetCookie(w, cookie.New(
|
||||
cookie.CookieUserSessionID,
|
||||
sessionToken,
|
||||
h.cfg.IsHTTPS,
|
||||
h.cfg.BasePath(),
|
||||
config.Opts.HTTPS,
|
||||
config.Opts.BasePath(),
|
||||
))
|
||||
|
||||
html.Redirect(w, r, route.Path(h.router, "unread"))
|
||||
|
|
|
@ -24,7 +24,7 @@ func (h *handler) oauth2Redirect(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
authProvider, err := getOAuth2Manager(h.cfg).Provider(provider)
|
||||
authProvider, err := getOAuth2Manager().Provider(provider)
|
||||
if err != nil {
|
||||
logger.Error("[OAuth2] %v", err)
|
||||
html.Redirect(w, r, route.Path(h.router, "login"))
|
||||
|
|
|
@ -24,7 +24,7 @@ func (h *handler) oauth2Unlink(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
authProvider, err := getOAuth2Manager(h.cfg).Provider(provider)
|
||||
authProvider, err := getOAuth2Manager().Provider(provider)
|
||||
if err != nil {
|
||||
logger.Error("[OAuth2] %v", err)
|
||||
html.Redirect(w, r, route.Path(h.router, "settings"))
|
||||
|
|
7
ui/ui.go
7
ui/ui.go
|
@ -7,7 +7,6 @@ package ui // import "miniflux.app/ui"
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"miniflux.app/config"
|
||||
"miniflux.app/reader/feed"
|
||||
"miniflux.app/storage"
|
||||
"miniflux.app/template"
|
||||
|
@ -17,9 +16,9 @@ import (
|
|||
)
|
||||
|
||||
// Serve declares all routes for the user interface.
|
||||
func Serve(router *mux.Router, cfg *config.Config, store *storage.Storage, pool *worker.Pool, feedHandler *feed.Handler) {
|
||||
middleware := newMiddleware(router, cfg, store)
|
||||
handler := &handler{router, cfg, store, template.NewEngine(cfg, router), pool, feedHandler}
|
||||
func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool, feedHandler *feed.Handler) {
|
||||
middleware := newMiddleware(router, store)
|
||||
handler := &handler{router, store, template.NewEngine(router), pool, feedHandler}
|
||||
|
||||
uiRouter := router.NewRoute().Subrouter()
|
||||
uiRouter.Use(middleware.handleUserSession)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue