influxdb/http
Chris Goller 532a3b7294
Merge pull request #2015 from zhulongcheng/trace-span
docs(http): add OpenTracing span
2018-12-20 20:46:10 -06:00
..
influxdb
Makefile No longer need to convert yaml to json to generate client 2018-12-20 14:59:24 -08:00
README.md
api_handler.go fix(http): update swagger doc for /api/v2/ response 2018-12-20 16:25:53 -05:00
assets.go
auth_service.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
auth_test.go fix(http): rename auths to authorizations 2018-12-18 00:12:16 +08:00
authentication_middleware.go fix(http): use platform.Error instead of old style error 2018-12-20 11:11:09 -05:00
authentication_test.go
bucket_service.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
bucket_test.go
chronograf_handler.go
client.go
cur_swagger.yml chore(http): remove plan specs from swagger 2018-12-18 12:45:30 -07:00
dashboard_service.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
dashboard_test.go
duration.go
duration_test.go
errors.go fix(http): use platform.Error instead of old style error 2018-12-20 11:11:09 -05:00
errors_test.go
handler.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
health.go
label_service.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
macro_service.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
macro_test.go
onboarding.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
onboarding_test.go
org_service.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
org_test.go
paging.go
piging_test.go
platform_handler.go
proxy_query_service.go
query.go Merge branch 'master' into flux-staging 2018-12-18 11:20:17 -07:00
query_handler.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
query_handler_test.go fix(http): remove /api/v2/query/plan 2018-12-18 12:17:34 -07:00
query_service.go fix(http): make query service look for platform.Error in response (#2027) 2018-12-18 13:37:25 -08:00
query_test.go
ready.go
requests.go
router.go
router_test.go
scraper_service.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
scraper_service_test.go
server.go feat(http): add error logger for http server errors 2018-12-20 13:21:28 -05:00
session_handler.go
session_test.go
source_proxy_service.go
source_service.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
source_service_test.go
status.go
swagger.yml Merge pull request #2015 from zhulongcheng/trace-span 2018-12-20 20:46:10 -06:00
swagger_test.go
task_service.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
task_service_test.go
task_test.go
telegraf.go Merge pull request #2073 from influxdata/fix/issue#1904 2018-12-20 14:53:04 -05:00
tokens.go
tokens_test.go
usage_service.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
user_resource_mapping_service.go
user_resource_mapping_test.go
user_service.go
user_test.go
view_service.go Merge pull request #2073 from influxdata/fix/issue#1904 2018-12-20 14:53:04 -05:00
view_test.go fix(http): view errors endpoint conversion 2018-12-19 16:54:48 -05:00
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