feat(equalize): Enhance EqualizeConfig to support external API URL
- Added ExternalAPIURL to EqualizeConfig for public EQ interface usage. - Updated loadEqualize function to initialize ExternalAPIURL from environment variable or default value. - Refactored reqEqualize method to accept apiURL as a parameter for improved flexibility. - Introduced eqAPIURL method to determine the appropriate API URL based on the source.
This commit is contained in:
@@ -2,19 +2,32 @@ package config
|
|||||||
|
|
||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
|
const defaultExternalEQAPIURL = "https://autoeq.app/equalize"
|
||||||
|
|
||||||
type EqualizeConfig struct {
|
type EqualizeConfig struct {
|
||||||
APIURL string
|
APIURL string // 内网 EQ 接口(正式环境 Eafonyoung 源使用)
|
||||||
|
ExternalAPIURL string // 公网 EQ 接口(正式环境非 Eafonyoung 源使用)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadEqualize(env string) EqualizeConfig {
|
func loadEqualize(env string) EqualizeConfig {
|
||||||
|
external := getEnv("EQ_EXTERNAL_API_URL", defaultExternalEQAPIURL)
|
||||||
if os.Getenv("EQ_API_URL") != "" {
|
if os.Getenv("EQ_API_URL") != "" {
|
||||||
return EqualizeConfig{APIURL: os.Getenv("EQ_API_URL")}
|
return EqualizeConfig{
|
||||||
|
APIURL: os.Getenv("EQ_API_URL"),
|
||||||
|
ExternalAPIURL: external,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch env {
|
switch env {
|
||||||
case "production":
|
case "production":
|
||||||
return EqualizeConfig{APIURL: "http://172.31.18.70:8000/equalize"}
|
return EqualizeConfig{
|
||||||
|
APIURL: "http://172.31.18.70:8000/equalize",
|
||||||
|
ExternalAPIURL: external,
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return EqualizeConfig{APIURL: "https://autoeq.app/equalize"}
|
return EqualizeConfig{
|
||||||
|
APIURL: defaultExternalEQAPIURL,
|
||||||
|
ExternalAPIURL: external,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -254,7 +254,8 @@ func (h *CurveHandler) getCurvePointFromPEQ(ctx context.Context, brand, name, ta
|
|||||||
bassBoost = map[string]any{"gain": 0, "fc": 100, "q": 0.7}
|
bassBoost = map[string]any{"gain": 0, "fc": 100, "q": 0.7}
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := h.reqEqualize(headPhone, measurement, targetParam, bassBoost, deref(m.Source), deref(m.Rig))
|
apiURL := h.eqAPIURL(deref(m.Source))
|
||||||
|
resp, err := h.reqEqualize(apiURL, headPhone, measurement, targetParam, bassBoost, deref(m.Source), deref(m.Rig))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -262,8 +263,16 @@ func (h *CurveHandler) getCurvePointFromPEQ(ctx context.Context, brand, name, ta
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// eqAPIURL 正式环境非 Eafonyoung 源走公网 EQ,其余走内网/默认地址
|
||||||
|
func (h *CurveHandler) eqAPIURL(source string) string {
|
||||||
|
if source != "Eafonyoung" && h.cfg.ExternalAPIURL != "" && h.cfg.ExternalAPIURL != h.cfg.APIURL {
|
||||||
|
return h.cfg.ExternalAPIURL
|
||||||
|
}
|
||||||
|
return h.cfg.APIURL
|
||||||
|
}
|
||||||
|
|
||||||
// reqEqualize 调用 autoeq API
|
// reqEqualize 调用 autoeq API
|
||||||
func (h *CurveHandler) reqEqualize(headPhone string, measurement map[string]any, target any, bassBoost map[string]any, source, rig string) (string, error) {
|
func (h *CurveHandler) reqEqualize(apiURL, headPhone string, measurement map[string]any, target any, bassBoost map[string]any, source, rig string) (string, error) {
|
||||||
gain := floatVal(bassBoost, "gain", 0)
|
gain := floatVal(bassBoost, "gain", 0)
|
||||||
fc := intVal(bassBoost, "fc", 100)
|
fc := intVal(bassBoost, "fc", 100)
|
||||||
q := floatVal(bassBoost, "q", 0.7)
|
q := floatVal(bassBoost, "q", 0.7)
|
||||||
@@ -317,7 +326,7 @@ func (h *CurveHandler) reqEqualize(headPhone string, measurement map[string]any,
|
|||||||
return "", fmt.Errorf("marshal eq request: %w", err)
|
return "", fmt.Errorf("marshal eq request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
httpReq, err := http.NewRequestWithContext(context.Background(), http.MethodPost, h.cfg.APIURL, bytes.NewReader(jsonData))
|
httpReq, err := http.NewRequestWithContext(context.Background(), http.MethodPost, apiURL, bytes.NewReader(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("create eq request: %w", err)
|
return "", fmt.Errorf("create eq request: %w", err)
|
||||||
}
|
}
|
||||||
@@ -330,7 +339,8 @@ func (h *CurveHandler) reqEqualize(headPhone string, measurement map[string]any,
|
|||||||
h.log.Info("eq api response",
|
h.log.Info("eq api response",
|
||||||
zap.String("headPhone", headPhone),
|
zap.String("headPhone", headPhone),
|
||||||
zap.Duration("latency", elapsed),
|
zap.Duration("latency", elapsed),
|
||||||
zap.String("url", h.cfg.APIURL),
|
zap.String("url", apiURL),
|
||||||
|
zap.String("source", source),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.log.Error("eq api request failed", zap.Duration("latency", elapsed), zap.Error(err))
|
h.log.Error("eq api request failed", zap.Duration("latency", elapsed), zap.Error(err))
|
||||||
|
|||||||
Reference in New Issue
Block a user