From ca6aebadef472d162cf5168d763f11c14d848247 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Thu, 12 Mar 2026 17:33:41 -0500 Subject: [PATCH] add channel for router to communicate config changes out --- api.go | 1 + router.go | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/api.go b/api.go index 8770895..a90a520 100644 --- a/api.go +++ b/api.go @@ -106,6 +106,7 @@ func (r *Router) handleConfigHTTP(w http.ResponseWriter, req *http.Request) { } w.Header().Set("Access-Control-Allow-Origin", "*") w.WriteHeader(http.StatusOK) + r.ConfigChange <- newConfig case http.MethodOptions: w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, OPTIONS") diff --git a/router.go b/router.go index 1716e86..5be194a 100644 --- a/router.go +++ b/router.go @@ -27,6 +27,7 @@ type Router struct { ModuleInstances map[string]module.Module // TODO(jwetzell): change to something easier to lookup RouteInstances []*route.Route + ConfigChange chan config.Config moduleWait sync.WaitGroup logger *slog.Logger runningConfig config.Config @@ -112,19 +113,20 @@ func (r *Router) getModule(moduleId string) module.Module { return moduleInstance } -func NewRouter(config config.Config) (*Router, []module.ModuleError, []route.RouteError) { +func NewRouter(routerConfig config.Config) (*Router, []module.ModuleError, []route.RouteError) { router := Router{ ModuleInstances: make(map[string]module.Module), RouteInstances: []*route.Route{}, + ConfigChange: make(chan config.Config, 1), logger: slog.Default().With("component", "router"), - runningConfig: config, + runningConfig: routerConfig, } router.logger.Debug("creating") var moduleErrors []module.ModuleError - for moduleIndex, moduleDecl := range config.Modules { + for moduleIndex, moduleDecl := range routerConfig.Modules { err := router.addModule(moduleDecl) if err != nil { @@ -142,7 +144,7 @@ func NewRouter(config config.Config) (*Router, []module.ModuleError, []route.Rou } var routeErrors []route.RouteError - for routeIndex, routeDecl := range config.Routes { + for routeIndex, routeDecl := range routerConfig.Routes { err := router.addRoute(routeDecl) if err != nil { if routeErrors == nil {