influxdb/http
George 78cafa861b
refactor(notifications): isolate endpoint service (#19818)
* refactor(notifications): isolate endpoint service

Following the ongoing effort to isolate behaviours into their own
packages and off of kv.Service, this change move the notification
endpoints service implementation into its own package. It removes the
endpoint behaviors from the kv service completely.

* chore(influxd): wire up the isolated check service in place of kv service
2020-10-28 15:22:14 +00:00
..
influxdb Merge branch 'master' into chore/merge-master 2020-04-06 12:17:47 -05:00
legacy fix: Ensure precision is passed to points parsing 2020-08-31 17:39:52 -07: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
mocks feat(http): V1 write support 2020-08-18 14:04:35 -07:00
points fix: Ensure precision is passed to points parsing 2020-08-31 17:39:52 -07:00
Makefile fix(http): silence echo command (#18277) 2020-06-11 11:04:50 -07:00
README.md chore(http): update error handling example in readme (#13874) 2019-05-13 12:50:25 -07:00
api_handler.go refactor(notification): move rule service into own package (#19804) 2020-10-27 11:45:05 +00: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 chore: update staticcheck and fix newly identified lint checks (#18737) 2020-06-26 18:54:09 -05:00
auth_test.go chore(tenant): make tenant and kv both conform with harness (#19266) 2020-08-11 15:56:42 +01:00
authentication_middleware.go feat(tracing): add userID to lookup traces 2020-04-08 14:19:05 +02:00
authentication_test.go chore: update staticcheck and fix newly identified lint checks (#18737) 2020-06-26 18:54:09 -05:00
backup_service.go chore(fs): refactor global file consts into internal/fs 2020-07-21 14:27:52 -07:00
bucket_service.go fix: add quotation marks to bucket name validation (#19222) 2020-08-05 13:30:45 -07:00
bucket_service_test.go chore(tenant): make tenant and kv both conform with harness (#19266) 2020-08-11 15:56:42 +01:00
check_service.go Merge branch 'master' into feat/use-algo-w 2020-05-18 13:25:26 -05:00
check_test.go feat(autoAggregate): BE work for autoAggregate (#19129) 2020-07-29 09:32:36 -07: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 fix(http): Use shared http.Transport 2020-06-05 09:52:45 -07:00
dashboard_service.go refactor: remove org service from kv dashboard service (#18504) 2020-06-16 14:03:42 -07:00
dashboard_test.go feat: add tick generation properties and legendColorizeRows 2020-10-27 16:50:53 -07:00
debug.go add spans to multiple services 2019-03-07 12:12:31 -07:00
delete_handler.go fix: Remove incorrect implementation of delete APIs and disable HTTP API 2020-09-24 11:11:46 -07:00
delete_test.go fix: Remove incorrect implementation of delete APIs and disable HTTP API 2020-09-24 11:11:46 -07: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 refactor(notification): move rule service into own package (#19804) 2020-10-27 11:45:05 +00: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 chore(errors): interpret more cases as influxdb error type in http.CheckError (#17888) 2020-04-30 17:55:17 +01:00
errors_test.go chore(errors): interpret more cases as influxdb error type in http.CheckError (#17888) 2020-04-30 17:55:17 +01:00
handler.go chore(http): add response_code as label to http_api_* metrics (#19389) 2020-08-19 13:19:18 -07:00
handler_test.go chore(http): add response_code as label to http_api_* metrics (#19389) 2020-08-19 13:19:18 -07:00
health.go feat(api/health): report commit 2020-05-26 14:03:52 +02:00
health_test.go feat(api/health): report commit 2020-05-26 14:03:52 +02: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 chore: update staticcheck and fix newly identified lint checks (#18737) 2020-06-26 18:54:09 -05:00
label_test.go refactor: migrator and introduce Store.(Create|Delete)Bucket (#18570) 2020-07-01 12:08:20 +01:00
legacy.go feat(http): V1 write support 2020-08-18 14:04:35 -07: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(notifications): isolate endpoint service (#19818) 2020-10-28 15:22:14 +00:00
notification_endpoint_test.go refactor(notifications): isolate endpoint service (#19818) 2020-10-28 15:22:14 +00:00
notification_rule.go refactor(notification): move rule service into own package (#19804) 2020-10-27 11:45:05 +00:00
notification_rule_test.go feat(query): implement window count aggregate pushdown (#18043) 2020-05-28 13:42:38 -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 chore(tenant): make tenant and kv both conform with harness (#19266) 2020-08-11 15:56:42 +01:00
org_service.go fix(api/orgs): (API-design-issue) FindOptions for orgs 2020-08-20 16:55:30 -06:00
org_service_test.go chore(tenant): make tenant and kv both conform with harness (#19266) 2020-08-11 15:56:42 +01:00
paging_test.go chore: update staticcheck and fix newly identified lint checks (#18737) 2020-06-26 18:54:09 -05:00
platform_handler.go feat(authorization): Create a v1 authorization service 2020-10-27 08:26:10 -07:00
proxy_handler.go chore(http): Let the callers convert these to http.Handler. 2020-05-08 09:02:06 -04:00
query.go feat: upgrade Flux to v0.83.2 (#19569) 2020-09-16 17:59:15 -07:00
query_handler.go feat: annotate context with feature flags when handling flux queries (#18506) 2020-06-17 16:55:29 +03:00
query_handler_test.go feat: upgrade Flux to v0.83.2 (#19569) 2020-09-16 17:59:15 -07:00
query_test.go feat: upgrade Flux to v0.83.2 (#19569) 2020-09-16 17:59:15 -07: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 refactor(http): Refactor write endpoint to enable 1.x write code reuse. 2020-06-17 13:13:58 -04: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 chore(http): fixup the chi base router using API type 2020-06-15 17:16:05 -07:00
router_test.go chore(http): add route path to panic log message 2020-06-09 18:00:11 -07: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 chore(tenant): point tests to new tenant service (#19477) 2020-09-02 11:50:26 -06:00
server.go refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
service_test.go refactor: migrator and introduce Store.(Create|Delete)Bucket (#18570) 2020-07-01 12:08:20 +01:00
session_handler.go feat: integrate the new session system behind a feature flag (#18042) 2020-05-18 11:50:22 -06:00
session_test.go fix: switch port to 8086 2020-09-03 14:40:29 -07: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 Merge branch 'master' into chore/merge-master 2020-04-06 12:17:47 -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: add tick generation properties and legendColorizeRows 2020-10-27 16:50:53 -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 refactor(notification): move rule service into own package (#19804) 2020-10-27 11:45:05 +00:00
task_service_test.go fix: switch port to 8086 2020-09-03 14:40:29 -07:00
telegraf.go feat(influx): add telegraf resource support to influx CLI 2020-07-20 15:37:43 -07:00
telegraf_test.go fix: switch port to 8086 2020-09-03 14:40:29 -07: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 fix: improve the http urm client to allow specific path options (#18715) 2020-06-25 12:13:05 -06: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 feat(task): Add new permission lookup pattern for executor (#18920) 2020-07-13 14:47:58 -06: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 fix(variables): add id to put variable request 2020-09-08 11:26:03 -07:00
variable_test.go fix(variables): add id to put variable request 2020-09-08 11:26:03 -07:00
write_handler.go fix: add ability to define bucket to write to with a BucketFilter (#19658) 2020-10-05 15:05:49 -06:00
write_handler_test.go fix: switch port to 8086 2020-09-03 14:40:29 -07:00
write_usage_recorder.go refactor(http): Refactor write endpoint to enable 1.x write code reuse. 2020-06-17 13:13:58 -04: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