diff --git a/api/main.go b/api/main.go index 68172c8..b51e164 100644 --- a/api/main.go +++ b/api/main.go @@ -1,7 +1,6 @@ package api import ( - "context" "encoding/json" "errors" "flag" @@ -58,7 +57,7 @@ func prep() error { } func start() error { - go Serve() + startServer() _ = updateAPIKeys(module.Ctx, nil) err := module.RegisterEventHook("config", "config change", "update API keys", updateAPIKeys) @@ -75,10 +74,7 @@ func start() error { } func stop() error { - if server != nil { - return server.Shutdown(context.Background()) - } - return nil + return stopServer() } func exportEndpointsCmd() error { diff --git a/api/router.go b/api/router.go index c12d20b..3cb4cc6 100644 --- a/api/router.go +++ b/api/router.go @@ -18,6 +18,9 @@ import ( "github.com/safing/portbase/utils" ) +// EnableServer defines if the HTTP server should be started. +const EnableServer = true + var ( // mainMux is the main mux router. mainMux = mux.NewRouter() @@ -48,15 +51,38 @@ func RegisterHandleFunc(path string, handleFunc func(http.ResponseWriter, *http. return mainMux.HandleFunc(path, handleFunc) } -// Serve starts serving the API endpoint. -func Serve() { - // configure server +func startServer() { + // Check if server is enabled. + if !EnableServer { + return + } + + // Configure server. server.Addr = listenAddressConfig() server.Handler = &mainHandler{ // TODO: mainMux should not be modified anymore. mux: mainMux, } + // Start server manager. + module.StartServiceWorker("http server manager", 0, serverManager) +} + +func stopServer() error { + // Check if server is enabled. + if !EnableServer { + return nil + } + + if server.Addr != "" { + return server.Shutdown(context.Background()) + } + + return nil +} + +// Serve starts serving the API endpoint. +func serverManager(_ context.Context) error { // start serving log.Infof("api: starting to listen on %s", server.Addr) backoffDuration := 10 * time.Second @@ -67,7 +93,7 @@ func Serve() { }) // return on shutdown error if errors.Is(err, http.ErrServerClosed) { - return + return nil } // log error and restart log.Errorf("api: http endpoint failed: %s - restarting in %s", err, backoffDuration)