/
package.go
51 lines (42 loc) · 1.03 KB
/
package.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
package proxy
import (
"net/http"
"net/url"
"github.com/ethereum/go-ethereum/rpc"
"github.com/vulcanize/gap-filler/pkg/qlservices"
)
// Proxy accept http and ws requests
type Proxy struct {
wsProxy http.Handler
httpProxy http.Handler
}
type PostgraphileOptions struct {
Default *url.URL
TracingAPI *url.URL
}
type RPCOptions struct {
DefaultClients []*rpc.Client
TracingClients []*rpc.Client
}
type Options struct {
Postgraphile PostgraphileOptions
RPC RPCOptions
}
// New create new router
func New(opts *Options) *Proxy {
return &Proxy{
wsProxy: NewWebsocketReverseProxy(opts.Postgraphile.Default),
httpProxy: NewHTTPReverseProxy(opts).
Register(qlservices.NewEthHeaderCidByBlockNumberService(opts.RPC.DefaultClients)).
Register(qlservices.NewGetGraphCallByTxHashService(opts.RPC.TracingClients)),
}
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var proxy http.Handler
if IsWebSocketRequest(r) {
proxy = p.wsProxy
} else {
proxy = p.httpProxy
}
proxy.ServeHTTP(w, r)
}