mirror of
https://github.com/khairul169/garage-webui.git
synced 2026-09-15 00:43:21 +07:00
feat: add base path configuration
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
#
|
||||
BASE_PATH=""
|
||||
AUTH_USER_PASS='username:$2y$10$DSTi9o0uQPEHSNlf66xMEOgm9KgVNBP3vHxA3SK0Xha2EVMb3mTXm'
|
||||
API_BASE_URL="http://garage:3903"
|
||||
S3_ENDPOINT_URL="http://garage:3900"
|
||||
API_ADMIN_KEY=""
|
||||
+13
-1
@@ -7,6 +7,7 @@ import (
|
||||
"khairul169/garage-webui/utils"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
@@ -21,10 +22,21 @@ func main() {
|
||||
log.Println("Cannot load garage config!", err)
|
||||
}
|
||||
|
||||
basePath := os.Getenv("BASE_PATH")
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/api/", http.StripPrefix("/api", router.HandleApiRouter()))
|
||||
|
||||
// Serve API
|
||||
apiPrefix := basePath + "/api"
|
||||
mux.Handle(apiPrefix+"/", http.StripPrefix(apiPrefix, router.HandleApiRouter()))
|
||||
|
||||
// Static files
|
||||
ui.ServeUI(mux)
|
||||
|
||||
// Redirect to UI if BASE_PATH is set
|
||||
if basePath != "" {
|
||||
mux.Handle("/", http.RedirectHandler(basePath, http.StatusMovedPermanently))
|
||||
}
|
||||
|
||||
host := utils.GetEnv("HOST", "0.0.0.0")
|
||||
port := utils.GetEnv("PORT", "3909")
|
||||
|
||||
|
||||
+32
-3
@@ -7,7 +7,10 @@ import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed dist
|
||||
@@ -16,19 +19,45 @@ var embeddedFs embed.FS
|
||||
func ServeUI(mux *http.ServeMux) {
|
||||
distFs, _ := fs.Sub(embeddedFs, "dist")
|
||||
fileServer := http.FileServer(http.FS(distFs))
|
||||
basePath := os.Getenv("BASE_PATH")
|
||||
|
||||
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mux.Handle(basePath+"/", http.StripPrefix(basePath, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_path := path.Clean(r.URL.Path)[1:]
|
||||
|
||||
// Rewrite non-existing paths to index.html
|
||||
if _, err := fs.Stat(distFs, _path); err != nil {
|
||||
index, _ := fs.ReadFile(distFs, "index.html")
|
||||
html := string(index)
|
||||
|
||||
// Set base path for the UI
|
||||
html = strings.ReplaceAll(html, "%BASE_PATH%", basePath)
|
||||
html = addBasePath(html, basePath)
|
||||
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(index)
|
||||
w.Write([]byte(html))
|
||||
return
|
||||
}
|
||||
|
||||
// Add prefix to each /assets strings in js
|
||||
if len(basePath) > 0 && strings.HasSuffix(_path, ".js") {
|
||||
data, _ := fs.ReadFile(distFs, _path)
|
||||
html := string(data)
|
||||
html = strings.ReplaceAll(html, "assets/", basePath[1:]+"/assets/")
|
||||
|
||||
w.Header().Add("Content-Type", "text/javascript")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(html))
|
||||
return
|
||||
}
|
||||
|
||||
fileServer.ServeHTTP(w, r)
|
||||
}))
|
||||
})))
|
||||
}
|
||||
|
||||
func addBasePath(html string, basePath string) string {
|
||||
re := regexp.MustCompile(`(href|src)=["'](/[^"'>]+)["']`)
|
||||
return re.ReplaceAllStringFunc(html, func(match string) string {
|
||||
return re.ReplaceAllString(match, `$1="`+basePath+`$2"`)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user