Skip to content

Commit

Permalink
fix final suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed May 9, 2022
1 parent 38bb43e commit c509a55
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion api/http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type errorResponse struct {

func handleErr(ctx context.Context, rw http.ResponseWriter, err error, status int) {
if status == http.StatusInternalServerError {
log.ErrorE(context.Background(), http.StatusText(status), err)
log.ErrorE(ctx, http.StatusText(status), err)
}

sendJSON(
Expand Down
6 changes: 3 additions & 3 deletions api/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type handler struct {
*chi.Mux
}

type ctxKey string
type ctxDB struct{}

// newHandler returns a handler with the router instantiated.
func newHandler(db client.DB) *handler {
Expand All @@ -36,7 +36,7 @@ func newHandler(db client.DB) *handler {

func (h *handler) handle(f http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
ctx := context.WithValue(req.Context(), ctxKey("DB"), h.db)
ctx := context.WithValue(req.Context(), ctxDB{}, h.db)
f(rw, req.WithContext(ctx))
}
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func sendJSON(ctx context.Context, rw http.ResponseWriter, v interface{}, code i
}

func dbFromContext(ctx context.Context) (client.DB, error) {
db, ok := ctx.Value(ctxKey("DB")).(client.DB)
db, ok := ctx.Value(ctxDB{}).(client.DB)
if !ok {
return nil, errors.New("no database available")
}
Expand Down
2 changes: 1 addition & 1 deletion api/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestDbFromContext(t *testing.T) {
t.Fatal(err)
}

reqCtx := context.WithValue(ctx, ctxKey("DB"), defra)
reqCtx := context.WithValue(ctx, ctxDB{}, defra)

_, err = dbFromContext(reqCtx)
assert.NoError(t, err)
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions api/http/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ func (lrw *loggingResponseWriter) WriteHeader(code int) {
}

func (lrw *loggingResponseWriter) Write(b []byte) (int, error) {
// used for chucked payloads. Content-Length should not be set
// for each chunk.
if lrw.ResponseWriter.Header().Get("Content-Length") != "" {
return lrw.ResponseWriter.Write(b)
}

lrw.contentLength = len(b)
lrw.ResponseWriter.Header().Set("Content-Length", strconv.Itoa(lrw.contentLength))
return lrw.ResponseWriter.Write(b)
Expand Down
9 changes: 6 additions & 3 deletions api/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ import (

// The Server struct holds the Handler for the HTTP API
type Server struct {
Handler http.Handler
http.Server
}

// NewServer instantiated a new server with the given http.Handler.
func NewServer(db client.DB) *Server {
return &Server{
Handler: newHandler(db),
http.Server{
Handler: newHandler(db),
},
}
}

// Listen calls ListenAndServe with our router.
func (s *Server) Listen(addr string) error {
return http.ListenAndServe(addr, s.Handler)
s.Addr = addr
return s.ListenAndServe()
}
20 changes: 17 additions & 3 deletions api/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,31 @@
package http

import (
"context"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewServerAndListen(t *testing.T) {
// @TODO: maybe it would be worth doing something a bit more thorough

// test with no config
s := NewServer(nil)
if ok := assert.NotNil(t, s); ok {
assert.Error(t, s.Listen(":303000"))
}

serverRunning := make(chan struct{})
serverDone := make(chan struct{})
go func() {
close(serverRunning)
err := s.Listen(":3131")
assert.ErrorIs(t, http.ErrServerClosed, err)
defer close(serverDone)
}()

<-serverRunning

s.Shutdown(context.Background())

<-serverDone
}

0 comments on commit c509a55

Please sign in to comment.