influxdb/http
Jared Scheib 20a06bea98 fix(http): correct typo in authentication scheme error 2018-10-05 12:55:53 -07:00
..
influxdb
README.md Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
api_handler.go Integrate WAL into engine 2018-10-05 12:44:27 +01:00
assets.go ci(goreleaser): override goreleaser build flags 2018-09-26 00:32:44 -05:00
auth_service.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
auth_test.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
authentication_middleware.go fix(http): correct typo in authentication scheme error 2018-10-05 12:55:53 -07: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(http): add owner/member endpoints for bucket service 2018-10-02 15:31:18 -07:00
bucket_test.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
chronograf_handler.go
client.go
dashboard_service.go feat(http): add owner/member endpoints to dashboards 2018-10-02 15:31:18 -07:00
dashboard_test.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
duration.go chore(http/duration): mention need for longer duration literals 2018-09-25 18:11:20 -05:00
duration_test.go
errors.go feat(http): add authentication handler middleware 2018-10-02 14:11:44 -04:00
errors_test.go
flux_lang.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
flux_lang_test.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
handler.go Merge pull request #527 from influxdata/js-remove-error-field-with-no-error 2018-10-04 11:51:15 -05:00
health.go
macro_service.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
macro_test.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
onboarding.go add setup service 2018-10-02 15:58:27 -04:00
org_service.go feat(http): move member/owner routes to factories 2018-10-02 15:31:18 -07:00
org_test.go
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
query_handler.go refactor(http): prefer Addr over URL in field names 2018-10-04 13:59:58 -07:00
query_handler_test.go refactor(http): prefer Addr over URL in field names 2018-10-04 13:59:58 -07:00
query_service.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
query_test.go
requests.go
scraper_service.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
server.go
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 chore(http): update signin route for session test 2018-10-02 14:11:44 -04:00
source_proxy_service.go refactor(http): prefer Addr over URL in field names 2018-10-04 13:59:58 -07:00
source_service.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
status.go
swagger.yml feat(http/view): add storing LogView config 2018-10-02 19:27:49 -05:00
task_service.go feat(task): explicitly handle orgs when finding run logs 2018-10-01 11:05:56 -07:00
tokens.go
tokens_test.go
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 feat(http): move member/owner routes to factories 2018-10-02 15:31:18 -07:00
user_service.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
user_test.go
view_service.go Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
view_test.go feat: line plus single stat view type (#936) 2018-10-03 10:17:14 -07:00
write_handler.go Integrate WAL into engine 2018-10-05 12:44:27 +01: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