From 46935a7a1298ebeba11dc71095115ccd41850529 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Tue, 10 Aug 2021 12:12:05 -0500 Subject: [PATCH] Fix broken path prefix --- pkg/api/api.go | 18 +++++++++++++----- pkg/api/routes.go | 5 +++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index d51132e..6c07e88 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -3,7 +3,6 @@ package api import ( "encoding/base64" "fmt" - "github.com/sosedoff/pgweb/static" "net/http" neturl "net/url" "regexp" @@ -18,6 +17,7 @@ import ( "github.com/sosedoff/pgweb/pkg/command" "github.com/sosedoff/pgweb/pkg/connection" "github.com/sosedoff/pgweb/pkg/shared" + "github.com/sosedoff/pgweb/static" ) var ( @@ -58,12 +58,20 @@ func setClient(c *gin.Context, newClient *client.Client) error { } // GetHome renderes the home page -func GetHome() http.Handler { - return http.FileServer(http.FS(static.Static)) +func GetHome(prefix string) http.Handler { + if prefix != "" { + prefix = "/" + prefix + } + return http.StripPrefix(prefix, http.FileServer(http.FS(static.Static))) } -func GetAssets() http.Handler { - return http.StripPrefix("/static/", http.FileServer(http.FS(static.Static))) +func GetAssets(prefix string) http.Handler { + if prefix != "" { + prefix = "/" + prefix + "static/" + } else { + prefix = "/static/" + } + return http.StripPrefix(prefix, http.FileServer(http.FS(static.Static))) } // GetSessions renders the number of active sessions diff --git a/pkg/api/routes.go b/pkg/api/routes.go index ab808ca..c2b1fbf 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -2,6 +2,7 @@ package api import ( "github.com/gin-gonic/gin" + "github.com/sosedoff/pgweb/pkg/command" ) @@ -20,8 +21,8 @@ func SetupMiddlewares(group *gin.RouterGroup) { func SetupRoutes(router *gin.Engine) { root := router.Group(command.Opts.Prefix) - root.GET("/", gin.WrapH(GetHome())) - root.GET("/static/*path", gin.WrapH(GetAssets())) + root.GET("/", gin.WrapH(GetHome(command.Opts.Prefix))) + root.GET("/static/*path", gin.WrapH(GetAssets(command.Opts.Prefix))) root.GET("/connect/:resource", ConnectWithBackend) api := root.Group("/api")