influxdb/http
Dane Strandboge 82d1123e78
build: upgrade to Go 1.18.1 ()
2022-04-13 15:24:27 -05:00
..
influxdb
legacy build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
metric
mock
mocks
points
README.md
api_handler.go
api_handler_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
auth_service.go
auth_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
authentication_middleware.go
authentication_test.go
backup_service.go
backup_service_test.go
check_service.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
check_test.go
client.go
config.go
config_test.go
debug.go
delete_handler.go
delete_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
document_service.go
document_service_test.go
document_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
duration.go
duration_test.go
errors.go
errors_test.go
handler.go
handler_test.go
health.go
health_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
label_service.go
label_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
legacy.go
middleware.go
middleware_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
notification_endpoint.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
notification_endpoint_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
notification_rule.go
notification_rule_test.go
paging_test.go
platform_handler.go
proxy_handler.go
query.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
query_handler.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
query_handler_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
query_test.go
ready.go
ready_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
redoc.go
requests.go
requests_test.go
resources.go
resources_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
restore_service.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
router.go
router_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
scraper_service.go
scraper_service_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
source_proxy_service.go
source_service.go
source_service_test.go
swagger.yml
swaggerV1Compat.yml
task_service.go
task_service_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
telegraf.go
telegraf_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
tokens.go
tokens_test.go
ua.go
ua_test.go
user_resource_mapping_service.go
user_resource_mapping_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
variable_service.go
variable_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
write_handler.go
write_handler_test.go build: upgrade to Go 1.18.1 () 2022-04-13 15:24:27 -05:00
write_usage_recorder.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 {
		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