diff --git a/internal/server/input.go b/internal/server/input.go index c366f39..f3e94c4 100644 --- a/internal/server/input.go +++ b/internal/server/input.go @@ -1,6 +1,7 @@ package server import ( + "context" "encoding/json" "errors" "github.com/dependabot/cli/internal/model" @@ -10,30 +11,29 @@ import ( ) type credServer struct { - listener net.Listener - data *model.Input + server *http.Server + data *model.Input } // the server receives one payload and shuts itself down func (s *credServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { - log.Println("received input") if err := json.NewDecoder(r.Body).Decode(&s.data); err != nil { panic(err) } w.WriteHeader(200) _ = r.Body.Close() - _ = s.listener.Close() - log.Println("shutting down server") + _ = s.server.Shutdown(context.Background()) } // Input receives configuration via HTTP on the port and returns it decoded func Input(listener net.Listener) (*model.Input, error) { - handler := &credServer{ - listener: listener, - } + handler := &credServer{} + srv := &http.Server{Handler: handler} + handler.server = srv + // printing so the user doesn't think the cli is hanging log.Println("waiting for input on", listener.Addr()) - if err := http.Serve(listener, handler); err != nil && !errors.Is(err, net.ErrClosed) { + if err := srv.Serve(listener); err != nil && !errors.Is(err, net.ErrClosed) { return nil, err } return handler.data, nil diff --git a/internal/server/input_test.go b/internal/server/input_test.go index 06ad867..f7cf27d 100644 --- a/internal/server/input_test.go +++ b/internal/server/input_test.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "github.com/dependabot/cli/internal/model" - "log" "net" "net/http" "os" @@ -30,7 +29,6 @@ func TestInput(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - log.Println("sending input on channel") inputCh <- input }()