alist/server/common/base.go

31 lines
595 B
Go
Raw Normal View History

2022-06-29 16:08:55 +08:00
package common
import (
"fmt"
"net/http"
stdpath "path"
2022-06-29 16:08:55 +08:00
"strings"
2022-08-03 14:26:59 +08:00
"github.com/alist-org/alist/v3/internal/conf"
2022-06-29 16:08:55 +08:00
)
2022-08-11 20:32:17 +08: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 20:32:17 +08: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 22:41:55 +08:00
}
2022-08-11 20:32:17 +08:00
strings.TrimSuffix(api, "/")
return api
2022-06-29 16:08:55 +08:00
}