influxdb/http
davidby-influx 5ef6b837b0
chore: remove erroneous dependency on istio (#21486)
Use same logging functions as the rest of the points code.
2021-05-17 12:06:06 -07:00
..
influxdb refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
legacy refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
metric refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
mock
mocks refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
points chore: remove erroneous dependency on istio (#21486) 2021-05-17 12:06:06 -07:00
Makefile
README.md
api_handler.go fix(upgrade): ensure query config written by `influxd upgrade` is valid (#21321) 2021-04-28 15:41:44 -04:00
api_handler_test.go refactor(kv): delete deprecated kv service code 2020-11-16 12:41:26 +00:00
assets.go
auth_service.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
auth_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
authentication_middleware.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
authentication_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
backup_service.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
check_service.go feat: expose task id to checks and notification rules (#21236) 2021-04-15 16:03:39 -07:00
check_test.go feat: expose task id to checks and notification rules (#21236) 2021-04-15 16:03:39 -07:00
chronograf_handler.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
client.go refactor(kv): delete deprecated kv service code 2020-11-16 12:41:26 +00:00
debug.go
delete_handler.go fix: add limits to delete api start and stop times (#21369) 2021-05-05 11:01:32 -04:00
delete_test.go fix: add limits to delete api start and stop times (#21369) 2021-05-05 11:01:32 -04:00
document_service.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
document_service_test.go chore(documents): reinstate minimal read-only document store for dashboard templates 2020-11-16 12:41:26 +00:00
document_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
duration.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
duration_test.go
errors.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
errors_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
handler.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
handler_test.go feat(http): add config option to disable metrics endpoint in influxd (#20963) 2021-03-15 15:11:23 -04:00
health.go
health_test.go fix: correct various typos (#19987) 2020-11-11 13:54:21 -05:00
label_service.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
label_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
legacy.go
middleware.go refactor: modularize backup/restore behavior so it's possible to test using `go test` (#20991) 2021-03-23 07:28:38 -04:00
middleware_test.go fix(http): don't log bodies of V1 write requests 2020-11-23 14:48:20 -05:00
no_assets.go
notification_endpoint.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
notification_endpoint_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
notification_rule.go feat: expose task id to checks and notification rules (#21236) 2021-04-15 16:03:39 -07:00
notification_rule_test.go refactor: refactor for tasks backport (#21117) 2021-04-07 14:42:55 -04:00
paging_test.go
platform_handler.go
proxy_handler.go
query.go refactor: refactor for tasks backport (#21117) 2021-04-07 14:42:55 -04:00
query_handler.go refactor: refactor for tasks backport (#21117) 2021-04-07 14:42:55 -04:00
query_handler_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
query_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
ready.go fix(http): fix /ready response content type 2020-11-05 19:20:57 +01:00
ready_test.go fix(http): fix /ready response content type 2020-11-05 19:20:57 +01:00
redoc.go
requests.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
requests_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
restore_service.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
router.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
router_test.go
scraper_service.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
scraper_service_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
server.go
service_test.go
session_handler.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
session_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
source_proxy_service.go
source_service.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
source_service_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
swagger.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
swagger.yml chore: add comment to unused swagger definition pointing to replacement (#21283) 2021-05-06 11:23:31 -06:00
swaggerV1Compat.yml fix(influxql): set correct Content-Type on v1 query responses (#20565) 2021-01-22 07:31:52 -08:00
swagger_assets.go
swagger_noassets.go
swagger_test.go
task_service.go refactor: refactor for tasks backport (#21117) 2021-04-07 14:42:55 -04:00
task_service_test.go refactor: refactor for tasks backport (#21117) 2021-04-07 14:42:55 -04:00
telegraf.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
telegraf_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
tokens.go
tokens_test.go
ua.go
ua_test.go
user_resource_mapping_service.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
user_resource_mapping_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
user_service.go feat(users): list users via the API now supports `offset`, `limit` or `after` parameter (#21367) 2021-05-12 14:09:52 +02:00
user_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
variable_service.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
variable_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
write_handler.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
write_handler_test.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
write_usage_recorder.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04: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