mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 13:00:52 +08:00
refactor(config): consolidate Amp settings into AmpCode struct
This commit is contained in:
@@ -95,7 +95,8 @@ func (m *AmpModule) Name() string {
|
||||
// This implements the RouteModuleV2 interface with Context.
|
||||
// Routes are registered only once via sync.Once for idempotent behavior.
|
||||
func (m *AmpModule) Register(ctx modules.Context) error {
|
||||
upstreamURL := strings.TrimSpace(ctx.Config.AmpUpstreamURL)
|
||||
settings := ctx.Config.AmpCode
|
||||
upstreamURL := strings.TrimSpace(settings.UpstreamURL)
|
||||
|
||||
// Determine auth middleware (from module or context)
|
||||
auth := m.getAuthMiddleware(ctx)
|
||||
@@ -104,7 +105,7 @@ func (m *AmpModule) Register(ctx modules.Context) error {
|
||||
var regErr error
|
||||
m.registerOnce.Do(func() {
|
||||
// Initialize model mapper from config (for routing unavailable models to alternatives)
|
||||
m.modelMapper = NewModelMapper(ctx.Config.AmpModelMappings)
|
||||
m.modelMapper = NewModelMapper(settings.ModelMappings)
|
||||
|
||||
// Always register provider aliases - these work without an upstream
|
||||
m.registerProviderAliases(ctx.Engine, ctx.BaseHandler, auth)
|
||||
@@ -120,7 +121,7 @@ func (m *AmpModule) Register(ctx modules.Context) error {
|
||||
// Create secret source with precedence: config > env > file
|
||||
// Cache secrets for 5 minutes to reduce file I/O
|
||||
if m.secretSource == nil {
|
||||
m.secretSource = NewMultiSourceSecret(ctx.Config.AmpUpstreamAPIKey, 0 /* default 5min */)
|
||||
m.secretSource = NewMultiSourceSecret(settings.UpstreamAPIKey, 0 /* default 5min */)
|
||||
}
|
||||
|
||||
// Create reverse proxy with gzip handling via ModifyResponse
|
||||
@@ -136,7 +137,7 @@ func (m *AmpModule) Register(ctx modules.Context) error {
|
||||
// Register management proxy routes (requires upstream)
|
||||
// Restrict to localhost by default for security (prevents drive-by browser attacks)
|
||||
handler := proxyHandler(proxy)
|
||||
m.registerManagementRoutes(ctx.Engine, ctx.BaseHandler, handler, ctx.Config.AmpRestrictManagementToLocalhost)
|
||||
m.registerManagementRoutes(ctx.Engine, ctx.BaseHandler, handler, settings.RestrictManagementToLocalhost)
|
||||
|
||||
log.Infof("Amp upstream proxy enabled for: %s", upstreamURL)
|
||||
log.Debug("Amp provider alias routes registered")
|
||||
@@ -166,8 +167,9 @@ func (m *AmpModule) getAuthMiddleware(ctx modules.Context) gin.HandlerFunc {
|
||||
func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error {
|
||||
// Update model mappings (hot-reload supported)
|
||||
if m.modelMapper != nil {
|
||||
log.Infof("amp config updated: reloading %d model mapping(s)", len(cfg.AmpModelMappings))
|
||||
m.modelMapper.UpdateMappings(cfg.AmpModelMappings)
|
||||
settings := cfg.AmpCode
|
||||
log.Infof("amp config updated: reloading %d model mapping(s)", len(settings.ModelMappings))
|
||||
m.modelMapper.UpdateMappings(settings.ModelMappings)
|
||||
} else {
|
||||
log.Warnf("amp model mapper not initialized, skipping model mapping update")
|
||||
}
|
||||
@@ -177,7 +179,7 @@ func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
upstreamURL := strings.TrimSpace(cfg.AmpUpstreamURL)
|
||||
upstreamURL := strings.TrimSpace(cfg.AmpCode.UpstreamURL)
|
||||
if upstreamURL == "" {
|
||||
log.Warn("Amp upstream URL removed from config, restart required to disable")
|
||||
return nil
|
||||
|
||||
@@ -56,8 +56,10 @@ func TestAmpModule_Register_WithUpstream(t *testing.T) {
|
||||
m := NewLegacy(accessManager, func(c *gin.Context) { c.Next() })
|
||||
|
||||
cfg := &config.Config{
|
||||
AmpUpstreamURL: upstream.URL,
|
||||
AmpUpstreamAPIKey: "test-key",
|
||||
AmpCode: config.AmpCode{
|
||||
UpstreamURL: upstream.URL,
|
||||
UpstreamAPIKey: "test-key",
|
||||
},
|
||||
}
|
||||
|
||||
ctx := modules.Context{Engine: r, BaseHandler: base, Config: cfg, AuthMiddleware: func(c *gin.Context) { c.Next() }}
|
||||
@@ -86,7 +88,9 @@ func TestAmpModule_Register_WithoutUpstream(t *testing.T) {
|
||||
m := NewLegacy(accessManager, func(c *gin.Context) { c.Next() })
|
||||
|
||||
cfg := &config.Config{
|
||||
AmpUpstreamURL: "", // No upstream
|
||||
AmpCode: config.AmpCode{
|
||||
UpstreamURL: "", // No upstream
|
||||
},
|
||||
}
|
||||
|
||||
ctx := modules.Context{Engine: r, BaseHandler: base, Config: cfg, AuthMiddleware: func(c *gin.Context) { c.Next() }}
|
||||
@@ -121,7 +125,9 @@ func TestAmpModule_Register_InvalidUpstream(t *testing.T) {
|
||||
m := NewLegacy(accessManager, func(c *gin.Context) { c.Next() })
|
||||
|
||||
cfg := &config.Config{
|
||||
AmpUpstreamURL: "://invalid-url",
|
||||
AmpCode: config.AmpCode{
|
||||
UpstreamURL: "://invalid-url",
|
||||
},
|
||||
}
|
||||
|
||||
ctx := modules.Context{Engine: r, BaseHandler: base, Config: cfg, AuthMiddleware: func(c *gin.Context) { c.Next() }}
|
||||
@@ -151,7 +157,7 @@ func TestAmpModule_OnConfigUpdated_CacheInvalidation(t *testing.T) {
|
||||
}
|
||||
|
||||
// Update config - should invalidate cache
|
||||
if err := m.OnConfigUpdated(&config.Config{AmpUpstreamURL: "http://x"}); err != nil {
|
||||
if err := m.OnConfigUpdated(&config.Config{AmpCode: config.AmpCode{UpstreamURL: "http://x"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -175,7 +181,7 @@ func TestAmpModule_OnConfigUpdated_URLRemoved(t *testing.T) {
|
||||
m.secretSource = ms
|
||||
|
||||
// Config update with empty URL - should log warning but not error
|
||||
cfg := &config.Config{AmpUpstreamURL: ""}
|
||||
cfg := &config.Config{AmpCode: config.AmpCode{UpstreamURL: ""}}
|
||||
|
||||
if err := m.OnConfigUpdated(cfg); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
@@ -187,7 +193,7 @@ func TestAmpModule_OnConfigUpdated_NonMultiSourceSecret(t *testing.T) {
|
||||
m := &AmpModule{enabled: true}
|
||||
m.secretSource = NewStaticSecretSource("static-key")
|
||||
|
||||
cfg := &config.Config{AmpUpstreamURL: "http://example.com"}
|
||||
cfg := &config.Config{AmpCode: config.AmpCode{UpstreamURL: "http://example.com"}}
|
||||
|
||||
// Should not error or panic
|
||||
if err := m.OnConfigUpdated(cfg); err != nil {
|
||||
@@ -240,8 +246,10 @@ func TestAmpModule_SecretSource_FromConfig(t *testing.T) {
|
||||
|
||||
// Config with explicit API key
|
||||
cfg := &config.Config{
|
||||
AmpUpstreamURL: upstream.URL,
|
||||
AmpUpstreamAPIKey: "config-key",
|
||||
AmpCode: config.AmpCode{
|
||||
UpstreamURL: upstream.URL,
|
||||
UpstreamAPIKey: "config-key",
|
||||
},
|
||||
}
|
||||
|
||||
ctx := modules.Context{Engine: r, BaseHandler: base, Config: cfg, AuthMiddleware: func(c *gin.Context) { c.Next() }}
|
||||
@@ -283,7 +291,7 @@ func TestAmpModule_ProviderAliasesAlwaysRegistered(t *testing.T) {
|
||||
|
||||
m := NewLegacy(accessManager, func(c *gin.Context) { c.Next() })
|
||||
|
||||
cfg := &config.Config{AmpUpstreamURL: scenario.configURL}
|
||||
cfg := &config.Config{AmpCode: config.AmpCode{UpstreamURL: scenario.configURL}}
|
||||
|
||||
ctx := modules.Context{Engine: r, BaseHandler: base, Config: cfg, AuthMiddleware: func(c *gin.Context) { c.Next() }}
|
||||
if err := m.Register(ctx); err != nil && scenario.configURL != "" {
|
||||
|
||||
Reference in New Issue
Block a user