router

package
v0.0.0-...-a114734 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2017 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ContentTypeJSON represents the content type of a JSON request
	ContentTypeJSON = "application/json"
	// ContentTypeMultipartForm represents the content type of a multipart request
	ContentTypeMultipartForm = "multipart/form-data"
)

Variables

This section is empty.

Functions

func AdminAccess

func AdminAccess(req *Request) bool

AdminAccess is a auth middleware that filters out non admin users

func Handler

func Handler(e *Endpoint) http.Handler

Handler makes it possible to use a RouteHandler where a http.Handler is required

func LoggedUserAccess

func LoggedUserAccess(req *Request) bool

LoggedUserAccess is a auth middleware that filters out anonymous users

Types

type Endpoint

type Endpoint struct {
	Verb string

	// Path is the path for the current component
	Path string

	// Auth is used to add a auth middleware
	Auth RouteAuth

	// Handler is the handler to call
	Handler RouteHandler

	// Params represents a list of params the endpoint needs
	Params interface{}
}

Endpoint represents an HTTP endpoint

type Endpoints

type Endpoints []*Endpoint

Endpoints represents a list of endpoint

func (Endpoints) Activate

func (endpoints Endpoints) Activate(router *mux.Router)

Activate adds the endpoints to the router

type ParamOptions

type ParamOptions struct {
	// Ignore means the field should not been parsed
	// json:"-"
	Ignore bool

	// Name contains the name of the field in the payload
	// json:"my_field"
	Name string

	// Required means the request should fail with a Bad Request if the field is missing.
	// params:"required"
	Required bool

	// Trim means the field needs to be trimmed before being retrieved and checked
	// params:"trim"
	Trim bool

	// ValidateUUID means the field should contain a valid UUIDv4
	// params:"uuid"
	ValidateUUID bool

	// ValidateOptionalBool means the field should either be empty or contain a bool
	// params:"bool"
	ValidateOptionalBool bool
}

ParamOptions represent all the options for a field

func NewParamOptions

func NewParamOptions(tags *reflect.StructTag) *ParamOptions

NewParamOptions returns a ParamOptions from a StructTag

func (*ParamOptions) ApplyTransformations

func (opts *ParamOptions) ApplyTransformations(value string) string

ApplyTransformations applies all the wanted transformations to the given value

func (*ParamOptions) Validate

func (opts *ParamOptions) Validate(value string) error

Validate checks the given value passes the options set

type Request

type Request struct {
	ID       string
	Response http.ResponseWriter
	Request  *http.Request
	Params   interface{}
	User     *auth.User
	// contains filtered or unexported fields
}

Request represent a client request

func (*Request) ContentType

func (req *Request) ContentType() string

ContentType returns the content type of the current request

func (*Request) Created

func (req *Request) Created(obj interface{})

Created sends a http.StatusCreated response with a JSON object attached It should be used for successful POST requests

func (*Request) Endpoint

func (req *Request) Endpoint() string

Endpoint returns the verb and the URI of the request

func (*Request) Error

func (req *Request) Error(e error)

Error sends an error to the client If the error is an instance of HTTPError, the returned code will match HTTPError.Code(). It returns a 500 if no code has been set.

func (*Request) JSONBody

func (req *Request) JSONBody() (url.Values, error)

JSONBody parses and returns the body of the request

func (*Request) MuxVariables

func (req *Request) MuxVariables() url.Values

MuxVariables returns the URL variables associated to the request

func (*Request) NoContent

func (req *Request) NoContent()

NoContent sends a http.StatusNoContent response It should be used for successful DELETE requests

func (*Request) Ok

func (req *Request) Ok(obj interface{})

Ok sends a http.StatusOK response with a JSON object attached It should be used for successful GET, PATCH, and PUR requests

func (*Request) ParamsBySource

func (req *Request) ParamsBySource() (map[string]url.Values, error)

ParamsBySource returns a map of params ordered by their source (url, query, form, ...)

func (*Request) ParseParams

func (r *Request) ParseParams() error

ParseParams will parse the params from the given request, and store them into the endpoint

func (*Request) RenderJSON

func (req *Request) RenderJSON(code int, obj interface{})

RenderJSON attaches a json object to the response

func (*Request) String

func (req *Request) String() string

String return a printable version of the object

type RouteAuth

type RouteAuth func(*Request) bool

RouteAuth represents a middleware used to allow/block the access to an endpoint

type RouteHandler

type RouteHandler func(*Request) error

RouteHandler is the function signature we nee

Jump to

Keyboard shortcuts

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