alist/server/common/base.go

31 lines
601 B
Go
Raw Normal View History

2022-06-29 08:08:55 +00:00
package common
import (
"fmt"
"net/http"
stdpath "path"
2022-06-29 08:08:55 +00:00
"strings"
2022-08-03 06:26:59 +00:00
"github.com/alist-org/alist/v3/internal/conf"
2022-06-29 08:08:55 +00:00
)
2022-08-11 12:32:17 +00:00
func GetApiUrl(r *http.Request) string {
api := conf.Conf.SiteURL
if strings.HasPrefix(api, "http") {
return api
}
if r != nil {
protocol := "http"
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
2022-08-11 12:32:17 +00:00
protocol = "https"
}
host := r.Host
if r.Header.Get("X-Forwarded-Host") != "" {
host = r.Header.Get("X-Forwarded-Host")
}
api = fmt.Sprintf("%s://%s", protocol, stdpath.Join(host, api))
2022-06-30 14:41:55 +00:00
}
2023-02-22 12:20:28 +00:00
api = strings.TrimSuffix(api, "/")
2022-08-11 12:32:17 +00:00
return api
2022-06-29 08:08:55 +00:00
}