/
proxy.go
90 lines (78 loc) · 2.23 KB
/
proxy.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package proxy
import (
"github.com/kyokan/chaind/pkg"
"github.com/kyokan/chaind/pkg/log"
"github.com/kyokan/chaind/pkg/config"
"net/http"
"fmt"
"context"
"time"
"github.com/kyokan/chaind/internal/audit"
"github.com/satori/go.uuid"
"github.com/kyokan/chaind/internal/cache"
"github.com/kyokan/chaind/internal/backend"
)
var logger = log.NewLog("proxy")
type Proxy struct {
sw backend.Switcher
config *config.Config
ethHandler *EthHandler
quitChan chan bool
errChan chan error
}
func NewProxy(sw backend.Switcher, auditor audit.Auditor, store *cache.ETHStore, fHelper *cache.BlockHeightWatcher, config *config.Config) *Proxy {
return &Proxy{
sw: sw,
config: config,
ethHandler: NewEthHandler(store, auditor, fHelper, config.ETHConfig.APIs),
quitChan: make(chan bool),
errChan: make(chan error),
}
}
func (p *Proxy) Start() error {
if p.config.UseTLS {
panic("TLS not implemented yet")
}
mux := http.NewServeMux()
mux.HandleFunc(fmt.Sprintf("/%s", p.config.ETHConfig.Path), p.handleETHRequest)
s := new(http.Server)
s.Addr = fmt.Sprintf(":%d", p.config.RPCPort)
s.Handler = mux
go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error("proxy server error", "port", p.config.RPCPort, "err", err)
}
}()
go func() {
<-p.quitChan
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
if err := s.Shutdown(ctx); err != nil {
p.errChan <- err
}
p.errChan <- nil
}()
logger.Info("started")
return nil
}
func (p *Proxy) Stop() error {
p.quitChan <- true
return <-p.errChan
}
func (p *Proxy) handleETHRequest(res http.ResponseWriter, req *http.Request) {
ctx := context.WithValue(req.Context(), log.RequestIDKey, uuid.NewV4().String())
req = req.WithContext(ctx)
cLog := log.WithContext(logger, req.Context())
if req.Method != "POST" {
cLog.Info("rejected non-POST request to eth endpoint")
res.WriteHeader(http.StatusMethodNotAllowed)
return
}
start := time.Now()
back, err := p.sw.BackendFor(pkg.EthBackend)
if err != nil {
res.WriteHeader(http.StatusServiceUnavailable)
return
}
p.ethHandler.Handle(res, req, back)
cLog.Info("finished handling Ethereum JSON-RPC request", "elapsed", time.Since(start))
}