influxdb/http
Chris Goller 623224614e feat(kv): random ids without comma, space, backslash for org and bucket
At times snowflake id generation would create org and bucket IDs with
characters that had special meaning for the storage engine.

The storage engine concats the org and bucket bytes together into a
single 128 bit value.  That value is used in the old measurement
section.  Measurement was transformed into the tag, _measurement.

However, certain properties of the older measurement data location
are still required for the org/bucket bytes.  We cannot have
commas, spaces, nor backslashes.

This PR puts a specific ID generator in place during the creation of
orgs and buckets.  The IDs are just random numbers but with each
of the restricted chars incremented by one.  While this changes the
entropy distribution somewhat, it does not matter too much for our
purposes.

... because now org and bucket ids are checked for previous existence
transactionally in the key-value stores.  If the ID does already exist
then we try to generate a new key up to 100 times.
2019-09-08 01:50:05 -05:00
..
influxdb fix client tracing propagation from doubly recording (#14204) 2019-06-26 00:48:15 -05:00
metric feat(influxdb): add query/write http event recorders 2019-04-17 08:33:31 -04: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 chore(http): update error handling example in readme (#13874) 2019-05-13 12:50:25 -07:00
api_handler.go fix(http): fix notfication endpoint secret store 2019-08-28 16:02:17 -04:00
api_handler_test.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04: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 Merge pull request #14339 from influxdata/add_debug_log 2019-07-15 17:08:41 -04:00
auth_test.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
authentication_middleware.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
authentication_test.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
bucket_service.go feat(http): Add bucket types (#15045) 2019-09-06 17:52:54 -07:00
bucket_test.go feat(kv): random ids without comma, space, backslash for org and bucket 2019-09-08 01:50:05 -05:00
check_service.go fix(http): remove non-txn task Create/Update/Delete for checks 2019-08-26 07:55:43 -04:00
check_test.go fix(notification/check): rename _check_type to _type in check object in flux 2019-08-30 10:54:16 -04:00
chronograf_handler.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
client.go fix client tracing propagation from doubly recording (#14204) 2019-06-26 00:48:15 -05:00
dashboard_service.go feat(http): add auth debug log 2019-07-15 12:09:59 -04:00
dashboard_test.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
debug.go add spans to multiple services 2019-03-07 12:12:31 -07:00
document_service.go feat(http): add auth debug log 2019-07-15 12:09:59 -04:00
document_test.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04: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 feat(http) add microsecond and nanosecond durations 2018-09-25 15:15:06 -05:00
errors.go fix(http): use the content type header to determine how to parse http errors (#14596) 2019-08-08 14:23:43 -05:00
errors_test.go fix(http): use the content type header to determine how to parse http errors (#14596) 2019-08-08 14:23:43 -05:00
handler.go feat(http): add user-agent metrics to all http endpoints 2019-05-02 11:58:13 -07:00
handler_test.go test(http): add test coverage for handler.go 2019-05-02 14:04:40 -07:00
health.go fix(http): health endpoint now returns pass 2018-12-20 21:53:54 -06:00
health_test.go chore(http): surface unmarshaling failures in tests (#13855) 2019-05-08 12:51:03 -07:00
label_service.go feat(http): add auth debug log 2019-07-15 12:09:59 -04:00
label_test.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
no_assets.go feat(http): host swagger docs at /docs and /api/v2/swagger.json 2019-01-22 11:16:27 -06:00
notification_endpoint.go fix(http): fix notfication endpoint secret store 2019-08-28 16:02:17 -04:00
notification_endpoint_test.go feat(notification/endpoint): add headers to http endpoint 2019-09-06 12:38:42 -04:00
notification_rule.go feat(http): add rule query endpoint 2019-09-05 10:22:07 -04:00
notification_rule_test.go Add Any to rule levels (#14866) 2019-08-29 17:46:00 -07:00
onboarding.go feat(http): add auth debug log 2019-07-15 12:09:59 -04:00
onboarding_test.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
org_service.go fix(http): The org parameter takes either the ID or Name interchangeably (#14540) 2019-08-06 06:20:26 +02:00
org_test.go feat(kv): random ids without comma, space, backslash for org and bucket 2019-09-08 01:50:05 -05:00
paging.go feat(http): add notification rule handler 2019-07-25 15:15:35 -04:00
paging_test.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
platform_handler.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
query.go fix(http): add org ID to flux handler (#14225) 2019-06-27 14:33:22 -07:00
query_handler.go chore(ci): update go-tools 2019-08-22 10:55:05 -05:00
query_handler_test.go chore(ci): update go-tools 2019-08-22 10:55:05 -05:00
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 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 fix(http): The org parameter takes either the ID or Name interchangeably (#14540) 2019-08-06 06:20:26 +02:00
requests_test.go fix(http): The org parameter takes either the ID or Name interchangeably (#14540) 2019-08-06 06:20:26 +02:00
router.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
router_test.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
scraper_service.go feat(http): add auth debug log 2019-07-15 12:09:59 -04:00
scraper_service_test.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
server.go chore: rename imports from platform to influxdb 2019-01-09 20:51:47 -08:00
session_handler.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04: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 client tracing propagation from doubly recording (#14204) 2019-06-26 00:48:15 -05:00
source_service.go feat(http): add auth debug log 2019-07-15 12:09:59 -04:00
source_service_test.go fix(http): fix source health endpoint 2019-03-13 11:04:16 -04:00
status.go feat(influxdb): add query/write http event recorders 2019-04-17 08:33:31 -04:00
swagger.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
swagger.yml feat(http): Add bucket types (#15045) 2019-09-06 17:52:54 -07:00
swagger_assets.go chore: serve swagger.json in development builds 2019-03-09 12:42:44 -08:00
swagger_noassets.go chore: serve swagger.json in development builds 2019-03-09 12:42:44 -08:00
swagger_test.go chore(dep): update kin-openapi to v0.2.0 2019-05-15 00:23:32 +02:00
task_service.go chore(ci): update go-tools 2019-08-22 10:55:05 -05:00
task_service_test.go feat(task): Remove token's from task structures (#14712) 2019-08-20 08:42:40 -06:00
task_test.go feat(task): add task types (#14567) 2019-08-06 10:27:52 -06:00
telegraf.go feat(http): add notification rule handler 2019-07-25 15:15:35 -04:00
telegraf_test.go fix(http): rename OrgID to orgID 2019-06-06 22:12:32 +08:00
tokens.go Ensure error strings not capitalised ST1005 2018-11-30 10:54:24 +00:00
tokens_test.go chore(bolt,http,mock,query,testing): fix megacheck errors 2018-11-01 12:54:46 -07:00
transport.go refactor(http): move queryd http interface to idpe (#13862) 2019-05-09 10:41:14 -07:00
usage_service.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
user_resource_mapping_service.go feat(http): add auth debug log 2019-07-15 12:09:59 -04:00
user_resource_mapping_test.go chore(http): surface unmarshaling failures in tests (#13855) 2019-05-08 12:51:03 -07:00
user_service.go feat(http): add auth debug log 2019-07-15 12:09:59 -04:00
user_test.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
variable_service.go feat(http): add auth debug log 2019-07-15 12:09:59 -04:00
variable_test.go feat(http): convert errorEncode to use interface 2019-06-26 21:41:01 -04:00
write_handler.go feat(http): block writes to system buckets (#14920) 2019-09-05 11:22:18 -07: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 {
		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