From 7c32af4649909dd7ebcafe2b58ed44c311223759 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Sun, 25 Sep 2022 17:57:54 +0800 Subject: [PATCH] refactor!: move `api_url` and `base_path` to config file --- internal/bootstrap/data/setting.go | 4 ++-- internal/conf/config.go | 3 ++- server/common/base.go | 14 +++++++----- server/static/config.go | 36 ++++++++++++++++++++++++++++++ server/static/static.go | 18 ++++----------- 5 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 server/static/config.go diff --git a/internal/bootstrap/data/setting.go b/internal/bootstrap/data/setting.go index 38b6df49..05cc9b77 100644 --- a/internal/bootstrap/data/setting.go +++ b/internal/bootstrap/data/setting.go @@ -70,8 +70,8 @@ func InitialSettings() []model.SettingItem { initialSettingItems = []model.SettingItem{ // site settings {Key: conf.VERSION, Value: conf.Version, Type: conf.TypeString, Group: model.SITE, Flag: model.READONLY}, - {Key: conf.ApiUrl, Value: "", Type: conf.TypeString, Group: model.SITE}, - {Key: conf.BasePath, Value: "", Type: conf.TypeString, Group: model.SITE}, + //{Key: conf.ApiUrl, Value: "", Type: conf.TypeString, Group: model.SITE}, + //{Key: conf.BasePath, Value: "", Type: conf.TypeString, Group: model.SITE}, {Key: conf.SiteTitle, Value: "AList", Type: conf.TypeString, Group: model.SITE}, {Key: conf.Announcement, Value: "### repo\nhttps://github.com/alist-org/alist", Type: conf.TypeText, Group: model.SITE}, {Key: "pagination_type", Value: "all", Type: conf.TypeSelect, Options: "all,pagination,load_more,auto_load_more", Group: model.SITE}, diff --git a/internal/conf/config.go b/internal/conf/config.go index 96306cc0..ede490a9 100644 --- a/internal/conf/config.go +++ b/internal/conf/config.go @@ -35,8 +35,9 @@ type Config struct { Force bool `json:"force" env:"FORCE"` Address string `json:"address" env:"ADDR"` Port int `json:"port" env:"PORT"` - JwtSecret string `json:"jwt_secret" env:"JWT_SECRET"` + SiteURL string `json:"site_url" env:"SITE_URL"` Cdn string `json:"cdn" env:"CDN"` + JwtSecret string `json:"jwt_secret" env:"JWT_SECRET"` Database Database `json:"database"` Scheme Scheme `json:"scheme"` TempDir string `json:"temp_dir" env:"TEMP_DIR"` diff --git a/server/common/base.go b/server/common/base.go index 406176e0..c8bf6143 100644 --- a/server/common/base.go +++ b/server/common/base.go @@ -10,15 +10,17 @@ import ( ) func GetApiUrl(r *http.Request) string { - api := setting.GetStr(conf.ApiUrl) - protocol := "http" - if r != nil { + api := conf.Conf.SiteURL + if api == "" { + api = setting.GetStr(conf.ApiUrl) + } + if r != nil && api == "" { + protocol := "http" if r.TLS != nil { protocol = "https" } - if api == "" { - api = fmt.Sprintf("%s://%s", protocol, r.Host) - } + api = fmt.Sprintf("%s://%s", protocol, r.Host) + } strings.TrimSuffix(api, "/") return api diff --git a/server/static/config.go b/server/static/config.go new file mode 100644 index 00000000..37746ae2 --- /dev/null +++ b/server/static/config.go @@ -0,0 +1,36 @@ +package static + +import ( + stdpath "path" + "strings" + + "github.com/alist-org/alist/v3/internal/conf" + "github.com/alist-org/alist/v3/internal/setting" + "github.com/alist-org/alist/v3/pkg/utils" +) + +type SiteConfig struct { + ApiURL string + BasePath string + Cdn string +} + +func getSiteConfig() SiteConfig { + siteConfig := SiteConfig{ + ApiURL: conf.Conf.SiteURL, + BasePath: stdpath.Base(conf.Conf.SiteURL), + Cdn: strings.ReplaceAll(strings.TrimSuffix(conf.Conf.Cdn, "/"), "$version", conf.WebVersion), + } + // try to get old config + if siteConfig.ApiURL == "" { + siteConfig.ApiURL = setting.GetStr(conf.ApiUrl) + siteConfig.BasePath = setting.GetStr(conf.BasePath) + } + if siteConfig.BasePath != "" { + siteConfig.BasePath = utils.StandardizePath(siteConfig.BasePath) + } + if siteConfig.Cdn == "" { + siteConfig.Cdn = siteConfig.BasePath + } + return siteConfig +} diff --git a/server/static/static.go b/server/static/static.go index 6d193d91..9f4260b3 100644 --- a/server/static/static.go +++ b/server/static/static.go @@ -10,7 +10,6 @@ import ( "github.com/alist-org/alist/v3/cmd/flags" "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/setting" - "github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/public" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" @@ -26,16 +25,7 @@ func InitIndex() { } func UpdateIndex() { - cdn := strings.TrimSuffix(conf.Conf.Cdn, "/") - cdn = strings.ReplaceAll(cdn, "$version", conf.WebVersion) - basePath := setting.GetStr(conf.BasePath) - if basePath != "" { - basePath = utils.StandardizePath(basePath) - } - if cdn == "" { - cdn = basePath - } - apiUrl := setting.GetStr(conf.ApiUrl) + siteConfig := getSiteConfig() favicon := setting.GetStr(conf.Favicon) title := setting.GetStr(conf.SiteTitle) customizeHead := setting.GetStr(conf.CustomizeHead) @@ -45,9 +35,9 @@ 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'", cdn), - "base_path: undefined": fmt.Sprintf("base_path: '%s'", basePath), - "api: undefined": fmt.Sprintf("api: '%s'", apiUrl), + "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 {