fix(accountsvc): guard nil agent registry on startup
This commit is contained in:
parent
ea3d6aec06
commit
d59a8a0415
17
api/api.go
17
api/api.go
@ -13,6 +13,7 @@ import (
|
||||
"math/big"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@ -220,10 +221,26 @@ func WithOAuthFrontendURL(url string) Option {
|
||||
// WithAgentRegistry configures the handler with the provided agent registry.
|
||||
func WithAgentRegistry(registry agentRegistry) Option {
|
||||
return func(h *handler) {
|
||||
if isNilAgentRegistry(registry) {
|
||||
return
|
||||
}
|
||||
h.agentRegistry = registry
|
||||
}
|
||||
}
|
||||
|
||||
func isNilAgentRegistry(registry agentRegistry) bool {
|
||||
if registry == nil {
|
||||
return true
|
||||
}
|
||||
value := reflect.ValueOf(registry)
|
||||
switch value.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
return value.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// WithGormDB configures the handler with the provided GORM database for admin settings.
|
||||
func WithGormDB(db *gorm.DB) Option {
|
||||
return func(h *handler) {
|
||||
|
||||
@ -113,6 +113,17 @@ func extractVerificationCodeFromMessage(t *testing.T, msg capturedEmail) string
|
||||
return ""
|
||||
}
|
||||
|
||||
func TestWithAgentRegistry_IgnoresTypedNil(t *testing.T) {
|
||||
var registry *agentserver.Registry
|
||||
h := &handler{}
|
||||
|
||||
WithAgentRegistry(registry)(h)
|
||||
|
||||
if h.agentRegistry != nil {
|
||||
t.Fatalf("expected nil agent registry, got %T", h.agentRegistry)
|
||||
}
|
||||
}
|
||||
|
||||
func decodeResponse(t *testing.T, rr *httptest.ResponseRecorder) apiResponse {
|
||||
t.Helper()
|
||||
var resp apiResponse
|
||||
|
||||
@ -841,10 +841,12 @@ func runServer(ctx context.Context, cfg *config.Config, logger *slog.Logger) err
|
||||
options = append(options, api.WithGormDB(gormDB))
|
||||
|
||||
// Pre-load sandbox bindings from database into the registry
|
||||
var sandboxBindings []model.SandboxBinding
|
||||
if err := gormDB.Find(&sandboxBindings).Error; err == nil {
|
||||
for _, b := range sandboxBindings {
|
||||
agentRegistry.SetSandboxAgent(b.AgentID, true)
|
||||
if agentRegistry != nil {
|
||||
var sandboxBindings []model.SandboxBinding
|
||||
if err := gormDB.Find(&sandboxBindings).Error; err == nil {
|
||||
for _, b := range sandboxBindings {
|
||||
agentRegistry.SetSandboxAgent(b.AgentID, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user