From c7d01b459363289329133edf2cf8bf4226a182d4 Mon Sep 17 00:00:00 2001 From: Pijus Kamandulis Date: Thu, 14 Nov 2024 18:42:17 +0200 Subject: [PATCH] Fix cosmos explorer incorrect redirect --- api/config/config.go | 6 ++++-- api/config/models.go | 21 ++++++++++--------- api/handlers/explorer.go | 2 +- api/handlers/middleware/authentication.go | 2 +- .../middleware/strip_trailing_slashes.go | 5 ++++- api/tests/config_test.go | 1 + 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/api/config/config.go b/api/config/config.go index 508e28e..f74dc9b 100644 --- a/api/config/config.go +++ b/api/config/config.go @@ -8,8 +8,9 @@ import ( ) const ( - DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" - EnvPrefix = "COSMIUM_" + DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" + EnvPrefix = "COSMIUM_" + ExplorerBaseUrlLocation = "/_explorer" ) var Config = ServerConfig{} @@ -45,6 +46,7 @@ func ParseFlags() { Config.DatabaseDomain = Config.Host Config.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", Config.Host, Config.Port) Config.AccountKey = *accountKey + Config.ExplorerBaseUrlLocation = ExplorerBaseUrlLocation } func setFlagsFromEnvironment() (err error) { diff --git a/api/config/models.go b/api/config/models.go index 5bc1310..9679ab6 100644 --- a/api/config/models.go +++ b/api/config/models.go @@ -6,14 +6,15 @@ type ServerConfig struct { DatabaseEndpoint string AccountKey string - ExplorerPath string - Port int - Host string - TLS_CertificatePath string - TLS_CertificateKey string - InitialDataFilePath string - PersistDataFilePath string - DisableAuth bool - DisableTls bool - Debug bool + ExplorerPath string + Port int + Host string + TLS_CertificatePath string + TLS_CertificateKey string + InitialDataFilePath string + PersistDataFilePath string + DisableAuth bool + DisableTls bool + Debug bool + ExplorerBaseUrlLocation string } diff --git a/api/handlers/explorer.go b/api/handlers/explorer.go index 999fbd3..65393f5 100644 --- a/api/handlers/explorer.go +++ b/api/handlers/explorer.go @@ -8,7 +8,7 @@ import ( ) func RegisterExplorerHandlers(router *gin.Engine) { - explorer := router.Group("/_explorer") + explorer := router.Group(config.Config.ExplorerBaseUrlLocation) { explorer.Use(func(ctx *gin.Context) { if ctx.Param("filepath") == "/config.json" { diff --git a/api/handlers/middleware/authentication.go b/api/handlers/middleware/authentication.go index f8965c2..4c95b9d 100644 --- a/api/handlers/middleware/authentication.go +++ b/api/handlers/middleware/authentication.go @@ -14,7 +14,7 @@ func Authentication() gin.HandlerFunc { return func(c *gin.Context) { requestUrl := c.Request.URL.String() if config.Config.DisableAuth || - strings.HasPrefix(requestUrl, "/_explorer") || + strings.HasPrefix(requestUrl, config.Config.ExplorerBaseUrlLocation) || strings.HasPrefix(requestUrl, "/cosmium") { return } diff --git a/api/handlers/middleware/strip_trailing_slashes.go b/api/handlers/middleware/strip_trailing_slashes.go index ff26ed0..18c091d 100644 --- a/api/handlers/middleware/strip_trailing_slashes.go +++ b/api/handlers/middleware/strip_trailing_slashes.go @@ -1,13 +1,16 @@ package middleware import ( + "strings" + "github.com/gin-gonic/gin" + "github.com/pikami/cosmium/api/config" ) func StripTrailingSlashes(r *gin.Engine) gin.HandlerFunc { return func(c *gin.Context) { path := c.Request.URL.Path - if len(path) > 1 && path[len(path)-1] == '/' { + if len(path) > 1 && path[len(path)-1] == '/' && !strings.Contains(path, config.Config.ExplorerBaseUrlLocation) { c.Request.URL.Path = path[:len(path)-1] r.HandleContext(c) c.Abort() diff --git a/api/tests/config_test.go b/api/tests/config_test.go index ece57dc..065ecc3 100644 --- a/api/tests/config_test.go +++ b/api/tests/config_test.go @@ -10,6 +10,7 @@ import ( func runTestServer() *httptest.Server { config.Config.AccountKey = config.DefaultAccountKey config.Config.ExplorerPath = "/tmp/nothing" + config.Config.ExplorerBaseUrlLocation = config.ExplorerBaseUrlLocation return httptest.NewServer(api.CreateRouter()) }