diff options
author | Frédéric Guillot <fred@miniflux.net> | 2018-02-03 15:33:17 -0800 |
---|---|---|
committer | Frédéric Guillot <fred@miniflux.net> | 2018-02-03 15:33:17 -0800 |
commit | 9c4299720900fce52daedfce2314d31e92f7fe1d (patch) | |
tree | 2a67a75d71011d071910a4cb4216d45ee9d904c4 /config/config.go | |
parent | 78385a351e208feb183549c2fa854b302da0c690 (diff) |
Add support for base URLs with subfolders
Diffstat (limited to 'config/config.go')
-rw-r--r-- | config/config.go | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/config/config.go b/config/config.go index 7788a38..05b5d3b 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ package config import ( + "net/url" "os" "strconv" ) @@ -26,7 +27,10 @@ const ( // Config manages configuration parameters. type Config struct { - IsHTTPS bool + IsHTTPS bool + baseURL string + rootURL string + basePath string } func (c *Config) get(key, fallback string) string { @@ -53,13 +57,34 @@ func (c *Config) HasDebugMode() bool { return c.get("DEBUG", "") != "" } -// BaseURL returns the application base URL. +// BaseURL returns the application base URL with path. func (c *Config) BaseURL() string { - baseURL := c.get("BASE_URL", defaultBaseURL) - if baseURL[len(baseURL)-1:] == "/" { - baseURL = baseURL[:len(baseURL)-1] + if c.baseURL == "" { + c.baseURL = c.get("BASE_URL", defaultBaseURL) + if c.baseURL[len(c.baseURL)-1:] == "/" { + c.baseURL = c.baseURL[:len(c.baseURL)-1] + } } - return baseURL + return c.baseURL +} + +// RootURL returns the base URL without path. +func (c *Config) RootURL() string { + if c.rootURL == "" { + u, _ := url.Parse(c.BaseURL()) + u.Path = "" + c.rootURL = u.String() + } + return c.rootURL +} + +// BasePath returns the application base path according to the base URL. +func (c *Config) BasePath() string { + if c.basePath == "" { + u, _ := url.Parse(c.BaseURL()) + c.basePath = u.Path + } + return c.basePath } // DatabaseURL returns the database URL. |