Skip to content

Commit

Permalink
back to using http.Server
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman committed Nov 17, 2023
1 parent aea95d8 commit d923849
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
18 changes: 9 additions & 9 deletions internal/server/input.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"context"
"encoding/json"
"errors"
"github.com/dependabot/cli/internal/model"
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions internal/server/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"github.com/dependabot/cli/internal/model"
"log"
"net"
"net/http"
"os"
Expand All @@ -30,7 +29,6 @@ func TestInput(t *testing.T) {
if err != nil {
t.Errorf(err.Error())
}
log.Println("sending input on channel")
inputCh <- input
}()

Expand Down

0 comments on commit d923849

Please sign in to comment.