influxdb/http
Johnny Steenbergen f527636fe0 chore(http): refactor user http service to use the httpc.Client 2019-12-17 12:49:14 -08:00
..
influxdb
metric
mock
Makefile
README.md
api_handler.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
api_handler_test.go
assets.go
auth_service.go chore(http): refactor auth service http client to use httpc.Client 2019-12-17 12:18:16 -08:00
auth_test.go chore(http): refactor auth service http client to use httpc.Client 2019-12-17 12:18:16 -08:00
authentication_middleware.go
authentication_test.go
bucket_service.go feat(httpc): extend httpc Client with JSON shorthand methods 2019-12-12 07:44:31 -08:00
bucket_test.go
check_service.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
check_test.go
chronograf_handler.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
client.go chore(http): refactor user http service to use the httpc.Client 2019-12-17 12:49:14 -08:00
dashboard_service.go feat(httpc): extend httpc Client with JSON shorthand methods 2019-12-12 07:44:31 -08:00
dashboard_test.go chore: refactor notication endpoints to make a service out of notification endpoints 2019-12-17 10:49:06 -08:00
debug.go
delete_handler.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
delete_test.go
document_service.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
document_test.go
duration.go
duration_test.go
errors.go
errors_test.go
handler.go
handler_test.go
health.go
health_test.go
label_service.go feat(httpc): extend httpc Client with JSON shorthand methods 2019-12-12 07:44:31 -08:00
label_test.go
middleware.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
middleware_test.go
no_assets.go
notification_endpoint.go feat(pkger): add support for secret references to notification endpoints parsing 2019-12-17 11:33:34 -08:00
notification_endpoint_test.go chore: refactor notication endpoints to make a service out of notification endpoints 2019-12-17 10:49:06 -08:00
notification_rule.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
notification_rule_test.go
onboarding.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
onboarding_test.go
org_service.go feat(httpc): extend httpc Client with JSON shorthand methods 2019-12-12 07:44:31 -08:00
org_test.go
paging.go
paging_test.go
pkger_http_server.go chore(pkger): utilize http client for integration tests for pkger 2019-12-13 09:46:01 -08:00
pkger_http_server_test.go feat(testttp): extend testttp with new JSON methods and refactors test to use it 2019-12-13 14:07:11 -08:00
platform_handler.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
query.go
query_handler.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
query_handler_test.go fix: test baseline to use rust parser! 2019-12-13 11:51:49 -05:00
query_test.go
ready.go
redoc.go
requests.go
requests_test.go
router.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
router_test.go refactor: http error serialization matches the new error schema (#15196) 2019-09-19 10:06:47 -05:00
scraper_service.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
scraper_service_test.go
server.go
session_handler.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
session_test.go
source_proxy_service.go fix client tracing propagation from doubly recording (#14204) 2019-06-26 00:48:15 -05:00
source_service.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
source_service_test.go
status.go
swagger.go
swagger.yml feat(pkger): add support for exporting notification endpoints 2019-12-17 11:33:34 -08:00
swagger_assets.go
swagger_noassets.go
swagger_test.go
task_service.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
task_service_test.go
telegraf.go feat(httpc): extend httpc Client with JSON shorthand methods 2019-12-12 07:44:31 -08:00
telegraf_test.go
tokens.go
tokens_test.go
ua.go
ua_test.go
usage_service.go
user_resource_mapping_service.go feat(httpc): extend httpc Client with JSON shorthand methods 2019-12-12 07:44:31 -08:00
user_resource_mapping_test.go
user_service.go chore(http): refactor user http service to use the httpc.Client 2019-12-17 12:49:14 -08:00
user_test.go chore(http): refactor user http service to use the httpc.Client 2019-12-17 12:49:14 -08:00
variable_service.go feat(httpc): extend httpc Client with JSON shorthand methods 2019-12-12 07:44:31 -08:00
variable_test.go
write_handler.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
write_handler_test.go chore: Remove several instances of WithLogger (#15996) 2019-12-04 15:10:23 -08: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", "/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 {
		EncodeError(ctx, err, w)
		return
	}

	// Do stuff here
	if err := h.ThingService.CreateThing(ctx, req.Thing); err != nil {
		EncodeError(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