From 228862fefaa645026caa483ffe9993bf8c00b22e Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sat, 1 Jun 2019 18:18:09 -0700 Subject: Refactor config package - Parse configuration only once during startup time - Store configuration values in a global variable --- template/engine.go | 5 ++--- template/functions.go | 23 +++++++++-------------- template/functions_test.go | 32 ++++++++++++++++---------------- 3 files changed, 27 insertions(+), 33 deletions(-) (limited to 'template') diff --git a/template/engine.go b/template/engine.go index e4cdc96..c7f7566 100644 --- a/template/engine.go +++ b/template/engine.go @@ -9,7 +9,6 @@ import ( "html/template" "time" - "miniflux.app/config" "miniflux.app/errors" "miniflux.app/locale" "miniflux.app/logger" @@ -78,10 +77,10 @@ func (e *Engine) Render(name, language string, data interface{}) []byte { } // NewEngine returns a new template engine. -func NewEngine(cfg *config.Config, router *mux.Router) *Engine { +func NewEngine(router *mux.Router) *Engine { tpl := &Engine{ templates: make(map[string]*template.Template), - funcMap: newFuncMap(cfg, router), + funcMap: &funcMap{router}, } tpl.parseAll() diff --git a/template/functions.go b/template/functions.go index d99f237..0f5b180 100644 --- a/template/functions.go +++ b/template/functions.go @@ -7,8 +7,8 @@ package template // import "miniflux.app/template" import ( "encoding/base64" "fmt" - "math" "html/template" + "math" "net/mail" "strings" "time" @@ -20,12 +20,11 @@ import ( "miniflux.app/timezone" "miniflux.app/url" - "github.com/gorilla/mux" "github.com/PuerkitoBio/goquery" + "github.com/gorilla/mux" ) type funcMap struct { - cfg *config.Config router *mux.Router } @@ -37,13 +36,13 @@ func (f *funcMap) Map() template.FuncMap { "truncate": truncate, "isEmail": isEmail, "baseURL": func() string { - return f.cfg.BaseURL() + return config.Opts.BaseURL() }, "rootURL": func() string { - return f.cfg.RootURL() + return config.Opts.RootURL() }, "hasOAuth2Provider": func(provider string) bool { - return f.cfg.OAuth2Provider() == provider + return config.Opts.OAuth2Provider() == provider }, "route": func(name string, args ...interface{}) string { return route.Path(f.router, name, args...) @@ -52,10 +51,10 @@ func (f *funcMap) Map() template.FuncMap { return template.HTML(str) }, "proxyFilter": func(data string) string { - return imageProxyFilter(f.router, f.cfg, data) + return imageProxyFilter(f.router, data) }, "proxyURL": func(link string) string { - proxyImages := f.cfg.ProxyImages() + proxyImages := config.Opts.ProxyImages() if proxyImages == "all" || (proxyImages != "none" && !url.IsHTTPS(link)) { return proxify(f.router, link) @@ -92,10 +91,6 @@ func (f *funcMap) Map() template.FuncMap { } } -func newFuncMap(cfg *config.Config, router *mux.Router) *funcMap { - return &funcMap{cfg, router} -} - func dict(values ...interface{}) (map[string]interface{}, error) { if len(values)%2 != 0 { return nil, fmt.Errorf("dict expects an even number of arguments") @@ -178,8 +173,8 @@ func elapsedTime(printer *locale.Printer, tz string, t time.Time) string { } } -func imageProxyFilter(router *mux.Router, cfg *config.Config, data string) string { - proxyImages := cfg.ProxyImages() +func imageProxyFilter(router *mux.Router, data string) string { + proxyImages := config.Opts.ProxyImages() if proxyImages == "none" { return data } diff --git a/template/functions_test.go b/template/functions_test.go index a816eb7..ff563b2 100644 --- a/template/functions_test.go +++ b/template/functions_test.go @@ -134,13 +134,13 @@ func TestElapsedTime(t *testing.T) { func TestProxyFilterWithHttpDefault(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "http-only") - c := config.NewConfig() + config.ParseConfig() r := mux.NewRouter() r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `

Test

` - output := imageProxyFilter(r, c, input) + output := imageProxyFilter(r, input) expected := `

Test

` if expected != output { @@ -151,13 +151,13 @@ func TestProxyFilterWithHttpDefault(t *testing.T) { func TestProxyFilterWithHttpsDefault(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "http-only") - c := config.NewConfig() + config.ParseConfig() r := mux.NewRouter() r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `

Test

` - output := imageProxyFilter(r, c, input) + output := imageProxyFilter(r, input) expected := `

Test

` if expected != output { @@ -168,13 +168,13 @@ func TestProxyFilterWithHttpsDefault(t *testing.T) { func TestProxyFilterWithHttpNever(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "none") - c := config.NewConfig() + config.ParseConfig() r := mux.NewRouter() r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `

Test

` - output := imageProxyFilter(r, c, input) + output := imageProxyFilter(r, input) expected := input if expected != output { @@ -185,13 +185,13 @@ func TestProxyFilterWithHttpNever(t *testing.T) { func TestProxyFilterWithHttpsNever(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "none") - c := config.NewConfig() + config.ParseConfig() r := mux.NewRouter() r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `

Test

` - output := imageProxyFilter(r, c, input) + output := imageProxyFilter(r, input) expected := input if expected != output { @@ -202,13 +202,13 @@ func TestProxyFilterWithHttpsNever(t *testing.T) { func TestProxyFilterWithHttpAlways(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "all") - c := config.NewConfig() + config.ParseConfig() r := mux.NewRouter() r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `

Test

` - output := imageProxyFilter(r, c, input) + output := imageProxyFilter(r, input) expected := `

Test

` if expected != output { @@ -219,13 +219,13 @@ func TestProxyFilterWithHttpAlways(t *testing.T) { func TestProxyFilterWithHttpsAlways(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "all") - c := config.NewConfig() + config.ParseConfig() r := mux.NewRouter() r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `

Test

` - output := imageProxyFilter(r, c, input) + output := imageProxyFilter(r, input) expected := `

Test

` if expected != output { @@ -236,13 +236,13 @@ func TestProxyFilterWithHttpsAlways(t *testing.T) { func TestProxyFilterWithHttpInvalid(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "invalid") - c := config.NewConfig() + config.ParseConfig() r := mux.NewRouter() r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `

Test

` - output := imageProxyFilter(r, c, input) + output := imageProxyFilter(r, input) expected := `

Test

` if expected != output { @@ -253,13 +253,13 @@ func TestProxyFilterWithHttpInvalid(t *testing.T) { func TestProxyFilterWithHttpsInvalid(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "invalid") - c := config.NewConfig() + config.ParseConfig() r := mux.NewRouter() r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `

Test

` - output := imageProxyFilter(r, c, input) + output := imageProxyFilter(r, input) expected := `

Test

` if expected != output { -- cgit v1.2.3