influxdb/http
Jorge Landivar 9e6a2247c0
Merge pull request #12134 from zhulongcheng/validate-task-status
fix(http): validate status for creating and updating a task
2019-02-27 13:08:06 -06:00
..
influxdb chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
mock updates based on review 2019-01-23 09:29:50 -08:00
Makefile Remove typescript client generation 2019-02-05 17:13:57 -08:00
README.md
api_handler.go fix(http): separate InfluxQL and Flux services 2019-02-22 10:31:20 -07:00
api_handler_test.go chore(http): make test diffs more readable (#2203) 2019-01-03 14:29:22 -08:00
assets.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
auth_service.go fix(influxdb): revert functionality added in pr#10947 2019-02-06 15:54:09 -05:00
auth_test.go fix(influxdb): revert functionality added in pr#10947 2019-02-06 15:54:09 -05:00
authentication_middleware.go fix(ui): add session timeout notification (#11281) 2019-01-18 17:43:00 -05:00
authentication_test.go fix(ui): add session timeout notification (#11281) 2019-01-18 17:43:00 -05:00
bucket_service.go fix(http): pass resource type through to label handlers 2019-02-07 19:08:56 -05:00
bucket_test.go feat(http): construct org handler by OrgBackend 2019-02-06 19:18:23 +01:00
chronograf_handler.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
client.go Rename all occurences of Macro to Variable 2019-02-14 13:21:57 -08:00
dashboard_service.go fix(http): pass resource type through to label handlers 2019-02-07 19:08:56 -05:00
dashboard_test.go test(http): add tests to ensure labels can be added to resources 2019-02-07 19:08:59 -05:00
debug.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
duration.go update(http): remove kerrors, change the default to be influxdb error 2019-01-25 10:29:26 -08:00
duration_test.go
errors.go update(http): changes based on review 2019-01-25 10:31:25 -08:00
errors_test.go fix(http): fix task errors 2019-01-25 10:31:24 -08:00
handler.go update(http): remove kerrors, change the default to be influxdb error 2019-01-25 10:29:26 -08:00
health.go
health_test.go chore(http): make test diffs more readable (#2203) 2019-01-03 14:29:22 -08:00
label_service.go fix(http): pass resource type through to label handlers 2019-02-07 19:08:56 -05:00
label_test.go fix root labels endpoint 2019-02-07 14:21:49 -08:00
no_assets.go feat(http): host swagger docs at /docs and /api/v2/swagger.json 2019-01-22 11:16:27 -06:00
onboarding.go fix(http): rename platform to influxdb in package 2019-02-07 19:08:41 -05:00
onboarding_test.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
org_service.go fix(http): return an empty list of operation logs if not found 2019-02-22 14:18:13 +08:00
org_test.go feat(http): construct org handler by OrgBackend 2019-02-06 19:18:23 +01:00
paging.go update(http): remove kerrors, change the default to be influxdb error 2019-01-25 10:29:26 -08:00
paging_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
platform_handler.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
proto.go feat(http): dashboards protos pagination 2019-01-17 12:35:00 +01:00
proto_test.go feat(http): dashboards protos pagination 2019-01-17 12:35:00 +01:00
proxy_query_service.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
query.go feat(http): define external preprocessing options via http api (#11939) 2019-02-21 13:11:50 -08:00
query_handler.go fix(http): separate InfluxQL and Flux services 2019-02-22 10:31:20 -07:00
query_handler_test.go refactor(query/stlib): move from implementation from Flux 2019-02-05 17:46:46 +01:00
query_service.go feat(storage/reads): add scanned values and bytes metadata to the query (#12156) 2019-02-25 14:44:18 -06:00
query_test.go feat(http): define external preprocessing options via http api (#11939) 2019-02-21 13:11:50 -08:00
ready.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
redoc.go feat(http): host swagger docs at /docs and /api/v2/swagger.json 2019-01-22 11:16:27 -06:00
requests.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
requests_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
router.go log api panic with a stack trace 2019-01-15 11:58:42 +08:00
router_test.go add test for logging http panics 2019-01-21 23:29:28 +08:00
scraper_service.go Merge pull request #11768 from bednar/scraper-backend 2019-02-19 18:57:09 -06:00
scraper_service_test.go feat(http): construct scraper handler by ScraperBackend 2019-02-06 19:18:23 +01:00
server.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
session_handler.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
session_test.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
source_proxy_service.go fix(http): ensure response body always closed 2019-01-18 14:55:48 -08:00
source_service.go feat(http): construct source handler by SourceBackend 2019-02-06 19:18:23 +01:00
source_service_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
status.go
swagger.go feat(http): host swagger docs at /docs and /api/v2/swagger.json 2019-01-22 11:16:27 -06:00
swagger.yml Merge pull request #12165 from influxdata/flux-staging 2019-02-26 13:12:37 -06:00
swagger_test.go Remove validation of cur_swagger 2019-02-06 16:28:49 -08:00
task_service.go Merge pull request #12134 from zhulongcheng/validate-task-status 2019-02-27 13:08:06 -06:00
task_service_test.go Merge pull request #12134 from zhulongcheng/validate-task-status 2019-02-27 13:08:06 -06:00
task_test.go Populate organization name on task create action (#12004) 2019-02-20 13:36:35 -07:00
telegraf.go fix(http): pass resource type through to label handlers 2019-02-07 19:08:56 -05:00
telegraf_test.go Enable adding a description to a telegraf config 2019-02-21 14:06:04 -08:00
tokens.go
tokens_test.go
usage_service.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
user_resource_mapping_service.go feat(http): construct member handler by MemberBackend 2019-02-06 19:18:23 +01:00
user_resource_mapping_test.go feat(http): construct member handler by MemberBackend 2019-02-06 19:18:23 +01:00
user_service.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
user_test.go feat(kv): implemented key/value store with end-to-end integration tests 2019-02-19 17:47:19 -06:00
variable_service.go Rename all occurences of Macro to Variable 2019-02-14 13:21:57 -08:00
variable_test.go Rename all occurences of Macro to Variable 2019-02-14 13:21:57 -08:00
view_service.go feat(http): view backend for view http handler 2019-02-06 19:18:23 +01:00
view_test.go feat(http): view backend for view http handler 2019-02-06 19:18:23 +01:00
write_handler.go chore(influxdb): add context to storage.PointsWriter 2019-02-25 11:11:20 -05:00
write_handler_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51: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 {
		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