influxdb/http
Nathaniel Cook 61e36cbee6 chore(Makefile): add target to check generated files are accurate
A standard Makefile is used now in all subdirs that run go generate.
Make will only generate the file if its source files changed.
The checkgenerate target runs clean to ensure all targets a generated
fresh.
2018-12-18 12:54:17 -07:00
..
influxdb Address PR feedback 2018-11-30 10:54:24 +00:00
Makefile chore(Makefile): add target to check generated files are accurate 2018-12-18 12:54:17 -07:00
README.md Convert everything to /api/v2 2018-09-28 14:23:21 +02:00
api_handler.go feat(http): add /query/analyze endpoint to analyze queries 2018-12-18 11:23:59 -05:00
assets.go chore(ui): update misc build files related to moving chronograf/ui to ui 2018-10-29 14:00:45 -04:00
auth_service.go fix(http): rename auths to authorizations 2018-12-18 00:12:16 +08:00
auth_test.go fix(http): rename auths to authorizations 2018-12-18 00:12:16 +08:00
authentication_middleware.go fix(http): convert auth errors 2018-11-16 19:33:20 -05: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 add patch label endpoints 2018-12-18 01:45:49 -08:00
bucket_test.go make prev/next links omitempty 2018-12-13 00:16:24 +08:00
chronograf_handler.go add custom-404 handler 2018-12-16 22:55:39 +08:00
client.go feat(http): add MacroService client 2018-09-20 09:32:04 -07:00
cur_swagger.yml Merge pull request #2007 from influxdata/fix/authorizations-response 2018-12-18 10:06:28 -08:00
dashboard_service.go add patch label endpoints 2018-12-18 01:45:49 -08:00
dashboard_test.go fix(http): convert dashboard errors 2018-12-13 14:36:46 -05:00
duration.go chore(http/duration): mention need for longer duration literals 2018-09-25 18:11:20 -05:00
duration_test.go feat(http) add microsecond and nanosecond durations 2018-09-25 15:15:06 -05:00
errors.go add custom-404 handler 2018-12-16 22:55:39 +08:00
errors_test.go fix(http): increase errorHeaderMaxLength 2018-08-01 16:04:36 -07:00
handler.go Add the ability to have readiness probe seperate from liveness probes (#1581) 2018-11-27 15:29:59 -07:00
health.go fix(http): links updated to match swagger doc 2018-10-06 10:11:20 -05:00
label_service.go use new errors library in label http handlers 2018-12-18 09:14:59 -08:00
macro_service.go add custom-404 handler 2018-12-16 22:55:39 +08:00
macro_test.go add custom-404 handler 2018-12-16 22:55:39 +08:00
onboarding.go add custom-404 handler 2018-12-16 22:55:39 +08:00
onboarding_test.go (feat/testing) add onboarding and basic auth service 2018-11-02 12:20:45 -04:00
org_service.go add patch label endpoints 2018-12-18 01:45:49 -08:00
org_test.go feat(http): add labels endpoints to organizations (#1827) 2018-12-11 10:15:45 -08:00
paging.go flatten paging links 2018-12-12 23:25:07 +08:00
piging_test.go flatten paging links 2018-12-12 23:25:07 +08:00
platform_handler.go fix(http): add {Authorization,Session}Service to AuthenticationHandler 2018-10-02 16:10:41 -04:00
proxy_query_service.go add custom-404 handler 2018-12-16 22:55:39 +08:00
query.go feat(http): add /query/analyze endpoint to analyze queries 2018-12-18 11:23:59 -05:00
query_handler.go feat(http): add /query/analyze endpoint to analyze queries 2018-12-18 11:23:59 -05:00
query_handler_test.go fix(http): update the query handler to use valid flux syntax 2018-12-12 11:11:56 -06:00
query_service.go add custom-404 handler 2018-12-16 22:55:39 +08:00
query_test.go fix(platform): keep authorization struct on proxy query request 2018-11-20 15:20:51 -08:00
ready.go Add uptime to ready endpoint 2018-12-12 11:41:50 +00:00
requests.go feat(http): add flux endpoint /v2/query influx 2018-09-04 16:08:00 -05:00
router.go add custom-404 handler 2018-12-16 22:55:39 +08:00
router_test.go add custom-404 handler 2018-12-16 22:55:39 +08:00
scraper_service.go Merge pull request #1990 from influxdata/convert_scraper_errors 2018-12-17 12:18:08 -05:00
scraper_service_test.go fix(http): convert scraper target error 2018-12-17 09:08:19 -05:00
server.go refactor: move the logger package from influxdb to platform 2018-10-10 15:49:07 -05:00
session_handler.go add custom-404 handler 2018-12-16 22:55:39 +08:00
session_test.go chore(bolt,http,mock,query,testing): fix megacheck errors 2018-11-01 12:54:46 -07:00
source_proxy_service.go chore(fmt): fix and simplify go formatting 2018-10-08 17:23:54 -05:00
source_service.go add custom-404 handler 2018-12-16 22:55:39 +08:00
source_service_test.go Remove use of verbose from control.Config (#1573) 2018-11-26 16:02:41 -07:00
status.go feat: Add optional http logging to handler 2018-07-30 16:16:37 -06:00
swagger.yml Merge pull request #2012 from influxdata/update-labels 2018-12-18 09:48:16 -08:00
swagger_test.go Update telegraf config schema to match cur swagger 2018-12-12 15:11:18 -08:00
task_service.go add patch label endpoints 2018-12-18 01:45:49 -08:00
task_service_test.go fix(test): pass label service to task handler in tests (#1706) 2018-12-04 15:20:46 -08:00
task_test.go Labels (#1668) 2018-12-03 08:07:08 -08:00
telegraf.go add patch label endpoints 2018-12-18 01:45:49 -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
usage_service.go add custom-404 handler 2018-12-16 22:55:39 +08:00
user_resource_mapping_service.go remove unused functions 2018-12-15 00:18:50 +08:00
user_resource_mapping_test.go add tests for members/owners endpoints 2018-12-15 00:18:50 +08:00
user_service.go add custom-404 handler 2018-12-16 22:55:39 +08:00
user_test.go fix(http): convert user errors 2018-12-11 20:11:26 -05:00
view_service.go add http handler for label PATCH 2018-12-18 01:38:49 -08:00
view_test.go feat(http): add labels for views (#1800) 2018-12-11 11:42:13 -08:00
write_handler.go add custom-404 handler 2018-12-16 22:55:39 +08:00
write_handler_test.go test(http): add tests for write client 2018-10-25 19:13:53 -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", "/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