-
Notifications
You must be signed in to change notification settings - Fork 10
Create a new Filter
srfrog edited this page Aug 9, 2014
·
3 revisions
A filter is an object that implements the relax.Filter
interface, which consists of a Run() function that returns a relax.HandlerFunc
closure.
Put simply:
// your filter object with as many options that your filter needs...
type MyFilter struct {
SomeOption string
}
func (f *MyFilter) Run(next relax.HandlerFunc) relax.HandlerFunc {
// initialize your filter
if f.SomeOption == "" {
f.SomeOption = "some value"
}
// delegate
return func(ctx *relax.Context) {
// do stuff before next filter...
if someError() {
// error found! stop the request.
ctx.Error(400, "Aborted!")
return
}
// continue to next filter
next(ctx) // continue to next filter...
// do stuff after previous filters...
}
}
Even simpler:
type LogLevel int
func (level LogLevel) Run(next relax.HandlerFunc) relax.HandlerFunc {
return func(ctx *relax.Context) {
if level > LOG_ERROR {
fmt.Printf("ERROR!")
return
}
next(ctx)
}
}