influxdb/http
Johnny Steenbergen 9c525ad413 feat(pkger): add ability to export all and defined list of resources via influx cli to a package
refers: #15921
2019-11-21 10:55:12 -08:00
..
influxdb
metric
mock
Makefile
README.md
api_handler.go feat: add support for setting password and org for a new user in the cli 2019-11-20 09:16:31 -08:00
api_handler_test.go
assets.go
auth_service.go feat(createdAt): Added createdat and updatedAt to Authorization (#15784) 2019-11-07 06:46:30 -08:00
auth_test.go feat(createdAt): Added createdat and updatedAt to Authorization (#15784) 2019-11-07 06:46:30 -08:00
authentication_middleware.go chore: refactor password service to provide userID instead of name 2019-11-20 09:16:31 -08:00
authentication_test.go
bucket_service.go chore(http): add missing client methods for label/buckets GET reqs 2019-11-01 11:59:09 -07:00
bucket_test.go
check_service.go fix(http/links): Added query links to checks and notificationRules response, updated swagger.yml (#15807) 2019-11-08 08:23:38 -08:00
check_test.go fix(http/links): Added query links to checks and notificationRules response, updated swagger.yml (#15807) 2019-11-08 08:23:38 -08:00
chronograf_handler.go
client.go
dashboard_service.go feat(pkger): add dashboard support to pkger 2019-11-01 12:20:54 -07:00
dashboard_test.go
debug.go
delete_handler.go
delete_test.go
document_service.go
document_test.go
duration.go
duration_test.go
errors.go
errors_test.go
handler.go
handler_test.go
health.go
health_test.go
label_service.go chore(http): add missing client methods for label/buckets GET reqs 2019-11-01 11:59:09 -07:00
label_test.go
middleware.go
middleware_test.go
no_assets.go
notification_endpoint.go
notification_endpoint_test.go
notification_rule.go fix(http/links): Added query links to checks and notificationRules response, updated swagger.yml (#15807) 2019-11-08 08:23:38 -08:00
notification_rule_test.go fix(http/links): Added query links to checks and notificationRules response, updated swagger.yml (#15807) 2019-11-08 08:23:38 -08:00
onboarding.go
onboarding_test.go feat(createdAt): Added createdat and updatedAt to Authorization (#15784) 2019-11-07 06:46:30 -08:00
org_service.go
org_test.go
paging.go
paging_test.go
pkger_http_server.go feat(pkger): extend apply HTTP API to return parse err with 422 resp 2019-11-14 12:11:13 -08:00
pkger_http_server_test.go feat(pkger): add ability to export all and defined list of resources via influx cli to a package 2019-11-21 10:55:12 -08:00
pkger_parse_err_test.go feat(pkger): extend apply HTTP API to return parse err with 422 resp 2019-11-14 12:11:13 -08:00
platform_handler.go feat(pkger): add pgker http server to the api handler 2019-11-07 09:44:24 -08:00
query.go
query_handler.go
query_handler_test.go
query_test.go
ready.go
redoc.go
requests.go
requests_test.go
router.go chore(http): provide mountable router for registering routes on APIHandler 2019-11-07 09:44:24 -08:00
router_test.go
scraper_service.go
scraper_service_test.go
server.go
session_handler.go chore: refactor password service to provide userID instead of name 2019-11-20 09:16:31 -08:00
session_test.go chore: refactor password service to provide userID instead of name 2019-11-20 09:16:31 -08:00
source_proxy_service.go
source_service.go
source_service_test.go
status.go
swagger.go
swagger.yml [chore/swagger] updated summaries and descriptions in swagger.yml (#15994) 2019-11-20 16:33:34 -07:00
swagger_assets.go
swagger_noassets.go
swagger_test.go
task_service.go refactor(tasks): use go Time for Task CreatedAt, UpdatedAt, LatestCompleted, Offset (#15672) 2019-11-12 17:13:56 -08:00
task_service_test.go
telegraf.go
telegraf_test.go
tokens.go
tokens_test.go
transport.go
usage_service.go
user_resource_mapping_service.go
user_resource_mapping_test.go
user_service.go feat: add support for setting password and org for a new user in the cli 2019-11-20 09:16:31 -08:00
user_test.go feat: add support for setting password and org for a new user in the cli 2019-11-20 09:16:31 -08:00
variable_service.go
variable_test.go feat(kv): unique variable names (#15695) 2019-11-04 14:28:21 -06:00
write_handler.go
write_handler_test.go

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