File size: 732 Bytes
619f93d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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
}
|