diff --git a/api/main.go b/api/main.go index 830fe79..f0ea824 100644 --- a/api/main.go +++ b/api/main.go @@ -7,13 +7,17 @@ import ( "github.com/safing/portbase/modules" ) +var ( + module *modules.Module +) + // API Errors var ( ErrAuthenticationAlreadySet = errors.New("the authentication function has already been set") ) func init() { - modules.Register("api", prep, start, stop, "base", "database", "config") + module = modules.Register("api", prep, start, stop, "base", "database", "config") } func prep() error { diff --git a/api/router.go b/api/router.go index bce3884..bed878e 100644 --- a/api/router.go +++ b/api/router.go @@ -1,8 +1,10 @@ package api import ( + "context" "net/http" "sync" + "time" "github.com/gorilla/mux" @@ -56,8 +58,20 @@ func Serve() { // start serving log.Infof("api: starting to listen on %s", server.Addr) - // TODO: retry if failed - log.Errorf("api: failed to listen on %s: %s", server.Addr, server.ListenAndServe()) + backoffDuration := 10 * time.Second + for { + // always returns an error + err := module.RunWorker("http endpoint", func(ctx context.Context) error { + return server.ListenAndServe() + }) + // return on shutdown error + if err == http.ErrServerClosed { + return + } + // log error and restart + log.Errorf("api: http endpoint failed: %s - restarting in %s", err, backoffDuration) + time.Sleep(backoffDuration) + } } // GetMuxVars wraps github.com/gorilla/mux.Vars in order to mitigate context key issues in multi-repo projects.