handlers

package
v0.0.0-...-9b598c7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ValidUuidRegexp               = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
	ValidClientIdUuidRegexp       = regexp.MustCompile(`^srvc-acct-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
	ValidServiceAccountNameRegexp = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`)
	ValidServiceAccountDescRegexp = regexp.MustCompile(`^[a-zA-Z0-9.,\-\s]*$`)
	ValidAlphaNumeric             = regexp.MustCompile(`^[a-zA-Z0-9]*$`)
	// taken from here: https://regex101.com/r/SEg6KL/1 - will likely be removed if we can use our permissions to get cluster dns from cluster id
	ValidDnsName           = regexp.MustCompile(`^(?:[_a-z0-9](?:[_a-z0-9-]{0,61}[a-z0-9])?\.)+(?:[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?)?$`)
	MinRequiredFieldLength = 1

	MaxServiceAccountNameLength = 50
	MaxServiceAccountDescLength = 255
	MaxServiceAccountId         = 36
	MaxServiceAccountClientId   = 47
)
View Source
var MetricsLabels = []string{
	metricsMethodLabel,
	metricsPathLabel,
	metricsCodeLabel,
}

MetricsLabels - Array of labels added to metrics:

View Source
var MetricsNames = []string{
	requestCount,
	requestDuration,
}

MetricsNames - Array of Names of the metrics:

View Source
var PathVarSub = "-"

PathVarSub replaces path variables to a same character

Functions

func ConvertToPrivateError

func ConvertToPrivateError(e compat.Error) compat.PrivateError

func DetermineListRange

func DetermineListRange(obj interface{}, page int, size int) (list []interface{}, total int)

Prepare a 'list' of non-db-backed resources

func Handle

func Handle(w http.ResponseWriter, r *http.Request, cfg *HandlerConfig, httpStatus int)

func HandleDelete

func HandleDelete(w http.ResponseWriter, r *http.Request, cfg *HandlerConfig, httpStatus int)

func HandleGet

func HandleGet(w http.ResponseWriter, r *http.Request, cfg *HandlerConfig)

func HandleList

func HandleList(w http.ResponseWriter, r *http.Request, cfg *HandlerConfig)

func MakeReferenceId

func MakeReferenceId(id interface{}) (string, bool)

func MetricsMiddleware

func MetricsMiddleware(handler http.Handler) http.Handler

MetricsMiddleware creates a new handler that collects metrics for the requests processed by the given handler.

func NewOpenAPIHandler

func NewOpenAPIHandler(openAPIDefinitions []byte) *openAPIHandler

func NewPrometheusMetricsHandler

func NewPrometheusMetricsHandler() *prometheusMetricsHandler

NewPrometheusMetricsHandler adds custom metrics and proxy to prometheus handler

func PresentError

func PresentError(err *errors.ServiceError, url string) compat.Error

func PresentReferenceWith

func PresentReferenceWith(id, obj interface{}, ObjectKind func(i interface{}) string, ObjectPath func(id string, obj interface{}) string) compat.ObjectReference

func ResetMetricCollectors

func ResetMetricCollectors()

ResetMetricCollectors resets all prometheus collectors

func ValidateJsonSchema

func ValidateJsonSchema(schemaName string, schemaLoader gojsonschema.JSONLoader, documentName string, documentLoader gojsonschema.JSONLoader) *errors.ServiceError

Types

type ErrorHandler

type ErrorHandler struct{}

func NewErrorsHandler

func NewErrorsHandler() *ErrorHandler

func (ErrorHandler) Create

func (h ErrorHandler) Create(w http.ResponseWriter, r *http.Request)

func (ErrorHandler) Delete

func (h ErrorHandler) Delete(w http.ResponseWriter, r *http.Request)

func (ErrorHandler) Get

func (ErrorHandler) List

func (ErrorHandler) Patch

func (h ErrorHandler) Patch(w http.ResponseWriter, r *http.Request)

type ErrorHandlerFunc

type ErrorHandlerFunc func(r *http.Request, w http.ResponseWriter, err *errors.ServiceError)

type EventStream

type EventStream struct {
	ContentType string
	// GetNextEvent should block until there is an event to return.  GetNextEvent should unblock and return io.EOF when if context is canceled.
	GetNextEvent HttpAction
	Close        func()
}

type HandlerConfig

type HandlerConfig struct {
	MarshalInto  interface{}
	Validate     []Validate
	Action       HttpAction
	ErrorHandler ErrorHandlerFunc
}

HandlerConfig defines the common things each REST controller must do. The corresponding handle() func runs the basic HandlerConfig. This is not meant to be an HTTP framework or anything larger than simple CRUD in handlers.

MarshalInto is a pointer to the object to hold the unmarshaled JSON.
Validate is a list of Validation function that run in order, returning fast on the first error.
Action is the specific logic a handler must take (e.g, find an object, save an object)
ErrorHandler is the way errors are returned to the client

type HttpAction

type HttpAction func() (interface{}, *errors.ServiceError)

type RestHandler

type RestHandler interface {
	List(w http.ResponseWriter, r *http.Request)
	Get(w http.ResponseWriter, r *http.Request)
	Create(w http.ResponseWriter, r *http.Request)
	Patch(w http.ResponseWriter, r *http.Request)
	Delete(w http.ResponseWriter, r *http.Request)
}

type Validate

type Validate func() *errors.ServiceError

func ValidateAsyncEnabled

func ValidateAsyncEnabled(r *http.Request, action string) Validate

ValidateAsyncEnabled returns a validator that returns an error if the async query param is not true

func ValidateDnsName

func ValidateDnsName(value *string, field string) Validate

func ValidateExternalClusterId

func ValidateExternalClusterId(value *string, field string) Validate

func ValidateLength

func ValidateLength(value *string, field string, min int, maxVal *int) Validate

func ValidateMaxLength

func ValidateMaxLength(value *string, field string, maxVal *int) Validate

func ValidateMinLength

func ValidateMinLength(value *string, field string, min int) Validate

func ValidateNotEmptyClusterId

func ValidateNotEmptyClusterId(value *string, field string) Validate

func ValidateQueryParam

func ValidateQueryParam(queryParams url.Values, field string) Validate

func ValidateServiceAccountClientId

func ValidateServiceAccountClientId(value *string, field string, ssoProvider string) Validate

func ValidateServiceAccountDesc

func ValidateServiceAccountDesc(value *string, field string) Validate

func ValidateServiceAccountId

func ValidateServiceAccountId(value *string, field string) Validate

func ValidateServiceAccountName

func ValidateServiceAccountName(value *string, field string) Validate

func Validation

func Validation(field string, value *string, options ...ValidateOption) Validate

type ValidateOption

type ValidateOption func(field string, value *string) *errors.ServiceError

func IsOneOf

func IsOneOf(options ...string) ValidateOption

func Matches

func Matches(regex *regexp.Regexp) ValidateOption

func MaxLen

func MaxLen(max int) ValidateOption

func MinLen

func MinLen(min int) ValidateOption

func WithDefault

func WithDefault(d string) ValidateOption

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL