refactor!: move `api_url` and `base_path` to config file

pull/1831/head
Noah Hsu 2022-09-25 17:57:54 +08:00
parent 03dbb3a403
commit 7c32af4649
5 changed files with 52 additions and 23 deletions

View File

@ -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},

View File

@ -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"`

View File

@ -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

36
server/static/config.go Normal file
View File

@ -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
}

View File

@ -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 {