Skip to content

Commit

Permalink
refactor!: move api_url and base_path to config file
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 25, 2022
1 parent 03dbb3a commit 7c32af4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 23 deletions.
4 changes: 2 additions & 2 deletions internal/bootstrap/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
3 changes: 2 additions & 1 deletion internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
14 changes: 8 additions & 6 deletions server/common/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions server/static/config.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 4 additions & 14 deletions server/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 7c32af4

Please sign in to comment.