influxdb/http
Michael Desa 45233d939a feat(platform): add uniform query endpoint for sources
Using query request struct to query resources

Signed-off-by: Lorenzo Fontana <lo@linux.com>

Use query.ProxyRequest instead query.Request

Signed-off-by: Lorenzo Fontana <lo@linux.com>

Proxy request from idpd

Signed-off-by: Lorenzo Fontana <lo@linux.com>

Comments about the desired results

Signed-off-by: Lorenzo Fontana <lo@linux.com>

V1 endpoints working with flux

Signed-off-by: Lorenzo Fontana <lo@linux.com>

Influxql working for v1

Signed-off-by: Lorenzo Fontana <lo@linux.com>
Co-authored-by: Michael De Sa <mjdesa@gmail.com>

V2 influxql query endpoint working

Signed-off-by: Lorenzo Fontana <lo@linux.com>
Co-authored-by: Michael De Sa <mjdesa@gmail.com>
Signed-off-by: Lorenzo Fontana <lo@linux.com>

V2 Flux compiler support

Co-authored-by: Michael De Sa <mjdesa@gmail.com>
Signed-off-by: Lorenzo Fontana <lo@linux.com>

Improve comments in bolt sources and give error on self

Signed-off-by: Lorenzo Fontana <lo@linux.com>
Co-authored-by: Michael De Sa <mjdesa@gmail.com>

Review tests failing

Signed-off-by: Lorenzo Fontana <lo@linux.com>
Co-authored-by: Michael De Sa <mjdesa@gmail.com>

Avoid type casts for compiler types

Signed-off-by: Lorenzo Fontana <lo@linux.com>
Co-authored-by: Michael De Sa <mjdesa@gmail.com>

Using nil instead of dbrp mapping service for influxql v1

Signed-off-by: Lorenzo Fontana <lo@linux.com>

Check if compiler types are valid for influxql

Signed-off-by: Lorenzo Fontana <lo@linux.com>

Organization as query param in the flux external handler

Signed-off-by: Lorenzo Fontana <lo@linux.com>

feat(http): update swagger documentation for flux query endpoint

feat(http): document query endpoint design

The code documented does not currently work. It is indended that this
will be implemented in follow up PRs.

feat(platform): move source to platform package

The source Query endpoint implements what's in the query swagger docs

Signed-off-by: Lorenzo Fontana <lo@linux.com>
Co-authored-by: Michael De Sa <mjdesa@gmail.com>

feat(platform): allow for encoding and decoding of csv dialects

feat(platform): specify dialect in flux page

Co-authored-by: Andrew Watkins <andrew.watkinz@gmail.com>
Co-authored-by: Michael Desa <mjdesa@gmail.com>
2018-08-28 15:53:20 -04:00
..
influxdb feat(platform): add uniform query endpoint for sources 2018-08-28 15:53:20 -04: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 revert #442 2018-08-01 14:54:32 -04:00
bucket_service.go revert #442 2018-08-01 14:54:32 -04: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 feat(platform): move chronogaf v2 dashboards to platform 2018-08-24 13:22:58 -04:00
errors_test.go fix(http): increase errorHeaderMaxLength 2018-08-01 16:04:36 -07:00
external_query_handler.go feat(platform): add uniform query endpoint for sources 2018-08-28 15:53:20 -04:00
flux_lang.go review(platform): update PR as suggested in review 2018-08-09 15:37:23 -04:00
handler.go feat: Add zap opentracing.Tracer 2018-08-16 14:32:04 -06:00
org_service.go revert #442 2018-08-01 14:54:32 -04:00
platform_handler.go feat(platform): move chronogaf v2 dashboards to platform 2018-08-24 13:22:58 -04:00
proxy_query_service.go feat: Add zap opentracing.Tracer 2018-08-16 14:32:04 -06:00
query_service.go feat: Add zap opentracing.Tracer 2018-08-16 14:32:04 -06: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 feat(platform): add uniform query endpoint for sources 2018-08-28 15:53:20 -04:00
source_service.go feat(platform): add uniform query endpoint for sources 2018-08-28 15:53:20 -04:00
status.go feat: Add optional http logging to handler 2018-07-30 16:16:37 -06:00
swagger.yml feat(platform): add uniform query endpoint for sources 2018-08-28 15:53:20 -04:00
task_service.go feat(task): create validation layer for TaskService (#591) 2018-08-20 15:15:04 -06:00
token_parse_test.go fix(http): fix auth header 2018-06-04 15:26:50 -07:00
token_parser.go fix(http): fix auth header 2018-06-04 15:26:50 -07:00
usage_service.go fix(errors): Update Fluxd errors 2018-06-28 16:56:35 -06:00
user_service.go revert #442 2018-08-01 14:54:32 -04: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

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