influxdb/http
Jade McGough 2cd14c7cdb fix failures in variable http testing 2019-06-20 16:25:38 -07:00
..
influxdb
metric
mock
Makefile
README.md chore(http): update error handling example in readme () 2019-05-13 12:50:25 -07:00
api_handler.go feat(authentication): Add cli args for specifying session length and renewal () 2019-05-15 10:16:47 -07:00
api_handler_test.go chore(http): surface unmarshaling failures in tests () 2019-05-08 12:51:03 -07:00
assets.go
auth_service.go refactor(http): move queryd http interface to idpe () 2019-05-09 10:41:14 -07:00
auth_test.go chore(http): surface unmarshaling failures in tests () 2019-05-08 12:51:03 -07:00
authentication_middleware.go feat(authentication): Add cli args for specifying session length and renewal () 2019-05-15 10:16:47 -07:00
authentication_test.go
bucket_service.go feat(influxdb): bucket created and updated time 2019-05-17 11:49:10 -04:00
bucket_test.go feat(influxdb): bucket created and updated time 2019-05-17 11:49:10 -04:00
chronograf_handler.go
client.go refactor(http): move queryd http interface to idpe () 2019-05-09 10:41:14 -07:00
dashboard_service.go fix(influxdb): use influxdb.Error in ID.Decode 2019-05-15 22:53:31 +08:00
dashboard_test.go Merge pull request from zhulongcheng/id-decode-err 2019-05-19 13:55:21 -04:00
debug.go
document_service.go feat(http): add audit log 2019-05-16 10:57:28 -04:00
document_test.go feat(http): add audit log 2019-05-16 10:57:28 -04:00
duration.go
duration_test.go
errors.go fix(http): do not discard non-json encoded errors when using `CheckError` () 2019-05-13 12:02:05 -05:00
errors_test.go
handler.go
handler_test.go
health.go
health_test.go chore(http): surface unmarshaling failures in tests () 2019-05-08 12:51:03 -07:00
label_service.go refactor(http): move queryd http interface to idpe () 2019-05-09 10:41:14 -07:00
label_test.go chore(http): surface unmarshaling failures in tests () 2019-05-08 12:51:03 -07:00
no_assets.go
onboarding.go refactor(http): move queryd http interface to idpe () 2019-05-09 10:41:14 -07:00
onboarding_test.go feat(influxdb): bucket created and updated time 2019-05-17 11:49:10 -04:00
org_service.go Merge pull request from influxdata/no_filter_parameters_provided 2019-05-09 15:14:54 -07:00
org_test.go feat(influxdb): add org create and update time 2019-05-20 17:36:29 -07:00
paging.go
paging_test.go
platform_handler.go feat(authentication): Add cli args for specifying session length and renewal () 2019-05-15 10:16:47 -07:00
query.go refactor(http): make http.Query use FluxCompiler when query is passed as text 2019-06-18 11:45:17 -07:00
query_handler.go Merge remote-tracking branch 'origin/master' into flux-staging 2019-05-09 15:29:00 -04:00
query_handler_test.go
query_test.go refactor(http): make http.Query use FluxCompiler when query is passed as text 2019-06-18 11:45:17 -07:00
ready.go
redoc.go
requests.go
requests_test.go
router.go
router_test.go
scraper_service.go refactor(http): move queryd http interface to idpe () 2019-05-09 10:41:14 -07:00
scraper_service_test.go chore(http): surface unmarshaling failures in tests () 2019-05-08 12:51:03 -07:00
server.go
session_handler.go
session_test.go
source_proxy_service.go refactor(http): move queryd http interface to idpe () 2019-05-09 10:41:14 -07:00
source_service.go refactor(http): move queryd http interface to idpe () 2019-05-09 10:41:14 -07:00
source_service_test.go
status.go
swagger.go
swagger.yml feat(http): adding createdAt variable 2019-06-20 16:25:38 -07:00
swagger_assets.go
swagger_noassets.go
swagger_test.go chore(dep): update kin-openapi to v0.2.0 2019-05-15 00:23:32 +02:00
task_service.go chore(tasks): consolidate task errors into task_errors.go 2019-06-17 16:03:18 -07:00
task_service_test.go chore(tasks): consolidate task errors into task_errors.go 2019-06-17 16:03:18 -07:00
task_test.go fix(task): Ensure tasks clean up user resource maps () 2019-06-11 10:28:09 -06:00
telegraf.go fix(http): rename OrgID to orgID 2019-06-06 22:12:32 +08:00
telegraf_test.go fix(http): rename OrgID to orgID 2019-06-06 22:12:32 +08:00
tokens.go
tokens_test.go
transport.go refactor(http): move queryd http interface to idpe () 2019-05-09 10:41:14 -07:00
usage_service.go
user_resource_mapping_service.go refactor(http): move queryd http interface to idpe () 2019-05-09 10:41:14 -07:00
user_resource_mapping_test.go chore(http): surface unmarshaling failures in tests () 2019-05-08 12:51:03 -07:00
user_service.go refactor(http): move queryd http interface to idpe () 2019-05-09 10:41:14 -07:00
user_test.go
variable_service.go fix(influxdb): use influxdb.Error in ID.Decode 2019-05-15 22:53:31 +08:00
variable_test.go fix failures in variable http testing 2019-06-20 16:25:38 -07:00
write_handler.go refactor(http): move queryd http interface to idpe () 2019-05-09 10:41:14 -07:00
write_handler_test.go

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