influxdb/http
Jonathan A. Sternberg a7d5a89844
Merge pull request #36 from influxdata/js-http-error-handling
feat(http): add a utility function for reading http errors
2018-05-23 13:42:07 -05:00
..
README.md migrate(platform): move public dependencies into platform 2018-05-14 17:12:53 -04:00
auth_service.go feat(http): add a utility function for reading http errors 2018-05-23 13:29:48 -05:00
bucket_service.go feat(http): add a utility function for reading http errors 2018-05-23 13:29:48 -05:00
client.go migrate(platform): move public dependencies into platform 2018-05-14 17:12:53 -04:00
errors.go feat(http): add a utility function for reading http errors 2018-05-23 13:29:48 -05:00
handler.go migrate(platform): move public dependencies into platform 2018-05-14 17:12:53 -04:00
org_service.go feat(http): add a utility function for reading http errors 2018-05-23 13:29:48 -05:00
platform_handler.go feat(http): add platform http handler 2018-05-15 17:22:42 -04:00
query_service.go move types around to avoid cyclic imports 2018-05-21 17:02:42 -06:00
server.go feat(cmd/transpilerd): follow the logging style guide for transpilerd 2018-05-23 12:06:09 -05:00
status.go fix(http): rename status_response_writer.go to status.go 2018-05-16 11:45:13 -04:00
swagger.yml migrate(platform): move public dependencies into platform 2018-05-14 17:12:53 -04:00
transpiler_handler.go migrate(cmd/transpilerd): migrate the transpiler and its http handler to platform 2018-05-22 11:13:30 -05:00
usage_service.go enable vet 2018-05-22 17:05:17 -05:00
user_service.go feat(http): add a utility function for reading http errors 2018-05-23 13:29:48 -05:00

README.md

HTTP Handler Style Guide

HTTP Handler

  • Each handler should implement http.Handler
    • This can be done by embedding a httprouter.Router (a light weight HTTP router that supports variables in the routing pattern and matches against the request method)
  • Required services should be exported on the struct
// ThingHandler represents an HTTP API handler for things.
type ThingHandler struct {
	// embedded httprouter.Router as a lazy way to implement http.Handler
	*httprouter.Router

	ThingService         platform.ThingService
	AuthorizationService platform.AuthorizationService

	Logger               *zap.Logger
}

HTTP Handler Constructor

  • Routes should be declared in the constructor
// NewThingHandler returns a new instance of ThingHandler.
func NewThingHandler() *ThingHandler {
	h := &ThingHandler{
		Router: httprouter.New(),
		Logger: zap.Nop(),
	}

	h.HandlerFunc("POST", "/v1/things", h.handlePostThing)
	h.HandlerFunc("GET", "/v1/things", h.handleGetThings)

	return h
}

Route handlers (http.HandlerFuncs)

  • Each route handler should have an associated request struct and decode function
  • The decode function should take a context.Context and an *http.Request and return the associated route request struct
type postThingRequest struct {
	Thing *platform.Thing
}

func decodePostThingRequest(ctx context.Context, r *http.Request) (*postThingRequest, error) {
	t := &platform.Thing{}
	if err := json.NewDecoder(r.Body).Decode(t); err != nil {
		return nil, err
	}

	return &postThingRequest{
		Thing: t,
	}, nil
}
  • Route http.HandlerFuncs should separate the decoding and encoding of HTTP requests/response from actual handler logic
// handlePostThing is the HTTP handler for the POST /v1/things route.
func (h *ThingHandler) handlePostThing(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	req, err := decodePostThingRequest(ctx, r)
	if err != nil {
		errors.EncodeHTTP(ctx, err, w)
		return
	}

	// Do stuff here
	if err := h.ThingService.CreateThing(ctx, req.Thing); err != nil {
		errors.EncodeHTTP(ctx, err, w)
		return
	}

	if err := encodeResponse(ctx, w, http.StatusCreated, req.Thing); err != nil {
		h.Logger.Info("encoding response failed", zap.Error(err))
		return
	}
}
  • http.HandlerFunc's that require particular encoding of http responses should implement an encode response function