influxdb/http
Lorenzo Affetti fd729f8a38
fix(http): refactor document service and fix auth
2020-03-09 20:32:31 +01:00
..
influxdb
metric
mock
Makefile
README.md
api_handler.go fix(http): refactor document service and fix auth 2020-03-09 20:32:31 +01: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
auth_service.go
auth_test.go fix(kv): remove URM and Authorizations by UserID Index (#16852) 2020-02-13 15:27:57 +00:00
authentication_middleware.go
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 fix(backup): handle backup with no credentials file (#16877) 2020-02-14 09:19:58 -08:00
bucket_service.go chore(http): export NewBucketResponse function 2020-02-06 16:01:30 -08:00
bucket_service_test.go chore(http): refactor bkt service to use API type 2020-02-04 14:41:56 -08:00
check_service.go fix(checks): typo causing panic in checks endpoint 2020-02-28 10:54:06 -06: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
client.go refactor(api): add HTTPC client service to notification rules API (#17087) 2020-03-04 16:32:33 -08:00
dashboard_service.go fix(http): improve error handling and response to api consumer for org service 2020-02-04 09:43:51 -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
delete_handler.go
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 fix(http): refactor document service and fix auth 2020-03-09 20:32:31 +01:00
document_service_test.go fix(http): refactor document service and fix auth 2020-03-09 20:32:31 +01:00
document_test.go fix(http): refactor document service and fix auth 2020-03-09 20:32:31 +01:00
duration.go
duration_test.go
errors.go fix: remove duplicate content types from httpc requests 2020-02-11 11:14:44 -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
handler_test.go
health.go
health_test.go
helpers.go fix(http): improve error handling and response to api consumer for org service 2020-02-04 09:43:51 -08:00
label_service.go
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
no_assets.go
notification_endpoint.go fix(http): improve error handling and response to api consumer for org service 2020-02-04 09:43:51 -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 refactor(api): add HTTPC client service to notification rules API (#17087) 2020-03-04 16:32:33 -08:00
notification_rule_test.go
onboarding.go chore(http): export NewBucketResponse function 2020-02-06 16:01:30 -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 feat(http): add secret service 2020-02-07 15:54:28 -05:00
org_service_test.go feat(http): add secret service 2020-02-07 15:54:28 -05:00
paging.go fix(http): improve error handling and response to api consumer for org service 2020-02-04 09:43:51 -08:00
paging_test.go fix(http): improve error handling and response to api consumer for org service 2020-02-04 09:43:51 -08:00
platform_handler.go
query.go fix(http): Revert d51447fc34. 2020-03-06 12:24:12 -05:00
query_handler.go fix(influxql): add explicit routing to influxql service (#16832) 2020-02-12 10:07:29 -07:00
query_handler_test.go fix(http): Revert d51447fc34. 2020-03-06 12:24:12 -05:00
query_test.go fix(http): Revert d51447fc34. 2020-03-06 12:24:12 -05:00
ready.go
redoc.go
requests.go
requests_test.go
router.go
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
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
session_handler.go fix(http): Add secure flag to session cookie (#16574) 2020-02-25 14:37:11 -08:00
session_test.go
source_proxy_service.go
source_service.go chore(http): refactor bkt service to use API type 2020-02-04 14:41:56 -08:00
source_service_test.go
swagger.go
swagger.yml feat(pkger): add support for exporting all and filter by resource types 2020-03-06 11:51:38 -08:00
swagger_assets.go
swagger_noassets.go
swagger_test.go
task_service.go refactor(task/backend): move the task/backend constants to the global package (#17133) 2020-03-06 16:19:32 -06:00
task_service_test.go refactor(task/backend): move the task/backend constants to the global package (#17133) 2020-03-06 16:19:32 -06:00
telegraf.go
telegraf_test.go
tokens.go
tokens_test.go
ua.go
ua_test.go
usage_service.go
user_resource_mapping_service.go
user_resource_mapping_test.go
user_service.go fix(http): improve error handling and response to api consumer for org service 2020-02-04 09:43:51 -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 fix(http): improve error handling and response to api consumer for org service 2020-02-04 09:43:51 -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
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