mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
Add support for base URLs with subfolders
This commit is contained in:
parent
78385a351e
commit
9c42997209
10 changed files with 105 additions and 23 deletions
|
@ -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.
|
||||
|
|
|
@ -9,7 +9,26 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestGetCustomBaseURL(t *testing.T) {
|
||||
func TestDebugModeOn(t *testing.T) {
|
||||
os.Clearenv()
|
||||
os.Setenv("DEBUG", "1")
|
||||
cfg := NewConfig()
|
||||
|
||||
if !cfg.HasDebugMode() {
|
||||
t.Fatalf(`Unexpected debug mode value, got "%v"`, cfg.HasDebugMode())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebugModeOff(t *testing.T) {
|
||||
os.Clearenv()
|
||||
cfg := NewConfig()
|
||||
|
||||
if cfg.HasDebugMode() {
|
||||
t.Fatalf(`Unexpected debug mode value, got "%v"`, cfg.HasDebugMode())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomBaseURL(t *testing.T) {
|
||||
os.Clearenv()
|
||||
os.Setenv("BASE_URL", "http://example.org")
|
||||
cfg := NewConfig()
|
||||
|
@ -17,9 +36,17 @@ func TestGetCustomBaseURL(t *testing.T) {
|
|||
if cfg.BaseURL() != "http://example.org" {
|
||||
t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL())
|
||||
}
|
||||
|
||||
if cfg.RootURL() != "http://example.org" {
|
||||
t.Fatalf(`Unexpected root URL, got "%s"`, cfg.RootURL())
|
||||
}
|
||||
|
||||
if cfg.BasePath() != "" {
|
||||
t.Fatalf(`Unexpected base path, got "%s"`, cfg.BasePath())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCustomBaseURLWithTrailingSlash(t *testing.T) {
|
||||
func TestCustomBaseURLWithTrailingSlash(t *testing.T) {
|
||||
os.Clearenv()
|
||||
os.Setenv("BASE_URL", "http://example.org/folder/")
|
||||
cfg := NewConfig()
|
||||
|
@ -27,13 +54,29 @@ func TestGetCustomBaseURLWithTrailingSlash(t *testing.T) {
|
|||
if cfg.BaseURL() != "http://example.org/folder" {
|
||||
t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL())
|
||||
}
|
||||
|
||||
if cfg.RootURL() != "http://example.org" {
|
||||
t.Fatalf(`Unexpected root URL, got "%s"`, cfg.BaseURL())
|
||||
}
|
||||
|
||||
if cfg.BasePath() != "/folder" {
|
||||
t.Fatalf(`Unexpected base path, got "%s"`, cfg.BasePath())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDefaultBaseURL(t *testing.T) {
|
||||
func TestDefaultBaseURL(t *testing.T) {
|
||||
os.Clearenv()
|
||||
cfg := NewConfig()
|
||||
|
||||
if cfg.BaseURL() != "http://localhost" {
|
||||
t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL())
|
||||
}
|
||||
|
||||
if cfg.RootURL() != "http://localhost" {
|
||||
t.Fatalf(`Unexpected root URL, got "%s"`, cfg.RootURL())
|
||||
}
|
||||
|
||||
if cfg.BasePath() != "" {
|
||||
t.Fatalf(`Unexpected base path, got "%s"`, cfg.BasePath())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue