diff options
author | Frédéric Guillot <fred@miniflux.net> | 2019-06-01 18:18:09 -0700 |
---|---|---|
committer | fguillot <fred@miniflux.net> | 2019-06-02 06:30:08 -0700 |
commit | 228862fefaa645026caa483ffe9993bf8c00b22e (patch) | |
tree | 2b590dc6cda3e50928a31ce673641357805f75ce /ui | |
parent | 04d85b3c63afcf6c9539fc8dc7a91c4e36c2e8fb (diff) |
Refactor config package
- Parse configuration only once during startup time
- Store configuration values in a global variable
Diffstat (limited to 'ui')
-rw-r--r-- | ui/entry_save.go | 2 | ||||
-rw-r--r-- | ui/handler.go | 4 | ||||
-rw-r--r-- | ui/integration_pocket.go | 11 | ||||
-rw-r--r-- | ui/integration_show.go | 3 | ||||
-rw-r--r-- | ui/login_check.go | 5 | ||||
-rw-r--r-- | ui/logout.go | 5 | ||||
-rw-r--r-- | ui/middleware.go | 13 | ||||
-rw-r--r-- | ui/oauth2.go | 8 | ||||
-rw-r--r-- | ui/oauth2_callback.go | 9 | ||||
-rw-r--r-- | ui/oauth2_redirect.go | 2 | ||||
-rw-r--r-- | ui/oauth2_unlink.go | 2 | ||||
-rw-r--r-- | ui/ui.go | 7 |
12 files changed, 36 insertions, 35 deletions
diff --git a/ui/entry_save.go b/ui/entry_save.go index 86d5b4d..43d2503 100644 --- a/ui/entry_save.go +++ b/ui/entry_save.go @@ -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"}) diff --git a/ui/handler.go b/ui/handler.go index 2bccf61..d4d4889 100644 --- a/ui/handler.go +++ b/ui/handler.go @@ -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 diff --git a/ui/integration_pocket.go b/ui/integration_pocket.go index 8f8c680..3e77187 100644 --- a/ui/integration_pocket.go +++ b/ui/integration_pocket.go @@ -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) diff --git a/ui/integration_show.go b/ui/integration_show.go index c7f2740..a43bb07 100644 --- a/ui/integration_show.go +++ b/ui/integration_show.go @@ -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")) } diff --git a/ui/login_check.go b/ui/login_check.go index 46229a4..99c6a70 100644 --- a/ui/login_check.go +++ b/ui/login_check.go @@ -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")) diff --git a/ui/logout.go b/ui/logout.go index 7c63c32..479426b 100644 --- a/ui/logout.go +++ b/ui/logout.go @@ -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")) diff --git a/ui/middleware.go b/ui/middleware.go index f53ac4e..5f6dacd 100644 --- a/ui/middleware.go +++ b/ui/middleware.go @@ -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) } diff --git a/ui/oauth2.go b/ui/oauth2.go index c5d594f..256137d 100644 --- a/ui/oauth2.go +++ b/ui/oauth2.go @@ -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(), ) } diff --git a/ui/oauth2_callback.go b/ui/oauth2_callback.go index bd7c999..6dce3d9 100644 --- a/ui/oauth2_callback.go +++ b/ui/oauth2_callback.go @@ -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")) diff --git a/ui/oauth2_redirect.go b/ui/oauth2_redirect.go index 85116cf..3a4a54f 100644 --- a/ui/oauth2_redirect.go +++ b/ui/oauth2_redirect.go @@ -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")) diff --git a/ui/oauth2_unlink.go b/ui/oauth2_unlink.go index 3283f89..84b009b 100644 --- a/ui/oauth2_unlink.go +++ b/ui/oauth2_unlink.go @@ -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,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) |