From b7479651e14a4562c0f4d375471171ef161487ed Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Tue, 27 Sep 2022 13:56:32 +0800 Subject: [PATCH] fix: incorrect base_path from site_url (close #1830) --- server/static/config.go | 8 ++++++-- server/static/static.go | 13 +++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/server/static/config.go b/server/static/config.go index 37746ae22b9..ebad8a6f4e6 100644 --- a/server/static/config.go +++ b/server/static/config.go @@ -1,7 +1,7 @@ package static import ( - stdpath "path" + "net/url" "strings" "github.com/alist-org/alist/v3/internal/conf" @@ -16,9 +16,13 @@ type SiteConfig struct { } func getSiteConfig() SiteConfig { + u, err := url.Parse(conf.Conf.SiteURL) + if err != nil { + utils.Log.Fatalf("can't parse site_url: %+v", err) + } siteConfig := SiteConfig{ ApiURL: conf.Conf.SiteURL, - BasePath: stdpath.Base(conf.Conf.SiteURL), + BasePath: u.Path, Cdn: strings.ReplaceAll(strings.TrimSuffix(conf.Conf.Cdn, "/"), "$version", conf.WebVersion), } // try to get old config diff --git a/server/static/static.go b/server/static/static.go index 9f4260b3c1a..7d566af501c 100644 --- a/server/static/static.go +++ b/server/static/static.go @@ -21,11 +21,19 @@ func InitIndex() { log.Fatalf("failed to read index.html: %v", err) } conf.RawIndexHtml = string(index) + siteConfig := getSiteConfig() + replaceMap := map[string]string{ + "cdn: undefined": fmt.Sprintf("cdn: '%s'", siteConfig.Cdn), + "base_path: undefined": fmt.Sprintf("base_path: '%s'", siteConfig.BasePath), + "api: undefined": fmt.Sprintf("api: '%s'", siteConfig.ApiURL), + } + for k, v := range replaceMap { + conf.RawIndexHtml = strings.Replace(conf.RawIndexHtml, k, v, 1) + } UpdateIndex() } func UpdateIndex() { - siteConfig := getSiteConfig() favicon := setting.GetStr(conf.Favicon) title := setting.GetStr(conf.SiteTitle) customizeHead := setting.GetStr(conf.CustomizeHead) @@ -35,9 +43,6 @@ func UpdateIndex() { replaceMap1 := map[string]string{ "https://jsd.nn.ci/gh/alist-org/logo@main/logo.svg": favicon, "Loading...": title, - "cdn: undefined": fmt.Sprintf("cdn: '%s'", siteConfig.Cdn), - "base_path: undefined": fmt.Sprintf("base_path: '%s'", siteConfig.BasePath), - "api: undefined": fmt.Sprintf("api: '%s'", siteConfig.ApiURL), "main_color: undefined": fmt.Sprintf("main_color: '%s'", mainColor), } for k, v := range replaceMap1 {