influxdb/http
Alex P d12e94bb01 feat(ui/labels): Add the ability to edit labels on a dashboard
Co-authored-by: Alex Paxton <thealexpaxton@gmail.com>
2019-01-09 17:41:19 -08:00
..
influxdb Address PR feedback 2018-11-30 10:54:24 +00:00
Makefile Make client generation optional via ENV variable 2019-01-03 11:33:23 -08:00
README.md Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
api_handler.go Merge pull request #2163 from zhulongcheng/session-backend 2019-01-09 17:05:59 -05:00
api_handler_test.go chore(http): make test diffs more readable (#2203) 2019-01-03 14:29:22 -08:00
assets.go fix(http): Fix developer mode default 2018-12-21 16:33:04 -05:00
auth_service.go fix(bolt): fix auth status update error 2019-01-04 12:21:34 -05:00
auth_test.go fix(bolt): fix auth status update error 2019-01-04 12:21:34 -05:00
authentication_middleware.go feat(http): session service with platform errors 2018-12-21 09:56:23 +01:00
authentication_test.go fix(http): use correct spelling of of authenication in handler constructor 2018-10-02 14:11:44 -04:00
bucket_service.go feat(http): add labels to bucket responses 2019-01-03 16:02:02 -08:00
bucket_test.go test json formatting 2019-01-03 16:46:47 -08:00
chronograf_handler.go add custom-404 handler 2018-12-16 22:55:39 +08:00
client.go feat(http): add MacroService client 2018-09-20 09:32:04 -07:00
cur_swagger.yml feat(ui/labels): Add the ability to edit labels on a dashboard 2019-01-09 17:41:19 -08:00
dashboard_service.go feat(platform): add proto service for creating templated resources 2019-01-09 09:55:15 -05:00
dashboard_test.go fix(platform): nest view beneath dashboard cell 2019-01-08 18:11:13 -05:00
duration.go chore(http/duration): mention need for longer duration literals 2018-09-25 18:11:20 -05:00
duration_test.go feat(http) add microsecond and nanosecond durations 2018-09-25 15:15:06 -05:00
errors.go add MethodNotAllowed handler 2018-12-23 15:55:45 +08:00
errors_test.go fix(http): increase errorHeaderMaxLength 2018-08-01 16:04:36 -07:00
handler.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05: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 Label responses (#2165) 2019-01-02 11:17:28 -08:00
macro_service.go fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
macro_test.go rename msg to message for platform.Error json 2018-12-27 10:34:00 +08:00
onboarding.go feat(http): add labels to bucket responses 2019-01-03 16:02:02 -08:00
onboarding_test.go (feat/testing) add onboarding and basic auth service 2018-11-02 12:20:45 -04:00
org_service.go feat(auth): rewrite authorization service (#2157) 2018-12-28 15:02:19 -08:00
org_test.go chore(http): make test diffs more readable (#2203) 2019-01-03 14:29:22 -08:00
paging.go flatten paging links 2018-12-12 23:25:07 +08:00
paging_test.go fix(http): rename piging_test to paging_test 2018-12-27 09:48:20 -06:00
platform_handler.go fix(http): Fix developer mode default 2018-12-21 16:33:04 -05:00
proto.go feat(platform): add proto service for creating templated resources 2019-01-09 09:55:15 -05:00
proto_test.go feat(platform): add proto service for creating templated resources 2019-01-09 09:55:15 -05:00
proxy_query_service.go add custom-404 handler 2018-12-16 22:55:39 +08:00
query.go refactor(http/query): update http query logic for new parser API 2019-01-04 11:28:55 -07:00
query_handler.go feat(query/functions/tests): end to end testing is configured to run against a live storage engine. 2019-01-07 13:05:29 -05:00
query_handler_test.go refactor(http/query): update http query logic for new parser API 2019-01-04 11:28:55 -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 refactor(http/query): update http query logic for new parser API 2019-01-04 11:28:55 -07:00
ready.go Add uptime to ready endpoint 2018-12-12 11:41:50 +00:00
requests.go fix(http): change /query to use org/orgID 2018-12-20 22:18:09 -06:00
requests_test.go fix(http): update test to use platform.Error 2018-12-21 08:58:16 -06:00
router.go add MethodNotAllowed handler 2018-12-23 15:55:45 +08:00
router_test.go chore(http): make test diffs more readable (#2203) 2019-01-03 14:29:22 -08:00
scraper_service.go chore(http): test update of scraper targets 2019-01-08 15:57:29 +01:00
scraper_service_test.go fix(http): remove fmt.Println from scraper service tests 2019-01-09 15:17:11 -05:00
server.go feat(http): add error logger for http server errors 2018-12-20 13:21:28 -05:00
session_handler.go initialize session handler by SessionBackend 2018-12-29 11:41:06 +08:00
session_test.go initialize session handler by SessionBackend 2018-12-29 11:41:06 +08:00
source_proxy_service.go chore(fmt): fix and simplify go formatting 2018-10-08 17:23:54 -05:00
source_service.go fix(http): convert source errors 2019-01-03 11:38:01 -05:00
source_service_test.go Remove use of verbose from control.Config (#1573) 2018-11-26 16:02:41 -07:00
status.go feat: Add optional http logging to handler 2018-07-30 16:16:37 -06:00
swagger.yml Merge pull request #2112 from zhulongcheng/doc-rm-org 2019-01-09 17:06:53 -05:00
swagger_test.go Update telegraf config schema to match cur swagger 2018-12-12 15:11:18 -08:00
task_service.go Label responses (#2165) 2019-01-02 11:17:28 -08:00
task_service_test.go chore(http): make test diffs more readable (#2203) 2019-01-03 14:29:22 -08:00
task_test.go feat(auth): rewrite authorization service (#2157) 2018-12-28 15:02:19 -08:00
telegraf.go fix(http): filtering telegraf configs only by organization ID 2019-01-09 22:17:19 +01:00
telegraf_test.go fix(http): filtering telegraf configs only by organization ID 2019-01-09 22:17:19 +01: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 fix(http): remove second potention call to w.WriteHeader 2018-12-20 11:11:09 -05:00
user_resource_mapping_service.go feat(auth): rewrite authorization service (#2157) 2018-12-28 15:02:19 -08:00
user_resource_mapping_test.go chore(http): make test diffs more readable (#2203) 2019-01-03 14:29:22 -08:00
user_service.go feat(auth): rewrite authorization service (#2157) 2018-12-28 15:02:19 -08:00
user_test.go fix(http): convert user errors 2018-12-11 20:11:26 -05:00
view_service.go fix(platform): nest view beneath dashboard cell 2019-01-08 18:11:13 -05:00
view_test.go chore(http): make test diffs more readable (#2203) 2019-01-03 14:29:22 -08:00
write_handler.go feat(auth): rewrite authorization service (#2157) 2018-12-28 15:02:19 -08:00
write_handler_test.go test(http): add tests for write client 2018-10-25 19:13:53 -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 {
		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