Add api for managing relays

This commit is contained in:
Jon Staab
2026-02-13 10:34:52 -08:00
parent 927a9e76e0
commit 616818386a
4 changed files with 1201 additions and 11 deletions
+32 -11
View File
@@ -19,18 +19,39 @@ func main() {
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
port := zooid.Env("PORT")
apiHost := zooid.Env("API_HOST")
apiWhitelist := zooid.Env("API_WHITELIST")
configDir := zooid.Env("CONFIG")
// Create the main handler
mainHandler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
instance, exists := zooid.Dispatch(r.Host)
if exists {
instance.Relay.ServeHTTP(w, r)
} else {
http.Error(w, "Not Found", http.StatusNotFound)
}
},
)
// Wrap with API handler if API_HOST is configured
var handler http.Handler = mainHandler
if apiHost != "" && apiWhitelist != "" {
apiHandler := zooid.NewAPIHandler(apiWhitelist, configDir)
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if this request is for the API host
if r.Host == apiHost {
apiHandler.ServeHTTP(w, r)
} else {
mainHandler.ServeHTTP(w, r)
}
})
}
srv := &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
instance, exists := zooid.Dispatch(r.Host)
if exists {
instance.Relay.ServeHTTP(w, r)
} else {
http.Error(w, "Not Found", http.StatusNotFound)
}
},
),
Addr: fmt.Sprintf(":%s", port),
Handler: handler,
}
go func() {