easyai-ai-gateway/apps/api/internal/httpapi/desktop_config_handlers.go

56 lines
1.4 KiB
Go

package httpapi
import (
"net/http"
"strings"
)
type desktopBillingConfigResponse struct {
GatewayBaseURL string `json:"gatewayBaseUrl,omitempty"`
GatewayBillingPath string `json:"gatewayBillingPath"`
}
type desktopConfigResponse struct {
Billing desktopBillingConfigResponse `json:"billing"`
}
// getDesktopConfig godoc
// @Summary 获取桌面端配置
// @Description 返回桌面端需要的 Gateway Web 账单入口配置。
// @Tags workspace
// @Produce json
// @Security BearerAuth
// @Success 200 {object} desktopConfigResponse
// @Failure 401 {object} ErrorEnvelope
// @Router /api/workspace/desktop-config [get]
func (s *Server) getDesktopConfig(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, desktopConfigResponse{
Billing: desktopBillingConfigResponse{
GatewayBaseURL: firstNonEmpty(
strings.TrimRight(strings.TrimSpace(s.cfg.WebBaseURL), "/"),
strings.TrimRight(strings.TrimSpace(s.cfg.PublicBaseURL), "/"),
requestOrigin(r),
),
GatewayBillingPath: "/workspace/billing",
},
})
}
func requestOrigin(r *http.Request) string {
host := strings.TrimSpace(r.Host)
if host == "" {
return ""
}
proto := strings.TrimSpace(r.Header.Get("X-Forwarded-Proto"))
if proto == "" {
proto = "http"
}
if comma := strings.Index(proto, ","); comma >= 0 {
proto = strings.TrimSpace(proto[:comma])
}
if proto == "" {
proto = "http"
}
return proto + "://" + host
}