influxdb/http
Mark Rushakoff 4df856c229 chore(task): add messages to all HTTP errors from tasks
Closes #11544.
2019-01-25 09:22:34 -08:00
..
influxdb chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
mock updates based on review 2019-01-23 09:29:50 -08:00
Makefile feat(http): host swagger docs at /docs and /api/v2/swagger.json 2019-01-22 11:16:27 -06:00
README.md Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
api_handler.go updates based on review 2019-01-23 09:29:50 -08:00
api_handler_test.go chore(http): make test diffs more readable (#2203) 2019-01-03 14:29:22 -08:00
assets.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
auth_service.go fix(http): ensure response body always closed 2019-01-18 14:55:48 -08:00
auth_test.go feat(bolt): add metrics for resources stored in boltdb 2019-01-18 17:45:45 -06:00
authentication_middleware.go fix(ui): add session timeout notification (#11281) 2019-01-18 17:43:00 -05:00
authentication_test.go fix(ui): add session timeout notification (#11281) 2019-01-18 17:43:00 -05:00
bucket_service.go fix(cmd/influx): make setup, write, query a solid experience 2019-01-22 13:25:17 -08:00
bucket_test.go Labels v2 (#11198) 2019-01-18 11:03:36 -08:00
chronograf_handler.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
client.go feat(http): add MacroService client 2018-09-20 09:32:04 -07:00
cur_swagger.yml fix(ui): wiring for labels (#11404) 2019-01-22 14:22:11 -08:00
dashboard_service.go fix(http): ensure response body always closed 2019-01-18 14:55:48 -08:00
dashboard_test.go Labels v2 (#11198) 2019-01-18 11:03:36 -08:00
duration.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
duration_test.go feat(http) add microsecond and nanosecond durations 2018-09-25 15:15:06 -05:00
errors.go fix(ui): add session timeout notification (#11281) 2019-01-18 17:43:00 -05:00
errors_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
handler.go feat(http): use default prom histogram buckets 2019-01-19 19:19:18 -06: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 chore: refactor according to label mappings improvements 2019-01-25 11:17:36 +01:00
label_test.go chore: use specific error for labels not found 2019-01-25 11:17:36 +01:00
macro_service.go fix(http): ensure response body always closed 2019-01-18 14:55:48 -08:00
macro_test.go chore(testing): test macro by org 2019-01-15 20:39:41 +01: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): ensure response body always closed 2019-01-18 14:55:48 -08:00
onboarding_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
org_service.go fix(http): ensure response body always closed 2019-01-18 14:55:48 -08:00
org_test.go test(http): ensure bucket service tests have appropriate services set 2019-01-16 20:09:03 -05:00
paging.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
paging_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
platform_handler.go feat(http): host swagger docs at /docs and /api/v2/swagger.json 2019-01-22 11:16:27 -06:00
proto.go feat(http): dashboards protos pagination 2019-01-17 12:35:00 +01:00
proto_test.go feat(http): dashboards protos pagination 2019-01-17 12:35:00 +01:00
proxy_query_service.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
query.go feat(http): add vnd.flux content-type support 2019-01-17 14:01:56 -07:00
query_handler.go fix(http): ensure response body always closed 2019-01-18 14:55:48 -08:00
query_handler_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
query_service.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
query_test.go feat(http): add vnd.flux content-type support 2019-01-17 14:01:56 -07: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 log api panic with a stack trace 2019-01-15 11:58:42 +08:00
router_test.go add test for logging http panics 2019-01-21 23:29:28 +08:00
scraper_service.go updates based on review 2019-01-23 09:29:50 -08:00
scraper_service_test.go updates based on review 2019-01-23 09:29:50 -08:00
server.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
session_handler.go fix(ui): add session timeout notification (#11281) 2019-01-18 17:43:00 -05:00
session_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
source_proxy_service.go fix(http): ensure response body always closed 2019-01-18 14:55:48 -08:00
source_service.go fix(http): ensure response body always closed 2019-01-18 14:55:48 -08:00
source_service_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
status.go feat: Add optional http logging to handler 2018-07-30 16:16:37 -06:00
swagger.go feat(http): host swagger docs at /docs and /api/v2/swagger.json 2019-01-22 11:16:27 -06:00
swagger.yml updates based on review 2019-01-23 09:29:50 -08:00
swagger_test.go Update telegraf config schema to match cur swagger 2018-12-12 15:11:18 -08:00
task_service.go chore(task): add messages to all HTTP errors from tasks 2019-01-25 09:22:34 -08:00
task_service_test.go Remove owner from task json responses (#11504) 2019-01-24 16:05:24 -07:00
task_test.go Show organization names in task api actions (#11223) 2019-01-18 09:10:14 -07:00
telegraf.go Labels v2 (#11198) 2019-01-18 11:03:36 -08:00
telegraf_test.go fix(http): fix the user resouce mapping filter 2019-01-14 12:07:51 -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
usage_service.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
user_resource_mapping_service.go fix(http): ensure response body always closed 2019-01-18 14:55:48 -08:00
user_resource_mapping_test.go fix(http): fix typo in owner/member links 2019-01-17 14:09:03 -08:00
user_service.go fix(http): ensure response body always closed 2019-01-18 14:55:48 -08:00
user_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
view_service.go Labels v2 (#11198) 2019-01-18 11:03:36 -08:00
view_test.go fix(http): add labels to view links responses 2019-01-10 11:41:17 -08:00
write_handler.go pr review changes 2019-01-22 21:24:05 +08: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