-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.go
More file actions
39 lines (33 loc) · 1.24 KB
/
factory.go
File metadata and controls
39 lines (33 loc) · 1.24 KB
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
package gateway
import (
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/open-webtech/go-api-gateway/handlers"
"github.com/open-webtech/go-api-gateway/services"
reverseproxy "github.com/open-webtech/go-reverse-proxy"
)
// HandlerFactory creates HTTP handlers preconfigured using the service context and other configuration options.
type HandlerFactory struct {
Transport http.RoundTripper
RequestHeader http.Header
ModifyResponse func(*http.Response) error
Services services.Context
}
// NewHandlerFactory creates a new instance of HandlerFactory with the given service context.
func NewHandlerFactory(sc services.Context) *HandlerFactory {
return &HandlerFactory{
Services: sc,
}
}
// MakeRouter creates a preconfigured httprouter.Router instance.
func (f *HandlerFactory) MakeRouter() *httprouter.Router {
return handlers.Router(f.Services)
}
// MakeReverseProxy creates a preconfigured reverseproxy.ReverseProxyMux instance for the given remote host.
func (f *HandlerFactory) MakeReverseProxy(remote string) (*reverseproxy.ReverseProxyMux, error) {
rp, err := handlers.ReverseProxy(remote, f.Services)
rp.Transport = f.Transport
rp.RequestHeader = f.RequestHeader
rp.ModifyResponse = f.ModifyResponse
return rp, err
}