influxdb/http
Lyon Hill d720661e77
feat(tenant): change the tenant services default behavior to enabled (#17872)
We have reached the stage wehre the new tenant service is being used and
is stable but we want to get it in more hands and used as the default service.
2020-04-29 14:04:26 -06:00
..
influxdb refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
metric refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
mock refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
Makefile Remove typescript client generation 2019-02-05 17:13:57 -08:00
README.md chore(http): update error handling example in readme (#13874) 2019-05-13 12:50:25 -07:00
api_handler.go revert(tenant): integrate the tenant service as the default backend (#17805) (#17832) 2020-04-22 17:23:17 +01:00
api_handler_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
assets.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
auth_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
auth_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
authentication_middleware.go feat(tracing): add userID to lookup traces 2020-04-08 14:19:05 +02:00
authentication_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
backup_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
bucket_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
bucket_service_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
check_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
check_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
chronograf_handler.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
client.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
dashboard_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
dashboard_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
debug.go add spans to multiple services 2019-03-07 12:12:31 -07:00
delete_handler.go fix(delete): fix delete handler to add org filter for bucket lookup (#17834) 2020-04-23 08:32:50 -06:00
delete_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
document_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
document_service_test.go fix(labels): prevent adding duplicate labels to resources in kv (#17340) 2020-04-16 09:02:50 -07:00
document_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
duration.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
duration_test.go feat(http) add microsecond and nanosecond durations 2018-09-25 15:15:06 -05:00
errors.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
errors_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
handler.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
handler_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
health.go fix(http): health endpoint now returns pass 2018-12-20 21:53:54 -06:00
health_test.go chore(http): surface unmarshaling failures in tests (#13855) 2019-05-08 12:51:03 -07:00
helpers.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
label_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
label_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
middleware.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
middleware_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
no_assets.go feat(http): host swagger docs at /docs and /api/v2/swagger.json 2019-01-22 11:16:27 -06:00
notification_endpoint.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
notification_endpoint_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
notification_rule.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
notification_rule_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
onboarding.go feat(tenant): Build out the onboarding system in tenant and integrate it into launcher (#17558) 2020-04-06 15:58:15 -06:00
onboarding_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
org_service.go fix: org filtering for both kv and tenant need to match behaviors. (#17758) 2020-04-16 12:22:21 -06:00
org_service_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
paging.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
paging_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
platform_handler.go feat(tenant): change the tenant services default behavior to enabled (#17872) 2020-04-29 14:04:26 -06:00
query.go fix(query): Respect the now-time of the compiled query if it's provided. 2020-04-08 11:50:47 -04:00
query_handler.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
query_handler_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
query_test.go fix(query): Respect the now-time of the compiled query if it's provided. 2020-04-08 11:50:47 -04:00
ready.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
redoc.go feat(http): host swagger docs at /docs and /api/v2/swagger.json 2019-01-22 11:16:27 -06:00
requests.go fix(delete): fix delete handler to add org filter for bucket lookup (#17834) 2020-04-23 08:32:50 -06:00
requests_test.go fix(delete): fix delete handler to add org filter for bucket lookup (#17834) 2020-04-23 08:32:50 -06:00
router.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
router_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
scraper_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
scraper_service_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
server.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
session_handler.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
session_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
source_proxy_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
source_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
source_service_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
swagger.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
swagger.yml feat(dashboards): extend dashboard update API to take cells as a put doc operation 2020-04-20 14:04:45 -07:00
swagger_assets.go chore: serve swagger.json in development builds 2019-03-09 12:42:44 -08:00
swagger_noassets.go chore: Remove several instances of WithLogger (#15996) 2019-12-04 15:10:23 -08:00
swagger_test.go feat(influxdb): Add labels create endpoints 2019-10-23 09:53:50 -07:00
task_service.go chore: remove extranenous fields on http.Task type 2020-04-20 15:34:58 -07:00
task_service_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
telegraf.go fix(http): provide support for telegraf update in http client 2020-04-24 17:05:55 -07:00
telegraf_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
tokens.go Ensure error strings not capitalised ST1005 2018-11-30 10:54:24 +00:00
tokens_test.go chore(bolt,http,mock,query,testing): fix megacheck errors 2018-11-01 12:54:46 -07:00
ua.go refactor(http): normalize user-agent names 2019-12-04 15:02:00 -06:00
ua_test.go refactor(http): normalize user-agent names 2019-12-04 15:02:00 -06:00
usage_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
user_resource_mapping_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
user_resource_mapping_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
user_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
user_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
variable_service.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
variable_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
write_handler.go fix(http/write_handler): propagate context to http request 2020-04-14 11:17:55 +02:00
write_handler_test.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -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", "/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