influxdb/http
Jade McGough a9b9a0b538
Merge pull request #1185 from influxdata/user-mapping-response
fix User mapping response
2018-10-26 10:25:45 -07:00
..
influxdb chore: Updates to be able to remove platform as a dependency of Flux 2018-09-12 10:18:54 -06:00
README.md Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
api_handler.go fixed inconsistency in links 2018-10-26 17:05:06 +02:00
assets.go ci(goreleaser): override goreleaser build flags 2018-09-26 00:32:44 -05:00
auth_service.go feat(platform/id): using uint64 for platform.IDs 2018-10-11 09:54:18 -05:00
auth_test.go refactor(testing): rename MustIDFromString to MustIDBase16 2018-10-11 09:54:19 -05:00
authentication_middleware.go tests(http): add tests for creation without IDs 2018-10-12 10:03:06 +02: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(orgs): update org bucket name and retention rule (#1181) 2018-10-25 15:23:05 -07:00
bucket_test.go feat(orgs): update org bucket name and retention rule (#1181) 2018-10-25 15:23:05 -07:00
chronograf_handler.go feat(platform): move chronogaf v2 dashboards to platform 2018-08-24 13:22:58 -04:00
client.go feat(http): add MacroService client 2018-09-20 09:32:04 -07:00
dashboard_service.go feat(http): validate dashboard cell updates (#1199) 2018-10-25 16:05:12 -07:00
dashboard_test.go feat(platform): add meta field to dashboards 2018-10-23 13:43:06 -04: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 http(write): support timestamp precision 2018-10-25 21:23:50 -05:00
errors_test.go fix(http): increase errorHeaderMaxLength 2018-08-01 16:04:36 -07:00
handler.go fix(http): links updated to match swagger doc 2018-10-06 10:11:20 -05:00
health.go fix(http): links updated to match swagger doc 2018-10-06 10:11:20 -05:00
macro_service.go feat(platform/id): method to create an invalid id 2018-10-11 09:54:18 -05:00
macro_test.go refactor(testing): rename MustIDFromString to MustIDBase16 2018-10-11 09:54:19 -05:00
onboarding.go add setup service 2018-10-02 15:58:27 -04:00
org_service.go run go fmt 2018-10-16 15:18:22 -07:00
org_test.go fix(http): add zero value mock implementation of u/r mapping service 2018-10-16 15:14:16 -07:00
platform_handler.go fix(http): add {Authorization,Session}Service to AuthenticationHandler 2018-10-02 16:10:41 -04:00
proxy_query_service.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
query.go moved to_http and to_kafka to flux and update to latest flux (#1175) 2018-10-24 12:33:43 -04:00
query_handler.go refactor(token): remove context token 2018-10-19 12:39:54 -04:00
query_handler_test.go Merge pull request #887 from influxdata/nc-flux-org-test 2018-10-08 14:04:19 -06:00
query_service.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
query_test.go Add tasks to the influx cli (#1163) 2018-10-23 11:51:13 -06:00
requests.go feat(http): add flux endpoint /v2/query influx 2018-09-04 16:08:00 -05:00
scraper_service.go fix scraper id 2018-10-12 10:03:06 +02:00
scraper_service_test.go fix scraper id 2018-10-12 10:03:06 +02:00
server.go refactor: move the logger package from influxdb to platform 2018-10-10 15:49:07 -05:00
session_handler.go feat(platform): add /api/v2/sign{in,out} routes to platform handler 2018-10-02 14:11:44 -04:00
session_test.go fix(http): session tests using uint64 IDs now 2018-10-11 09:54:18 -05:00
source_proxy_service.go chore(fmt): fix and simplify go formatting 2018-10-08 17:23:54 -05:00
source_service.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
status.go feat: Add optional http logging to handler 2018-07-30 16:16:37 -06:00
swagger.yml feat(platform): add meta field to dashboards 2018-10-23 13:43:06 -04:00
swagger_test.go test(http): remove spurious byte cast 2018-10-23 08:47:21 -05:00
task_service.go chore(http): add links to tasks and runs responses 2018-10-24 14:04:08 -07:00
task_test.go Add tasks to the influx cli (#1163) 2018-10-23 11:51:13 -06:00
tokens.go feat(auth): allow authorizations to be enabled/disabled 2018-08-29 13:07:38 -05:00
tokens_test.go feat(auth): allow authorizations to be enabled/disabled 2018-08-29 13:07:38 -05:00
usage_service.go fix(usage): use stop time as upper bound (#932) 2018-10-02 12:00:23 -07:00
user_resource_mapping_service.go fix(http): add links to user resource response 2018-10-25 14:42:26 -07:00
user_service.go chore(http): update user server after rebase 2018-10-11 09:54:19 -05:00
user_test.go feat(testing): add service testing helpers 2018-09-14 21:08:49 -05:00
view_service.go run go fmt 2018-10-16 15:18:22 -07:00
view_test.go fix(http): add zero value mock implementation of u/r mapping service 2018-10-16 15:14:16 -07:00
write_handler.go http(write): support timestamp precision 2018-10-25 21:23:50 -05: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