influxdb/http
Johnny Steenbergen 343c9b166b chore(pkger): consolidate pkger http server into pkger
this is the last step for pkger to follow the service definition pattern
that is in the works. Some bits from http were moved into kit/transport/http
for reusability. End result is to hopefully axe http pkg for resuable types in
kit. Long ways off still...
2020-02-03 12:39:47 -08:00
..
influxdb fix(tasks): tasks look up system bucket id 2019-10-21 14:48:47 -07:00
metric test(http): add unit tests for /api/v2/write 2019-09-12 13:39:24 -05: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 chore(http): update error handling example in readme (#13874) 2019-05-13 12:50:25 -07:00
api_handler.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
api_handler_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -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 chore(http): refactor auth service http client to use httpc.Client 2019-12-17 12:18:16 -08:00
auth_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
authentication_middleware.go chore: Remove several instances of WithLogger (#15996) 2019-12-04 15:10:23 -08:00
authentication_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
backup_service.go feat(storage): implement backup and restore (#16504) 2020-01-21 14:22:45 -08:00
bucket_service.go feat(httpc): extend httpc Client with JSON shorthand methods 2019-12-12 07:44:31 -08:00
bucket_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
check_service.go feat(checks): Add custom check type (#16350) 2019-12-31 20:21:48 +00:00
check_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
chronograf_handler.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
client.go feat(storage): implement backup and restore (#16504) 2020-01-21 14:22:45 -08:00
dashboard_service.go refactor(ui/views): normalization (#16616) 2020-01-23 13:17:08 -08:00
dashboard_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
debug.go add spans to multiple services 2019-03-07 12:12:31 -07:00
delete_handler.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
delete_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
document_service.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
document_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08: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): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
errors_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
handler.go chore(http): fixup middleware stack and normalize metrics into same buckets for id fields 2020-01-08 15:05:34 -08:00
handler_test.go chore(http): fixup middleware stack and normalize metrics into same buckets for id fields 2020-01-08 15:05:34 -08: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
label_service.go feat(httpc): extend httpc Client with JSON shorthand methods 2019-12-12 07:44:31 -08:00
label_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
middleware.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
middleware_test.go chore(http): fixup middleware stack and normalize metrics into same buckets for id fields 2020-01-08 15:05:34 -08: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 feat(pkger): add support for secret references to notification endpoints parsing 2019-12-17 11:33:34 -08:00
notification_endpoint_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
notification_rule.go feat(ui): added notification rule & checks last run status & error (#16338) 2019-12-27 10:14:34 -08:00
notification_rule_test.go feat(ui): added notification rule & checks last run status & error (#16338) 2019-12-27 10:14:34 -08:00
onboarding.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
onboarding_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
org_service.go chore(http): fixup middleware stack and normalize metrics into same buckets for id fields 2020-01-08 15:05:34 -08:00
org_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
paging.go chore(http): refactor org service to use httpc client 2019-12-10 10:49:22 -08:00
paging_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
platform_handler.go chore(http): fixup middleware stack and normalize metrics into same buckets for id fields 2020-01-08 15:05:34 -08:00
query.go feat(task): make task query without returning content 2020-01-17 16:51:01 +01:00
query_handler.go feat(task): make task query without returning content 2020-01-17 16:51:01 +01:00
query_handler_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
query_test.go refactor(http): make http.Query use FluxCompiler when query is passed as text 2019-06-18 11:45:17 -07:00
ready.go chore(http): fixup middleware stack and normalize metrics into same buckets for id fields 2020-01-08 15:05:34 -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 feat(http): add predicate endpoint 2019-10-18 12:02:53 -04:00
requests_test.go feat(http): add predicate endpoint 2019-10-18 12:02:53 -04:00
router.go chore(http): fixup middleware stack and normalize metrics into same buckets for id fields 2020-01-08 15:05:34 -08:00
router_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
scraper_service.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
scraper_service_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
server.go chore: Remove several instances of WithLogger (#15996) 2019-12-04 15:10:23 -08:00
session_handler.go chore(http): refactor api handler take advantage of route tree instead of switch statement 2019-12-11 09:53:29 -08:00
session_test.go chore: Remove several instances of WithLogger (#15996) 2019-12-04 15:10:23 -08:00
source_proxy_service.go fix client tracing propagation from doubly recording (#14204) 2019-06-26 00:48:15 -05:00
source_service.go chore(http): refactor source http service client to use httpc.Client 2019-12-17 13:19:33 -08:00
source_service_test.go fix(http): fix source health endpoint 2019-03-13 11:04:16 -04:00
swagger.go chore: Remove several instances of WithLogger (#15996) 2019-12-04 15:10:23 -08:00
swagger.yml refactor(ui): normalize cells (#16569) 2020-01-21 10:54:24 -08: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 fix(tasks): add content length check to decode force run request (#16458) 2020-01-08 18:55:42 -08:00
task_service_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
telegraf.go fix(http): fix /telegrafs panics when using org=org_name parameter 2020-01-27 11:04:11 +08:00
telegraf_test.go fix(http): fix /telegrafs panics when using org=org_name parameter 2020-01-27 11:04:11 +08: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 chore: Remove several instances of WithLogger (#15996) 2019-12-04 15:10:23 -08:00
user_resource_mapping_service.go feat(httpc): extend httpc Client with JSON shorthand methods 2019-12-12 07:44:31 -08:00
user_resource_mapping_test.go chore: Remove several instances of WithLogger (#15996) 2019-12-04 15:10:23 -08:00
user_service.go chore(http): refactor user http service to use the httpc.Client 2019-12-17 12:49:14 -08:00
user_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
variable_service.go feat(httpc): extend httpc Client with JSON shorthand methods 2019-12-12 07:44:31 -08:00
variable_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39:47 -08:00
write_handler.go feat(http): Enforce limits in write handler and expose config options 2020-01-14 09:13:40 -07:00
write_handler_test.go chore(http): pull out eror handler into kit pkg for portability 2020-02-03 12:39: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 {
		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