Files
OpenList/server/common/base.go
j2rong4cn 0c461991f9 chore: standardize context keys with custom ContextKey type (#697)
* chore: standardize context keys with custom ContextKey type

* fix bug

* 使用Request.Context
2025-07-14 23:55:17 +08:00

37 lines
732 B
Go

package common
import (
"context"
"fmt"
"net/http"
stdpath "path"
"strings"
"github.com/OpenListTeam/OpenList/v4/internal/conf"
)
func GetApiUrlFromRequest(r *http.Request) string {
api := conf.Conf.SiteURL
if strings.HasPrefix(api, "http") {
return strings.TrimSuffix(api, "/")
}
if r != nil {
protocol := "http"
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
protocol = "https"
}
host := r.Header.Get("X-Forwarded-Host")
if host == "" {
host = r.Host
}
api = fmt.Sprintf("%s://%s", protocol, stdpath.Join(host, api))
}
api = strings.TrimSuffix(api, "/")
return api
}
func GetApiUrl(ctx context.Context) string {
api, _ := ctx.Value(conf.ApiUrlKey).(string)
return api
}