Documentation ¶
Index ¶
- Constants
- Variables
- func BindRoutes(injector *dingo.Injector, m RoutesModule)
- func ContextWithRequest(ctx context.Context, r *Request) context.Context
- func ContextWithSession(ctx context.Context, session *Session) context.Context
- func HandlerCmd(router *Router, area *config.Area) *cobra.Command
- func RoutesCmd(router *Router, area *config.Area) *cobra.Command
- func URLTitle(title string) string
- type Action
- type CanonicalDomainFunc
- type DataAction
- type DataResponse
- type Filter
- type FilterChain
- type GetPartialDataFunc
- type Handler
- type IsExternalURL
- type Match
- type OnFinishEvent
- type OnRequestEvent
- type OnResponseEvent
- type Path
- type RenderResponse
- type Request
- func (r *Request) Form(name string) ([]string, error)
- func (r *Request) Form1(name string) (string, error)
- func (r *Request) FormAll() (map[string][]string, error)
- func (r *Request) Query(name string) ([]string, error)
- func (r *Request) Query1(name string) (string, error)
- func (r *Request) QueryAll() url.Values
- func (r *Request) RemoteAddress() []string
- func (r *Request) Request() *http.Request
- func (r *Request) Session() *Session
- type RequestParams
- type Responder
- func (r *Responder) Data(data interface{}) *DataResponse
- func (r *Responder) Download(data io.Reader, contentType string, fileName string, forceDownload bool) *Response
- func (r *Responder) Forbidden(err error) *ServerErrorResponse
- func (r *Responder) HTTP(status uint, body io.Reader) *Response
- func (r *Responder) Inject(router *Router, logger flamingo.Logger, cfg ...) *Responder
- func (r *Responder) NotFound(err error) *ServerErrorResponse
- func (r *Responder) Render(tpl string, data interface{}) *RenderResponse
- func (r *Responder) RouteRedirect(to string, data map[string]string) *RouteRedirectResponse
- func (r *Responder) ServerError(err error) *ServerErrorResponse
- func (r *Responder) ServerErrorWithCodeAndTemplate(err error, tpl string, status uint) *ServerErrorResponse
- func (r *Responder) TODO() *Response
- func (r *Responder) URLRedirect(url *url.URL) *URLRedirectResponse
- func (r *Responder) Unavailable(err error) *ServerErrorResponse
- type Response
- type Result
- type ReverseRouter
- type RouteRedirectResponse
- type Router
- func (r *Router) Absolute(req *Request, to string, params map[string]string) (*url.URL, error)
- func (r *Router) Base() *url.URL
- func (r *Router) Data(ctx context.Context, handler string, params map[interface{}]interface{}) interface{}
- func (r *Router) Handler() http.Handler
- func (r *Router) Inject(cfg ..., eventRouter flamingo.EventRouter, filterProvider filterProvider, ...)
- func (r *Router) ListenAndServe(addr string) error
- func (r *Router) Relative(to string, params map[string]string) (*url.URL, error)
- func (r *Router) URL(to string, params map[string]string) (*url.URL, error)
- type RouterRegistry
- func (registry *RouterRegistry) Alias(name, to string)
- func (registry *RouterRegistry) GetRoutes() []*Handler
- func (registry *RouterRegistry) HandleAny(name string, action Action)
- func (registry *RouterRegistry) HandleData(name string, action DataAction)
- func (registry *RouterRegistry) HandleDelete(name string, action Action)
- func (registry *RouterRegistry) HandleGet(name string, action Action)
- func (registry *RouterRegistry) HandleHead(name string, action Action)
- func (registry *RouterRegistry) HandleMethod(method, name string, action Action)
- func (registry *RouterRegistry) HandleOptions(name string, action Action)
- func (registry *RouterRegistry) HandlePost(name string, action Action)
- func (registry *RouterRegistry) HandlePut(name string, action Action)
- func (registry *RouterRegistry) Has(method, name string) bool
- func (registry *RouterRegistry) HasAny(name string) bool
- func (registry *RouterRegistry) HasData(name string) bool
- func (registry *RouterRegistry) Reverse(name string, params map[string]string) (string, error)
- func (registry *RouterRegistry) Route(path, handler string) (*Handler, error)
- type RoutesModule
- type ServerErrorResponse
- type Session
- func (s *Session) AddFlash(value interface{}, vars ...string)
- func (s *Session) ClearAll() *Session
- func (s *Session) Delete(key interface{})
- func (s *Session) Flashes(vars ...string) []interface{}
- func (s *Session) ID() (id string)
- func (s *Session) Keys() []interface{}
- func (s *Session) Load(key interface{}) (data interface{}, ok bool)
- func (s *Session) Store(key interface{}, data interface{}) *Session
- func (s *Session) Try(key interface{}) (data interface{})
- type SetPartialDataFunc
- type URLRedirectResponse
Examples ¶
Constants ¶
const ( // FlamingoError is the Controller name for errors FlamingoError = "flamingo.error" // FlamingoNotfound is the Controller name for 404 notfound FlamingoNotfound = "flamingo.notfound" )
Variables ¶
var (
// ControllerKey exposes the current controller/handler key
ControllerKey, _ = tag.NewKey("controller")
RouterError contextKeyType = "error"
)
var ( // ErrFormNotFound is returned for unknown form values ErrFormNotFound = errors.New("form value not found") // ErrQueryNotFound is returned for unknown URL query parameters ErrQueryNotFound = errors.New("query value not found") )
Functions ¶
func BindRoutes ¶
func BindRoutes(injector *dingo.Injector, m RoutesModule)
BindRoutes is a convenience helper to multi-bind router modules
func ContextWithRequest ¶
ContextWithRequest stores the request in a new context
func ContextWithSession ¶
ContextWithSession returns a new Context with an attached session
func HandlerCmd ¶
HandlerCmd for debugging the router/handler configuration
Types ¶
type Action ¶
Action defines an explicit http action
func WrapDataAction ¶
func WrapDataAction(da DataAction) Action
WrapDataAction allows to register a data action for a HTTP method
func WrapHTTPHandler ¶
WrapHTTPHandler wraps an http.Handler to be used in the flamingo http package
type CanonicalDomainFunc ¶
type CanonicalDomainFunc struct {
// contains filtered or unexported fields
}
CanonicalDomainFunc is exported as a template function
func (*CanonicalDomainFunc) Func ¶
func (c *CanonicalDomainFunc) Func(ctx context.Context) interface{}
Func returns the canonicalDomain func
func (*CanonicalDomainFunc) Inject ¶
func (c *CanonicalDomainFunc) Inject(router ReverseRouter) *CanonicalDomainFunc
Inject dependencies
type DataAction ¶
type DataAction func(ctx context.Context, req *Request, callParams RequestParams) interface{}
DataAction is a method called which does not return the web response itself, but data instead
type DataResponse ¶
type DataResponse struct { Response Data interface{} }
DataResponse returns a response containing data, e.g. as JSON
func (*DataResponse) Apply ¶
func (r *DataResponse) Apply(c context.Context, w http.ResponseWriter) error
Apply response todo: support more than json
func (*DataResponse) SetNoCache ¶
func (r *DataResponse) SetNoCache() *DataResponse
SetNoCache helper
func (*DataResponse) Status ¶
func (r *DataResponse) Status(status uint) *DataResponse
Status changes response status code
type Filter ¶
type Filter interface {
Filter(ctx context.Context, req *Request, w http.ResponseWriter, fc *FilterChain) Result
}
Filter is an interface which can filter requests
type FilterChain ¶
type FilterChain struct {
// contains filtered or unexported fields
}
FilterChain defines the chain which contains all filters which will be worked off
func NewFilterChain ¶
func NewFilterChain(final lastFilter, filters ...Filter) *FilterChain
NewFilterChain constructs and sets the final filter and optional filters
func (*FilterChain) AddPostApply ¶
func (fc *FilterChain) AddPostApply(callback func(err error, result Result))
AddPostApply adds a callback to be called after the response has been applied to the responsewriter
func (*FilterChain) Next ¶
func (fc *FilterChain) Next(ctx context.Context, req *Request, w http.ResponseWriter) Result
Next calls the next filter and deletes it of the chain
type GetPartialDataFunc ¶
type GetPartialDataFunc struct{}
GetPartialDataFunc allows to get partial data
func (*GetPartialDataFunc) Func ¶
func (*GetPartialDataFunc) Func(c context.Context) interface{}
Func getter to bind the context
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler defines a concrete Controller
func (*Handler) GetHandlerName ¶
GetHandlerName getter
type IsExternalURL ¶
type IsExternalURL struct {
// contains filtered or unexported fields
}
IsExternalURL is exported as a template function
func (*IsExternalURL) Func ¶
func (c *IsExternalURL) Func(ctx context.Context) interface{}
Func returns a boolean if a given URL is external
func (*IsExternalURL) Inject ¶
func (c *IsExternalURL) Inject(router ReverseRouter) *IsExternalURL
Inject dependencies
type OnFinishEvent ¶
type OnFinishEvent struct { OnRequestEvent Error error }
OnFinishEvent is the event object associated to OnFinish
type OnRequestEvent ¶
type OnRequestEvent struct { Request *Request ResponseWriter http.ResponseWriter }
OnRequestEvent contains the bare request
type OnResponseEvent ¶
type OnResponseEvent struct { OnRequestEvent Result Result }
OnResponseEvent is the event associated to OnResponse
type Path ¶
type Path struct {
// contains filtered or unexported fields
}
Path is a matchable routing path
type RenderResponse ¶
type RenderResponse struct { DataResponse Template string // contains filtered or unexported fields }
RenderResponse renders data
func (*RenderResponse) Apply ¶
func (r *RenderResponse) Apply(c context.Context, w http.ResponseWriter) error
Apply response
func (*RenderResponse) SetNoCache ¶
func (r *RenderResponse) SetNoCache() *RenderResponse
SetNoCache helper
type Request ¶
type Request struct { Params RequestParams Values sync.Map // contains filtered or unexported fields }
Request object stores the actual HTTP Request, Session, Params and attached Values
func CreateRequest ¶
CreateRequest creates a new request, with optional http.Request and Session. If any variable is nil it is ignored, otherwise it is copied into the new Request.
func RequestFromContext ¶
RequestFromContext retrieves the request from the context, if available
func (*Request) RemoteAddress ¶
RemoteAddress get the requests real remote address
type RequestParams ¶
RequestParams store string->string values for request data
type Responder ¶
type Responder struct {
// contains filtered or unexported fields
}
Responder generates responses
func (*Responder) Data ¶
func (r *Responder) Data(data interface{}) *DataResponse
Data returns a data response which can be serialized
func (*Responder) Download ¶
func (r *Responder) Download(data io.Reader, contentType string, fileName string, forceDownload bool) *Response
Download returns a download response to handle file downloads
func (*Responder) Forbidden ¶
func (r *Responder) Forbidden(err error) *ServerErrorResponse
Forbidden creates a 403 error response
func (*Responder) Inject ¶
func (r *Responder) Inject(router *Router, logger flamingo.Logger, cfg *struct { Engine flamingo.TemplateEngine `inject:",optional"` Debug bool `inject:"config:debug.mode"` TemplateForbidden string `inject:"config:flamingo.template.err403"` TemplateNotFound string `inject:"config:flamingo.template.err404"` TemplateUnavailable string `inject:"config:flamingo.template.err503"` TemplateErrorWithCode string `inject:"config:flamingo.template.errWithCode"` }) *Responder
Inject Responder dependencies
func (*Responder) NotFound ¶
func (r *Responder) NotFound(err error) *ServerErrorResponse
NotFound creates a 404 error response
func (*Responder) Render ¶
func (r *Responder) Render(tpl string, data interface{}) *RenderResponse
Render creates a render response, with the supplied template and data
func (*Responder) RouteRedirect ¶
func (r *Responder) RouteRedirect(to string, data map[string]string) *RouteRedirectResponse
RouteRedirect generator
func (*Responder) ServerError ¶
func (r *Responder) ServerError(err error) *ServerErrorResponse
ServerError creates a 500 error response
func (*Responder) ServerErrorWithCodeAndTemplate ¶
func (r *Responder) ServerErrorWithCodeAndTemplate(err error, tpl string, status uint) *ServerErrorResponse
ServerErrorWithCodeAndTemplate error response with template and http status code
func (*Responder) URLRedirect ¶
func (r *Responder) URLRedirect(url *url.URL) *URLRedirectResponse
URLRedirect returns a 303 redirect to a given URL
func (*Responder) Unavailable ¶
func (r *Responder) Unavailable(err error) *ServerErrorResponse
Unavailable creates a 503 error response
type Result ¶
type Result interface { // Apply executes the response on the http.ResponseWriter Apply(ctx context.Context, rw http.ResponseWriter) error }
Result defines the generic web response
type ReverseRouter ¶
type ReverseRouter interface { // Relative returns a root-relative URL, starting with `/` // if to starts with "/" it will be used as the target, instead of resolving the URL Relative(to string, params map[string]string) (*url.URL, error) // Absolute returns an absolute URL, with scheme and host. // It takes the request to construct as many information as possible // if to starts with "/" it will be used as the target, instead of resolving the URL Absolute(r *Request, to string, params map[string]string) (*url.URL, error) }
ReverseRouter allows to retrieve urls for controller
type RouteRedirectResponse ¶
type RouteRedirectResponse struct { Response To string Data map[string]string // contains filtered or unexported fields }
RouteRedirectResponse redirects to a certain route
func (*RouteRedirectResponse) Apply ¶
func (r *RouteRedirectResponse) Apply(c context.Context, w http.ResponseWriter) error
Apply response
func (*RouteRedirectResponse) Permanent ¶
func (r *RouteRedirectResponse) Permanent() *RouteRedirectResponse
Permanent marks a redirect as being permanent (http 301)
func (*RouteRedirectResponse) SetNoCache ¶
func (r *RouteRedirectResponse) SetNoCache() *RouteRedirectResponse
SetNoCache helper
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
func (*Router) Absolute ¶
Absolute returns an absolute URL, with scheme and host. It takes the request to construct as many information as possible
func (*Router) Data ¶
func (r *Router) Data(ctx context.Context, handler string, params map[interface{}]interface{}) interface{}
Data calls a flamingo data controller
func (*Router) Inject ¶
func (r *Router) Inject( cfg *struct { // base url configuration Scheme string `inject:"config:flamingo.router.scheme,optional"` Host string `inject:"config:flamingo.router.host,optional"` Path string `inject:"config:flamingo.router.path,optional"` }, eventRouter flamingo.EventRouter, filterProvider filterProvider, routesProvider routesProvider, logger flamingo.Logger, configArea *config.Area, sessionStore sessions.Store, )
func (*Router) ListenAndServe ¶
type RouterRegistry ¶
type RouterRegistry struct {
// contains filtered or unexported fields
}
RouterRegistry holds a list of all routes and handlers to be registered in modules.
We have: routes: key-params -> path, for reverse routes
path: url-pattern -> key+params
Handler: key -> Controller
func (*RouterRegistry) Alias ¶
func (registry *RouterRegistry) Alias(name, to string)
Alias for an existing router definition
func (*RouterRegistry) GetRoutes ¶
func (registry *RouterRegistry) GetRoutes() []*Handler
GetRoutes returns registered Routes
func (*RouterRegistry) HandleAny ¶
func (registry *RouterRegistry) HandleAny(name string, action Action)
HandleAny serves as a fallback to handle HTTP requests which are not taken care of by other handlers
func (*RouterRegistry) HandleData ¶
func (registry *RouterRegistry) HandleData(name string, action DataAction)
HandleData sets the controllers data action
func (*RouterRegistry) HandleDelete ¶
func (registry *RouterRegistry) HandleDelete(name string, action Action)
HandleDelete handles HTTP DELETE requests
func (*RouterRegistry) HandleGet ¶
func (registry *RouterRegistry) HandleGet(name string, action Action)
HandleGet handles a HTTP GET request
func (*RouterRegistry) HandleHead ¶
func (registry *RouterRegistry) HandleHead(name string, action Action)
HandleHead handles HTTP HEAD requests
func (*RouterRegistry) HandleMethod ¶
func (registry *RouterRegistry) HandleMethod(method, name string, action Action)
HandleMethod handles requests for the specified HTTP Method
func (*RouterRegistry) HandleOptions ¶
func (registry *RouterRegistry) HandleOptions(name string, action Action)
HandleOptions handles HTTP OPTIONS requests
func (*RouterRegistry) HandlePost ¶
func (registry *RouterRegistry) HandlePost(name string, action Action)
HandlePost handles HTTP POST requests
func (*RouterRegistry) HandlePut ¶
func (registry *RouterRegistry) HandlePut(name string, action Action)
HandlePut handles HTTP PUT requests
func (*RouterRegistry) Has ¶
func (registry *RouterRegistry) Has(method, name string) bool
Has checks if a method is set for a given handler name
func (*RouterRegistry) HasAny ¶
func (registry *RouterRegistry) HasAny(name string) bool
HasAny checks if an any handler is set for a given name
func (*RouterRegistry) HasData ¶
func (registry *RouterRegistry) HasData(name string) bool
HasData checks if a data handler is set for a given name
type RoutesModule ¶
type RoutesModule interface {
Routes(registry *RouterRegistry)
}
RoutesModule defines a router RoutesModule, which is able to register routes
type ServerErrorResponse ¶
type ServerErrorResponse struct { RenderResponse Error error }
ServerErrorResponse returns a server error, by default http 500
func (*ServerErrorResponse) Apply ¶
func (r *ServerErrorResponse) Apply(c context.Context, w http.ResponseWriter) error
Apply response
func (*ServerErrorResponse) SetNoCache ¶
func (r *ServerErrorResponse) SetNoCache() *ServerErrorResponse
SetNoCache helper
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session holds the data connected to the current user session
func EmptySession ¶
func EmptySession() *Session
EmptySession creates an empty session instance for testing etc.
func SessionFromContext ¶
SessionFromContext allows to retrieve the stored session
func (*Session) Delete ¶
func (s *Session) Delete(key interface{})
Delete a given key from the session
func (*Session) Keys ¶
func (s *Session) Keys() []interface{}
Keys returns an unordered list of session keys
type SetPartialDataFunc ¶
type SetPartialDataFunc struct{}
SetPartialDataFunc allows to set partial data
func (*SetPartialDataFunc) Func ¶
func (*SetPartialDataFunc) Func(c context.Context) interface{}
Func getter to bind the context
type URLRedirectResponse ¶
URLRedirectResponse redirects to a certain URL
func (*URLRedirectResponse) Apply ¶
func (r *URLRedirectResponse) Apply(c context.Context, w http.ResponseWriter) error
Apply response
func (*URLRedirectResponse) Permanent ¶
func (r *URLRedirectResponse) Permanent() *URLRedirectResponse
Permanent marks a redirect as being permanent (http 301)
func (*URLRedirectResponse) SetNoCache ¶
func (r *URLRedirectResponse) SetNoCache() *URLRedirectResponse
SetNoCache helper