Documentation ¶
Overview ¶
The filters package provides information that is contextual to a given request/response By chaining contexts together you can safely provide a controller access to request/response data with a minimal duplication of logic.
This package's chain builder _may panic_ if runtime type dependencies are not satisfied. This is considered an unrecoverable error; and it is caught by the web-server while loading routes.
Continuing to use a context-chain which has panic'd will likely result in nil-pointers or calls to improperly initialized contexts.
Index ¶
- Constants
- func BuildChain() *contextChain
- func BuildDefaultChain() *contextChain
- func FlashChain() web.ChainableContext
- type AuthChainLink
- type AuthContext
- func (ac *AuthContext) AfterAttach(w http.ResponseWriter, r *http.Request) error
- func (ac *AuthContext) ApplyContext(controller web.Controller, response http.ResponseWriter, request *http.Request, ...)
- func (ac *AuthContext) Can(permission string) bool
- func (ac *AuthContext) CloseContext()
- func (ac *AuthContext) CurrentUser() (*models.User, error)
- func (ac *AuthContext) DeleteCurrentSession() error
- func (ac *AuthContext) NewInstance() web.ChainableContext
- func (ac *AuthContext) TestContext(route web.Controller, chain []web.ChainableContext) error
- func (ac *AuthContext) WriteSessionFor(user *models.User) error
- type AuthorizableController
- type DevContext
- func (dc *DevContext) ApplyContext(controller web.Controller, response http.ResponseWriter, request *http.Request, ...)
- func (dc *DevContext) CloseContext()
- func (dc *DevContext) GetParams() *web.Param
- func (dc *DevContext) NewInstance() web.ChainableContext
- func (dc *DevContext) SetParams(params *web.Param)
- func (dc *DevContext) SetResponsePair(w http.ResponseWriter, r *http.Request)
- func (dc *DevContext) TestContext(route web.Controller, chain []web.ChainableContext) error
- type EventChainLink
- type EventContext
- func (ec *EventContext) ApplyContext(controller web.Controller, response http.ResponseWriter, request *http.Request, ...)
- func (ec *EventContext) CloseContext()
- func (ec *EventContext) NewInstance() web.ChainableContext
- func (ec *EventContext) ReadStats(infoHash string) *bridge.TorrentStatMessage
- func (ec *EventContext) SendMessage(msg *bridge.Message)
- func (ec *EventContext) TestContext(route web.Controller, chain []web.ChainableContext) error
- type EventController
- type FlashChainLink
- type FlashContext
- func (fc *FlashContext) AddFlash(message string)
- func (fc *FlashContext) ApplyContext(controller web.Controller, response http.ResponseWriter, request *http.Request, ...)
- func (fc *FlashContext) CloseContext()
- func (fc *FlashContext) GetFlashes() []interface{}
- func (fc *FlashContext) GetViewHelpers() []interface{}
- func (fc *FlashContext) NewInstance() web.ChainableContext
- func (fc *FlashContext) TestContext(route web.Controller, chain []web.ChainableContext) error
- type FlashableController
- type ParameterizedController
- type ParamterChainLink
- type SessionAware
- type SessionChainLink
- type SessionContext
- func (sc *SessionContext) ApplyContext(controller web.Controller, response http.ResponseWriter, request *http.Request, ...)
- func (sc *SessionContext) CloseContext()
- func (sc *SessionContext) GetSession() (*sessions.Session, error)
- func (sc *SessionContext) NewInstance() web.ChainableContext
- func (sc *SessionContext) SaveAll()
- func (sc *SessionContext) SetRequestPair(w http.ResponseWriter, r *http.Request)
- func (sc *SessionContext) SetStore(store sessions.Store)
- func (sc *SessionContext) TestContext(route web.Controller, chain []web.ChainableContext) error
Constants ¶
const ( EVENT_TIMEOUT int = 5 EVENT_CTX_NAME string = "web-event-ctx" )
Variables ¶
This section is empty.
Functions ¶
func BuildChain ¶
func BuildChain() *contextChain
Prepares a route with no contexts. Will simply call a bare metal controller by default.
func BuildDefaultChain ¶
func BuildDefaultChain() *contextChain
Prepares a route with the default contexts: this includes a ParamterChainLink, FlashChainLink, and SessionChainLink. The latter are lazily loaded when requested by a controller. (This implements the chain tested by the default application controller's `testContext` method.)
func FlashChain ¶
func FlashChain() web.ChainableContext
Returns an uninitialized FlashContext which is used by the default context chainer to test type-requirements at runtime as well as route requests to a request-specific instance.
Types ¶
type AuthChainLink ¶
type AuthContext ¶
type AuthContext struct { Required bool // contains filtered or unexported fields }
An implementation of SessionContext that uses it to provide helper methods for authorizing a user.
func AuthChain ¶
func AuthChain(required bool) *AuthContext
Returns an uninitialized AuthContext suitable for use in a context chain
`required: true` will require that a current sesion meet some criteria otherwise this chain will stop the response during the AFTER-ATTACH resolution / phase
func (*AuthContext) AfterAttach ¶
func (ac *AuthContext) AfterAttach(w http.ResponseWriter, r *http.Request) error
Requires authentication based on request/response This requires authentication without involving your controller.
Useful for protecting groups of routes.
The AuthContext will only call this if it is initialized with `AuthContext.required = true`
func (*AuthContext) ApplyContext ¶
func (ac *AuthContext) ApplyContext(controller web.Controller, response http.ResponseWriter, request *http.Request, chain []web.ChainableContext)
Applies the context to an authorizable controller.
func (*AuthContext) Can ¶
func (ac *AuthContext) Can(permission string) bool
Checks if a user can perform a specified action:
func (*AuthContext) CurrentUser ¶
func (ac *AuthContext) CurrentUser() (*models.User, error)
Returns the currently authenticated user
func (*AuthContext) DeleteCurrentSession ¶
func (ac *AuthContext) DeleteCurrentSession() error
func (*AuthContext) NewInstance ¶
func (ac *AuthContext) NewInstance() web.ChainableContext
Returns a clean instance of AuthContext that can be used safely for a single request.
func (*AuthContext) TestContext ¶
func (ac *AuthContext) TestContext(route web.Controller, chain []web.ChainableContext) error
This context requires a chain with a SessionChainLink as well as an AuthorizableController route.
This method can be used to ensure that those dependencies are satisfied at runtime.
func (*AuthContext) WriteSessionFor ¶
func (ac *AuthContext) WriteSessionFor(user *models.User) error
Only doing this b/c AC has access to request/response Some way I can make this private to `login` controller?
type AuthorizableController ¶
type AuthorizableController interface { web.Controller SetAuthContext(*AuthContext) error }
An authorizable controller must be willing to accept an AuthContext.
This AuthContext will be tied to a session for a single request.
type DevContext ¶
type DevContext struct { Params *web.Param Response http.ResponseWriter Request *http.Request // contains filtered or unexported fields }
Test impl. of Context interface.
func ParameterChain ¶
func ParameterChain() *DevContext
Returns an instance of ParameterChain that is not initialized for request handling.
This can be used for checking runtime-type dependencies as well as creating clean instances on a per request basis.
func (*DevContext) ApplyContext ¶
func (dc *DevContext) ApplyContext(controller web.Controller, response http.ResponseWriter, request *http.Request, chain []web.ChainableContext)
Applies the context to a ParamterizedController
func (*DevContext) CloseContext ¶
func (dc *DevContext) CloseContext()
func (*DevContext) GetParams ¶
func (dc *DevContext) GetParams() *web.Param
Can retrieve a map of get/post vars for the current request being processed.
Note that in the case of name conflicts - the POST variables take precedence and replace any conflict GET variables.
func (*DevContext) NewInstance ¶
func (dc *DevContext) NewInstance() web.ChainableContext
Returns an uninitialized paramter context that is suitable for creating per-request instances and checking runtime type dependencies.
func (*DevContext) SetParams ¶
func (dc *DevContext) SetParams(params *web.Param)
Sets the get/post variables for this request.
func (*DevContext) SetResponsePair ¶
func (dc *DevContext) SetResponsePair(w http.ResponseWriter, r *http.Request)
Sets the current HTTP Response & Request pair
func (*DevContext) TestContext ¶
func (dc *DevContext) TestContext(route web.Controller, chain []web.ChainableContext) error
The parameter context requires that a route implements ParamterizedController
type EventChainLink ¶
type EventChainLink interface { web.ChainableContext }
type EventContext ¶
type EventContext struct {
// contains filtered or unexported fields
}
An implementation of SessionContext that uses it to provide helper methods for authorizing a user.
func EventChain ¶
func EventChain(serverBridge *bridge.Bridge) *EventContext
Returns an uninitialized AuthContext suitable for use in a context chain TODO: Synchronized so long as this is the only subscriber writing to the event context's internal structures.
func (*EventContext) ApplyContext ¶
func (ec *EventContext) ApplyContext(controller web.Controller, response http.ResponseWriter, request *http.Request, chain []web.ChainableContext)
Applies the context to an authorizable controller.
func (*EventContext) NewInstance ¶
func (ec *EventContext) NewInstance() web.ChainableContext
Returns the globally routable instance of the event context This context can be safely shared because it does not read or write the request/response
Instead it simply acts a message-broker between controllers and the web server's event bridge.
func (*EventContext) ReadStats ¶
func (ec *EventContext) ReadStats(infoHash string) *bridge.TorrentStatMessage
func (*EventContext) SendMessage ¶
func (ec *EventContext) SendMessage(msg *bridge.Message)
Sends a properly typed message over the bridge.
This may block indefinitely if the send buffer is full. This should be used for callers that are not time-sensitive OR for callers that must take responsibility for delivery of a message.
func (*EventContext) TestContext ¶
func (ec *EventContext) TestContext(route web.Controller, chain []web.ChainableContext) error
Just need a supported controller and a link to the event bridge
type EventController ¶
type EventController interface { web.Controller SetEventContext(*EventContext) error }
An authorizable controller must be willing to accept an AuthContext.
This AuthContext will be tied to a session for a single request.
type FlashChainLink ¶
type FlashChainLink interface { web.ViewableContext AddFlash(string) GetFlashes() []interface{} }
type FlashContext ¶
type FlashContext struct {
// contains filtered or unexported fields
}
Wraps the gorilla muxer's "flash" messages into a `babou/lib/web.Context`
func (*FlashContext) AddFlash ¶
func (fc *FlashContext) AddFlash(message string)
Add a message to the session's flash context. The message will persist until GetFlashes() is called by the controller or view layer.
func (*FlashContext) ApplyContext ¶
func (fc *FlashContext) ApplyContext(controller web.Controller, response http.ResponseWriter, request *http.Request, chain []web.ChainableContext)
Applies this context to an instance of FlashableController
func (*FlashContext) CloseContext ¶
func (fc *FlashContext) CloseContext()
func (*FlashContext) GetFlashes ¶
func (fc *FlashContext) GetFlashes() []interface{}
func (*FlashContext) GetViewHelpers ¶
func (fc *FlashContext) GetViewHelpers() []interface{}
Implements a ViewableContext.
When passed to a rendering context it adds the following helpers to the template layer:
{{#Flash}} This content is shown when a flash message is present {{Message}} The Message variable holds the first flash message as a string. {{/Flash}}
func (*FlashContext) NewInstance ¶
func (fc *FlashContext) NewInstance() web.ChainableContext
Returns a clean instance of FlashContext that can be used to service a request.
func (*FlashContext) TestContext ¶
func (fc *FlashContext) TestContext(route web.Controller, chain []web.ChainableContext) error
FlashContext requires a chain with a SessionChainLink as well as a FlashableController route.
This method can be called to ensure that both those requirements are satisfied for a given route and context chain.
type FlashableController ¶
type FlashableController interface { web.Controller SetFlashContext(*FlashContext) error }
A controller must accept a FlashContext object in order to support the FlashContext chain-link
type ParameterizedController ¶
type ParameterizedController interface {
SetContext(*DevContext) error
}
A controller which accepts GET/POST request variables.
type ParamterChainLink ¶
type ParamterChainLink interface { SetParams(*web.Param) GetParams() *web.Param SetResponsePair(http.ResponseWriter, *http.Request) }
A chain which can store and retrieve GET/POST request variables.
type SessionAware ¶
type SessionAware interface {
SetSessionContext(*SessionContext) error
}
A SessionAware controller must be willing to accept a SessionContext. Use of the session context
type SessionChainLink ¶
type SessionChainLink interface { SetRequestPair(http.ResponseWriter, *http.Request) SetStore(sessions.Store) GetSession() (*sessions.Session, error) SaveAll() }
Describes an element in a context chain which is capable of providing session-state to all elements in the chain.
This interfsce allows a contextChain to determine if a chain requires a SessionChain to be processed. This provides additional type-safety at runtime.
type SessionContext ¶
type SessionContext struct {
// contains filtered or unexported fields
}
A context which provides server-backed session storage to a controller.
func SessionChain ¶
func SessionChain() *SessionContext
func (*SessionContext) ApplyContext ¶
func (sc *SessionContext) ApplyContext(controller web.Controller, response http.ResponseWriter, request *http.Request, chain []web.ChainableContext)
Applies this context to a SessionAware controller. Exposing all session variables to the underlying route.
func (*SessionContext) CloseContext ¶
func (sc *SessionContext) CloseContext()
func (*SessionContext) GetSession ¶
func (sc *SessionContext) GetSession() (*sessions.Session, error)
Retrieves or creates a session key and stores it in the backend.
func (*SessionContext) NewInstance ¶
func (sc *SessionContext) NewInstance() web.ChainableContext
Returns an instance of this context that can safely be used to process a single response-request pairing.
func (*SessionContext) SaveAll ¶
func (sc *SessionContext) SaveAll()
Saves all currently open sessions for this request to disk.
func (*SessionContext) SetRequestPair ¶
func (sc *SessionContext) SetRequestPair(w http.ResponseWriter, r *http.Request)
Gives the authorization a request w/ complete headers; used in retrieving/storing the user's session key.
func (*SessionContext) SetStore ¶
func (sc *SessionContext) SetStore(store sessions.Store)
Sets the session store to the specified store; or fetches one from the default database-backed session store.
TODO: Should be configurable; must be shared among cooperative frontends.
func (*SessionContext) TestContext ¶
func (sc *SessionContext) TestContext(route web.Controller, chain []web.ChainableContext) error
Tests that the context is being applied to a route which is SessionAware.
This method can be used to ensure that the type-dependencies are satisfied at runtime.