influxdb/http
Chris Goller 310a64fc97 feat(http): use proxy request in flux service 2018-09-13 13:00:27 -05: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 migrate(platform): move public dependencies into platform 2018-05-14 17:12:53 -04:00
assets.go fix(http): fix chronograf build asset paths 2018-07-24 14:25:06 -04:00
auth_service.go feat(http): add links to auth service response structures 2018-09-12 11:44:44 -04:00
auth_test.go feat(http): add links to auth service response structures 2018-09-12 11:44:44 -04:00
bucket_service.go Add org as a query param to bucket 2018-09-11 15:24:23 +02:00
bucket_test.go Add org as a query param to bucket 2018-09-11 15:24:23 +02:00
chronograf_handler.go feat(platform): move chronogaf v2 dashboards to platform 2018-08-24 13:22:58 -04:00
client.go feat: Add zap opentracing.Tracer 2018-08-16 14:32:04 -06:00
dashboard_service.go feat(platform): move chronogaf v2 dashboards to platform 2018-08-24 13:22:58 -04:00
dashboard_test.go feat(platform): move chronogaf v2 dashboards to platform 2018-08-24 13:22:58 -04:00
errors.go chore(http): return early if a critical error occurs in write path org/bucket lookup (#767) 2018-09-05 15:53:57 -07:00
errors_test.go fix(http): increase errorHeaderMaxLength 2018-08-01 16:04:36 -07:00
external_query_handler.go feat(http): update /v2/query client to send query.Request with auth 2018-09-13 10:39:08 -05:00
flux_lang.go refactor: Migrate query package to influxdata/flux repository 2018-09-06 11:13:48 -07:00
handler.go feat(http) add top-level links and health 2018-09-04 17:28:59 -05:00
health.go feat(http) add top-level links and health 2018-09-04 17:28:59 -05:00
org_service.go Add links to orgs http handler 2018-09-11 14:55:29 +02:00
platform_handler.go feat(http) add top-level links and health 2018-09-04 17:28:59 -05:00
proxy_query_service.go chore: Updates to be able to remove platform as a dependency of Flux 2018-09-12 10:18:54 -06:00
query.go feat(http): update /v2/query client to send query.Request with auth 2018-09-13 10:39:08 -05:00
query_handler.go feat(http): use proxy request in flux service 2018-09-13 13:00:27 -05:00
query_handler_test.go feat(http): use proxy request in flux service 2018-09-13 13:00:27 -05:00
query_service.go chore: Updates to be able to remove platform as a dependency of Flux 2018-09-12 10:18:54 -06:00
requests.go feat(http): add flux endpoint /v2/query influx 2018-09-04 16:08:00 -05:00
server.go fix(http): use a duration literal for the server's shutdown timeout 2018-06-11 12:39:06 -05:00
source_proxy_service.go chore: Updates to be able to remove platform as a dependency of Flux 2018-09-12 10:18:54 -06:00
source_service.go chore: Updates to be able to remove platform as a dependency of Flux 2018-09-12 10:18:54 -06:00
status.go feat: Add optional http logging to handler 2018-07-30 16:16:37 -06:00
swagger.yml Add links to orgs http handler 2018-09-11 14:55:29 +02:00
task_service.go feat(task): create validation layer for TaskService (#591) 2018-08-20 15:15:04 -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(errors): Update Fluxd errors 2018-06-28 16:56:35 -06:00
user_service.go chore(http): return early if a critical error occurs in write path org/bucket lookup (#767) 2018-09-05 15:53:57 -07:00
view_service.go feat(platform): move chronogaf v2 dashboards to platform 2018-08-24 13:22:58 -04:00
view_test.go feat(platform): move chronogaf v2 dashboards to platform 2018-08-24 13:22:58 -04:00
write_handler.go Merge pull request #771 from influxdata/feature/query 2018-09-06 11:27:54 -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", "/v1/things", h.handlePostThing)
	h.HandlerFunc("GET", "/v1/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 /v1/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