influxdb/http
Jonathan A. Sternberg 3bff8acd96
fix(http): ignore an empty trailer inside of the proxy query service client (#12834)
2019-03-21 16:30:04 -05:00
..
influxdb
mock
Makefile
README.md
api_handler.go Merge pull request #12677 from influxdata/swagger_resource_buckets 2019-03-18 18:29:23 -04:00
api_handler_test.go
assets.go
auth_service.go
auth_test.go
authentication_middleware.go
authentication_test.go
bucket_service.go fix(http): update bucket write links to include org/bucket 2019-03-11 13:46:16 -05:00
bucket_test.go fix(http): update bucket write links to include org/bucket 2019-03-11 13:46:16 -05:00
chronograf_handler.go
client.go
dashboard_service.go fix(http): log endpoint should be logs 2019-03-11 12:45:25 -05:00
dashboard_test.go fix(http): log endpoint should be logs 2019-03-11 12:45:25 -05:00
debug.go
document_service.go
duration.go
duration_test.go
errors.go
errors_test.go
handler.go
health.go
health_test.go
label_service.go fix(authorizer): labels can be created if the user has read access to the specified org 2019-03-19 01:56:55 -07:00
label_test.go pass tests 2019-03-19 07:00:43 -07:00
no_assets.go
onboarding.go
onboarding_test.go
org_service.go fix(http): change secrets to match swagger 2019-03-13 11:06:36 -04:00
org_test.go fix(http): change secrets to match swagger 2019-03-13 11:06:36 -04:00
paging.go
paging_test.go
platform_handler.go
proto.go
proto_test.go fix(http): log endpoint should be logs 2019-03-11 12:45:25 -05:00
proxy_query_service.go fix(http): ignore an empty trailer inside of the proxy query service client (#12834) 2019-03-21 16:30:04 -05:00
query.go
query_handler.go
query_handler_test.go
query_test.go
ready.go
redoc.go
requests.go
requests_test.go
router.go
router_test.go
scraper_service.go
scraper_service_test.go
server.go
session_handler.go
session_test.go
source_proxy_service.go
source_service.go fix(http): change source buckets from slice to obj 2019-03-18 17:53:53 -04:00
source_service_test.go fix(http): fix source health endpoint 2019-03-13 11:04:16 -04:00
status.go
swagger.go
swagger.yml Merge pull request #12370 from influxdata/feat/task-delete-options 2019-03-19 16:18:39 -05:00
swagger_assets.go
swagger_noassets.go
swagger_test.go
task_service.go test(task): ensure task can be created over HTTP using org name 2019-03-14 21:23:14 -07:00
task_service_test.go test(task): ensure task can be created over HTTP using org name 2019-03-14 21:23:14 -07:00
task_test.go Update task servicetest to move dependency to the new TaskControlService (#12817) 2019-03-21 15:11:22 -06:00
telegraf.go fix(http): fix telegraf delete 2019-03-15 11:31:22 -04:00
telegraf_test.go fix(http/telegraf): add missing member/owner test links 2019-03-10 22:56:52 -05:00
tokens.go
tokens_test.go
usage_service.go
user_resource_mapping_service.go
user_resource_mapping_test.go fix(http): log endpoint should be logs 2019-03-11 12:45:25 -05:00
user_service.go fix(http): log endpoint should be logs 2019-03-11 12:45:25 -05:00
user_test.go
variable_service.go
variable_test.go
view_service.go
view_test.go
write_handler.go
write_handler_test.go

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", "/api/v2/things", h.handlePostThing)
	h.HandlerFunc("GET", "/api/v2/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 /api/v2/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