mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-07-18 17:38:07 +08:00

* chore: standardize context keys with custom ContextKey type * fix bug * 使用Request.Context
37 lines
732 B
Go
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
|
|
}
|