mirror of
https://github.com/miniflux/v2.git
synced 2025-08-26 18:21:01 +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
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
||||
output := imageProxyFilter(r, c, input)
|
||||
output := imageProxyFilter(r, input)
|
||||
expected := `<p><img src="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
|
||||
|
||||
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 := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
||||
output := imageProxyFilter(r, c, input)
|
||||
output := imageProxyFilter(r, input)
|
||||
expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
||||
|
||||
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 := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
||||
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 := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
||||
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 := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
||||
output := imageProxyFilter(r, c, input)
|
||||
output := imageProxyFilter(r, input)
|
||||
expected := `<p><img src="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
|
||||
|
||||
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 := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
||||
output := imageProxyFilter(r, c, input)
|
||||
output := imageProxyFilter(r, input)
|
||||
expected := `<p><img src="/proxy/aHR0cHM6Ly93ZWJzaXRlL2ZvbGRlci9pbWFnZS5wbmc=" alt="Test"/></p>`
|
||||
|
||||
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 := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
||||
output := imageProxyFilter(r, c, input)
|
||||
output := imageProxyFilter(r, input)
|
||||
expected := `<p><img src="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
|
||||
|
||||
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 := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
||||
output := imageProxyFilter(r, c, input)
|
||||
output := imageProxyFilter(r, input)
|
||||
expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
||||
|
||||
if expected != output {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue