-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynaroutes.go
54 lines (46 loc) · 1.59 KB
/
dynaroutes.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
51
52
53
54
// Copyright Piero de Salvia.
// All Rights Reserved
package dynaroutes
import (
"net/http"
"time"
"github.com/pierods/pluginator"
)
// Router is lib's entry point
type Router struct {
preFilters []PreFilter
prefilterCode map[string]string
postFilters []PostFilter
postFilterCode map[string]string
server *http.Server
client http.Client
pluginDir string
pluginator *pluginator.Pluginator
requestTimeout time.Duration
}
// Route is returned by pre filters to set the reverse proxy request parameters
type Route struct {
Scheme string
Host string
URI string
Port int
Method string
Timeout time.Duration
}
type filterBase interface {
Name() string
Order() int
Description() string
}
// A PreFilter is applied to a request before proxying. Many pre filters cam be defined, and they are applied in the order defined by Order(), however as soon as a non-nil route is returned, the router stops applying pre filters.
type PreFilter interface {
filterBase
//Filter returns a Route in case it decides to route the request, nil otherwise.
Filter(request *http.Request) (*Route, error)
}
//PostFilter is applied before a response is sent back to the client. Many post filter can be defined, and they are applied in the order defined by Order(), however as soon as a non-nil []byte is returned, the router stops applying post filters.
type PostFilter interface {
filterBase
//Filter can return nil, or a []byte, in which case that's what will be sent back to the client
Filter(request, proxyRequest *http.Request, proxyResponse *http.Response) ([]byte, error)
}