From 9c4299720900fce52daedfce2314d31e92f7fe1d Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sat, 3 Feb 2018 15:33:17 -0800 Subject: Add support for base URLs with subfolders --- config/config.go | 37 ++++++++++++++++++++++++++----- config/config_test.go | 49 ++++++++++++++++++++++++++++++++++++++--- daemon/routes.go | 4 ++++ http/cookie/cookie.go | 15 +++++++++---- http/middleware/session.go | 2 +- template/html/integrations.html | 4 ++-- template/template.go | 3 +++ template/views.go | 8 +++---- ui/login.go | 4 ++-- ui/oauth2.go | 2 +- 10 files changed, 105 insertions(+), 23 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. diff --git a/config/config_test.go b/config/config_test.go index 4c01bc5..fbc7175 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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()) + } } diff --git a/daemon/routes.go b/daemon/routes.go index 6b44fb4..35cec9e 100644 --- a/daemon/routes.go +++ b/daemon/routes.go @@ -45,6 +45,10 @@ func routes(cfg *config.Config, store *storage.Storage, feedHandler *feed.Handle middleware.NewSessionMiddleware(cfg, store).Handler, )) + if cfg.BasePath() != "" { + router = router.PathPrefix(cfg.BasePath()).Subrouter() + } + router.Handle("/fever/", feverHandler.Use(feverController.Handler)).Name("feverEndpoint") router.Handle("/v1/users", apiHandler.Use(apiController.CreateUser)).Methods("POST") diff --git a/http/cookie/cookie.go b/http/cookie/cookie.go index d1f3e72..4407daa 100644 --- a/http/cookie/cookie.go +++ b/http/cookie/cookie.go @@ -19,11 +19,11 @@ const ( ) // New creates a new cookie. -func New(name, value string, isHTTPS bool) *http.Cookie { +func New(name, value string, isHTTPS bool, path string) *http.Cookie { return &http.Cookie{ Name: name, Value: value, - Path: "/", + Path: basePath(path), Secure: isHTTPS, HttpOnly: true, Expires: time.Now().Add(cookieDuration * 24 * time.Hour), @@ -31,14 +31,21 @@ func New(name, value string, isHTTPS bool) *http.Cookie { } // Expired returns an expired cookie. -func Expired(name string, isHTTPS bool) *http.Cookie { +func Expired(name string, isHTTPS bool, path string) *http.Cookie { return &http.Cookie{ Name: name, Value: "", - Path: "/", + Path: basePath(path), Secure: isHTTPS, HttpOnly: true, MaxAge: -1, Expires: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), } } + +func basePath(path string) string { + if path == "" { + return "/" + } + return path +} diff --git a/http/middleware/session.go b/http/middleware/session.go index c3876f6..9b7dd86 100644 --- a/http/middleware/session.go +++ b/http/middleware/session.go @@ -36,7 +36,7 @@ func (s *SessionMiddleware) Handler(next http.Handler) http.Handler { return } - http.SetCookie(w, cookie.New(cookie.CookieSessionID, session.ID, s.cfg.IsHTTPS)) + http.SetCookie(w, cookie.New(cookie.CookieSessionID, session.ID, s.cfg.IsHTTPS, s.cfg.BasePath())) } else { logger.Debug("[Middleware:Session] %s", session) } diff --git a/template/html/integrations.html b/template/html/integrations.html index 8c597ee..91f5b7a 100644 --- a/template/html/integrations.html +++ b/template/html/integrations.html @@ -40,7 +40,7 @@ -

{{ t "Fever API endpoint:" }} {{ baseURL }}{{ route "feverEndpoint" }}

+

{{ t "Fever API endpoint:" }} {{ rootURL }}{{ route "feverEndpoint" }}

Pinboard

@@ -120,7 +120,7 @@

{{ t "This special link allows you to subscribe to a website directly by using a bookmark in your web browser." }}

-