From f1665246f6f932c841d2f424c5fdb3f51136892e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Fri, 11 Jun 2021 20:07:04 +0200 Subject: [PATCH] feat: add update endpoint to free the API port --- pkg/api/api.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index b2279e1..a5ed210 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -3,6 +3,7 @@ package api import ( "fmt" log "github.com/sirupsen/logrus" + "golang.org/x/net/context" "net/http" ) @@ -10,16 +11,31 @@ const tokenMissingMsg = "api token is empty or has not been set. exiting" // API is the http server responsible for serving the HTTP API endpoints type API struct { - Token string - hasHandlers bool + Token string + hasHandlers bool + Server *http.Server + ShutdownContext *context.Context } // New is a factory function creating a new API instance func New(token string) *API { - return &API{ + + api := &API{ Token: token, hasHandlers: false, } + http.Handle("/v1/prepare-self-update", api.RequireToken(api.handlePrepareUpdate)) + return api +} + +func (api *API) handlePrepareUpdate(writer http.ResponseWriter, _ *http.Request) { + err := api.Server.Shutdown(context.Background()) + if err != http.ErrServerClosed && err != nil { + writer.WriteHeader(500) + _, _ = writer.Write([]byte(err.Error())) + } else { + writer.WriteHeader(201) + } } // RequireToken is wrapper around http.HandleFunc that checks token validity @@ -61,16 +77,19 @@ func (api *API) Start(block bool) error { log.Info("Watchtower HTTP API started.") if block { - runHTTPServer() + api.runHTTPServer() } else { go func() { - runHTTPServer() + api.runHTTPServer() }() } return nil } -func runHTTPServer() { +func (api *API) runHTTPServer() { log.Info("Serving HTTP") - log.Fatal(http.ListenAndServe(":8080", nil)) + api.Server = &http.Server{Addr: ":8080", Handler: nil} + if err := api.Server.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatal(err) + } }