influxdb/http
Alirie Gray 6a562757ad test(http): add test coverage for handler.go 2019-05-02 14:04:40 -07:00
..
influxdb Merge branch 'master' into flux-staging 2019-04-17 12:26:10 -06:00
metric feat(influxdb): add query/write http event recorders 2019-04-17 08:33:31 -04:00
mock updates based on review 2019-01-23 09:29:50 -08:00
Makefile Remove typescript client generation 2019-02-05 17:13:57 -08:00
README.md Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
api_handler.go feat(influxdb): add scraper filter 2019-04-25 11:33:34 -04:00
api_handler_test.go chore(http): make test diffs more readable (#2203) 2019-01-03 14:29:22 -08:00
assets.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
auth_service.go fix(http): don't include nil values when auth responses are skipped 2019-04-23 14:34:04 -07:00
auth_test.go fix(http): don't include nil values when auth responses are skipped 2019-04-23 14:34:04 -07:00
authentication_middleware.go Fix typos in miscellaneous packages 2019-04-17 13:30:22 -07:00
authentication_test.go fix(ui): add session timeout notification (#11281) 2019-01-18 17:43:00 -05:00
bucket_service.go feat(influxdb): add bucket desc 2019-04-24 11:20:05 -04:00
bucket_test.go remove org from bucket 2019-04-22 18:39:05 -04:00
chronograf_handler.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
client.go docs(http): default http transport commentary 2019-04-18 11:27:35 +02:00
dashboard_service.go Revert "fix(http): remove view ID" 2019-04-15 15:00:40 -07:00
dashboard_test.go Revert "fix(inmem): remove the old inmem implementation" 2019-04-09 14:24:40 -04:00
debug.go add spans to multiple services 2019-03-07 12:12:31 -07:00
document_service.go fix document service panic 2019-04-26 14:46:37 +08:00
document_test.go fix(http): make org param optional in document get request 2019-04-25 14:45:01 -07:00
duration.go update(http): remove kerrors, change the default to be influxdb error 2019-01-25 10:29:26 -08:00
duration_test.go feat(http) add microsecond and nanosecond durations 2018-09-25 15:15:06 -05:00
errors.go chore(http): add 429 response to errors 2019-04-16 16:50:35 -07:00
errors_test.go fix(http): fix task errors 2019-01-25 10:31:24 -08:00
handler.go feat(http): add user-agent metrics to all http endpoints 2019-05-02 11:58:13 -07:00
handler_test.go test(http): add test coverage for handler.go 2019-05-02 14:04:40 -07:00
health.go fix(http): health endpoint now returns pass 2018-12-20 21:53:54 -06:00
health_test.go chore(http): make test diffs more readable (#2203) 2019-01-03 14:29:22 -08:00
label_service.go organizationID -> orgID 2019-04-12 01:29:42 -07:00
label_test.go fix label tests 2019-04-12 01:29:49 -07:00
no_assets.go feat(http): host swagger docs at /docs and /api/v2/swagger.json 2019-01-22 11:16:27 -06:00
onboarding.go fix(http): rename platform to influxdb in package 2019-02-07 19:08:41 -05:00
onboarding_test.go Revert "fix(inmem): remove the old inmem implementation" 2019-04-09 14:24:40 -04:00
org_service.go Merge pull request #13636 from influxdata/delete_invalid_org_id 2019-04-29 10:15:36 -07:00
org_test.go Revert "fix(inmem): remove the old inmem implementation" 2019-04-09 14:24:40 -04:00
paging.go update(http): remove kerrors, change the default to be influxdb error 2019-01-25 10:29:26 -08:00
paging_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
platform_handler.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
proxy_query_service.go fix(http): use influxdb.Error in ProxyQueryHandler 2019-04-26 08:19:00 -07:00
proxy_query_service_test.go fix(http): use influxdb.Error in ProxyQueryHandler 2019-04-26 08:19:00 -07:00
query.go Merge branch 'master' into flux-staging 2019-04-17 12:26:10 -06:00
query_handler.go Merge branch 'master' into flux-staging 2019-04-18 11:01:22 -06:00
query_handler_test.go refactor(http): remove api/v2/query/spec endpoint and handler 2019-04-12 07:15:27 -07:00
query_test.go Merge branch 'master' into flux-staging 2019-04-18 11:01:22 -06:00
ready.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08: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 chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
requests_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
router.go feat(http): pretty stack trace for the http panic recovery handler (#13509) 2019-04-26 15:06:41 -05:00
router_test.go add test for logging http panics 2019-01-21 23:29:28 +08:00
scraper_service.go feat(influxdb): add scraper filter 2019-04-25 11:33:34 -04:00
scraper_service_test.go feat(influxdb): add scraper filter 2019-04-25 11:33:34 -04:00
server.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
session_handler.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
session_test.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
source_proxy_service.go fix(query): add Check method to query service interfaces (#12866) 2019-03-25 20:05:44 -07:00
source_service.go refactor(http): change all instances of spec compiler to repl compiler (#13268) 2019-04-12 07:15:27 -07:00
source_service_test.go fix(http): fix source health endpoint 2019-03-13 11:04:16 -04:00
status.go feat(influxdb): add query/write http event recorders 2019-04-17 08:33:31 -04:00
swagger.go chore: serve swagger.json in development builds 2019-03-09 12:42:44 -08:00
swagger.yml feat(influxdb): add scraper filter 2019-04-25 11:33:34 -04:00
swagger_assets.go chore: serve swagger.json in development builds 2019-03-09 12:42:44 -08:00
swagger_noassets.go chore: serve swagger.json in development builds 2019-03-09 12:42:44 -08:00
swagger_test.go Remove validation of cur_swagger 2019-02-06 16:28:49 -08:00
task_service.go fix(http): use lang.Compile 2019-04-12 07:15:27 -07:00
task_service_test.go remove org from bucket 2019-04-22 18:39:05 -04:00
task_test.go Revert "fix(inmem): remove the old inmem implementation" 2019-04-09 14:24:40 -04:00
telegraf.go fix(http): remove newPatchLabelHandler, it should not exist 2019-03-27 14:31:04 -07:00
telegraf_test.go fix(http/telegraf): add missing member/owner test links 2019-03-10 22:56:52 -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
transport.go docs(http): default http transport commentary 2019-04-18 11:27:35 +02:00
usage_service.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
user_resource_mapping_service.go feat(influxdb): add support for templates 2019-03-06 13:18:31 -08:00
user_resource_mapping_test.go fix(http): log endpoint should be logs 2019-03-11 12:45:25 -05:00
user_service.go fix(http): swagger change user pass reset resp 2019-03-25 12:46:56 -04:00
user_test.go Revert "fix(inmem): remove the old inmem implementation" 2019-04-09 14:24:40 -04:00
variable_service.go feat(http): add labels to variables 2019-03-28 15:05:13 -07:00
variable_test.go Fix typos in miscellaneous packages 2019-04-17 13:30:22 -07:00
write_handler.go Merge point parse & explode. 2019-04-24 10:12:15 -06:00
write_handler_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -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 {
		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