-
Notifications
You must be signed in to change notification settings - Fork 10
/
filter.go
50 lines (43 loc) · 1.71 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2014 Codehack http://codehack.com
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package relax
/*
Filter is a function closure that is chained in FILO (First-In Last-Out) order.
Filters pre and post process all requests. At any time, a filter can stop a request by
returning before the next chained filter is called. The final link points to the
resource handler.
Filters are run at different times during a request, and in order: Service, Resource and, Route.
Service filters are run before resource filters, and resource filters before route filters.
This allows some granularity to filters.
Relax comes with filters that provide basic functionality needed by most REST API's.
Some included filters: CORS, method override, security, basic auth and content negotiation.
Adding filters is a matter of creating new objects that implement the Filter interface.
The position of the ``next()`` handler function is important to the effect of the particular
filter execution.
*/
type Filter interface {
// Run executes the current filter in a chain.
// It takes a HandlerFunc function argument, which is executed within the
// closure returned.
Run(HandlerFunc) HandlerFunc
}
/*
LimitedFilter are filters that only can be used with a set of resources.
Where resource is one of: ``Router`` (interface), ``*Resource`` and ``*Service``
The ``RunIn()`` func should return true for the type(s) allowed, false otherwise.
func (f *MyFilter) RunIn(r interface{}) bool {
switch r.(type) {
case relax.Router:
return true
case *relax.Resource:
return true
case *relax.Service:
return false
}
return false
}
*/
type LimitedFilter interface {
RunIn(interface{}) bool
}