-
Notifications
You must be signed in to change notification settings - Fork 672
/
server.go
277 lines (244 loc) · 8.1 KB
/
server.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package server
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"sync"
"time"
"github.com/gorilla/handlers"
"github.com/rs/cors"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/logging"
)
const (
baseURL = "/ext"
serverShutdownTimeout = 10 * time.Second
)
var (
errUnknownLockOption = errors.New("invalid lock options")
)
type RouteAdder interface {
AddRoute(handler *common.HTTPHandler, lock *sync.RWMutex, base, endpoint string, loggingWriter io.Writer) error
}
// Server maintains the HTTP router
type Server struct {
// log this server writes to
log logging.Logger
// generates new logs for chains to write to
factory logging.Factory
// Maps endpoints to handlers
router *router
// points the the router handlers
handler http.Handler
// Listens for HTTP traffic on this address
listenAddress string
// http server
srv *http.Server
}
// Initialize creates the API server at the provided host and port
func (s *Server) Initialize(
log logging.Logger,
factory logging.Factory,
host string,
port uint16,
allowedOrigins []string,
wrappers ...Wrapper,
) {
s.log = log
s.factory = factory
s.listenAddress = fmt.Sprintf("%s:%d", host, port)
s.router = newRouter()
s.log.Info("API created with allowed origins: %v", allowedOrigins)
corsWrapper := cors.New(cors.Options{
AllowedOrigins: allowedOrigins,
AllowCredentials: true,
})
s.handler = corsWrapper.Handler(s.router)
for _, wrapper := range wrappers {
s.handler = wrapper.WrapHandler(s.handler)
}
}
// Dispatch starts the API server
func (s *Server) Dispatch() error {
listener, err := net.Listen("tcp", s.listenAddress)
if err != nil {
return err
}
s.log.Info("HTTP API server listening on %q", s.listenAddress)
s.srv = &http.Server{Handler: s.handler}
return s.srv.Serve(listener)
}
// DispatchTLS starts the API server with the provided TLS certificate
func (s *Server) DispatchTLS(certFile, keyFile string) error {
listener, err := net.Listen("tcp", s.listenAddress)
if err != nil {
return err
}
s.log.Info("HTTPS API server listening on %q", s.listenAddress)
return http.ServeTLS(listener, s.handler, certFile, keyFile)
}
// RegisterChain registers the API endpoints associated with this chain. That is,
// add <route, handler> pairs to server so that API calls can be made to the VM.
// This method runs in a goroutine to avoid a deadlock in the event that the caller
// holds the engine's context lock. Namely, this could happen when the P-Chain is
// creating a new chain and holds the P-Chain's lock when this function is held,
// and at the same time the server's lock is held due to an API call and is trying
// to grab the P-Chain's lock.
func (s *Server) RegisterChain(chainName string, ctx *snow.Context, engine common.Engine) {
go s.registerChain(chainName, ctx, engine)
}
func (s *Server) registerChain(chainName string, ctx *snow.Context, engine common.Engine) {
var (
handlers map[string]*common.HTTPHandler
err error
)
ctx.Lock.Lock()
handlers, err = engine.GetVM().CreateHandlers()
ctx.Lock.Unlock()
if err != nil {
s.log.Error("failed to create %s handlers: %s", chainName, err)
return
}
httpLogger, err := s.factory.MakeChain(chainName, "http")
if err != nil {
s.log.Error("failed to create new http logger: %s", err)
return
}
s.log.Verbo("About to add API endpoints for chain with ID %s", ctx.ChainID)
// all subroutes to a chain begin with "bc/<the chain's ID>"
defaultEndpoint := "bc/" + ctx.ChainID.String()
// Register each endpoint
for extension, handler := range handlers {
// Validate that the route being added is valid
// e.g. "/foo" and "" are ok but "\n" is not
_, err := url.ParseRequestURI(extension)
if extension != "" && err != nil {
s.log.Error("could not add route to chain's API handler because route is malformed: %s", err)
continue
}
if err := s.AddChainRoute(handler, ctx, defaultEndpoint, extension, httpLogger); err != nil {
s.log.Error("error adding route: %s", err)
}
}
}
// AddChainRoute registers a route to a chain's handler
func (s *Server) AddChainRoute(handler *common.HTTPHandler, ctx *snow.Context, base, endpoint string, loggingWriter io.Writer) error {
url := fmt.Sprintf("%s/%s", baseURL, base)
s.log.Info("adding route %s%s", url, endpoint)
// Apply logging middleware
h := handlers.CombinedLoggingHandler(loggingWriter, handler.Handler)
// Apply middleware to grab/release chain's lock before/after calling API method
h, err := lockMiddleware(h, handler.LockOptions, &ctx.Lock)
if err != nil {
return err
}
// Apply middleware to reject calls to the handler before the chain finishes bootstrapping
h = rejectMiddleware(h, ctx)
return s.router.AddRouter(url, endpoint, h)
}
// AddRoute registers a route to a handler.
func (s *Server) AddRoute(handler *common.HTTPHandler, lock *sync.RWMutex, base, endpoint string, loggingWriter io.Writer) error {
url := fmt.Sprintf("%s/%s", baseURL, base)
s.log.Info("adding route %s%s", url, endpoint)
// Apply logging middleware
h := handlers.CombinedLoggingHandler(loggingWriter, handler.Handler)
// Apply middleware to grab/release chain's lock before/after calling API method
h, err := lockMiddleware(h, handler.LockOptions, lock)
if err != nil {
return err
}
return s.router.AddRouter(url, endpoint, h)
}
// Wraps a handler by grabbing and releasing a lock before calling the handler.
func lockMiddleware(handler http.Handler, lockOption common.LockOption, lock *sync.RWMutex) (http.Handler, error) {
switch lockOption {
case common.WriteLock:
return middlewareHandler{
before: lock.Lock,
after: lock.Unlock,
handler: handler,
}, nil
case common.ReadLock:
return middlewareHandler{
before: lock.RLock,
after: lock.RUnlock,
handler: handler,
}, nil
case common.NoLock:
return handler, nil
default:
return nil, errUnknownLockOption
}
}
// Reject middleware wraps a handler. If the chain that the context describes is
// not done bootstrapping, writes back an error.
func rejectMiddleware(handler http.Handler, ctx *snow.Context) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // If chain isn't done bootstrapping, ignore API calls
if !ctx.IsBootstrapped() {
w.WriteHeader(http.StatusServiceUnavailable)
// Doesn't matter if there's an error while writing. They'll get the StatusServiceUnavailable code.
_, _ = w.Write([]byte("API call rejected because chain is not done bootstrapping"))
} else {
handler.ServeHTTP(w, r)
}
})
}
// AddAliases registers aliases to the server
func (s *Server) AddAliases(endpoint string, aliases ...string) error {
url := fmt.Sprintf("%s/%s", baseURL, endpoint)
endpoints := make([]string, len(aliases))
for i, alias := range aliases {
endpoints[i] = fmt.Sprintf("%s/%s", baseURL, alias)
}
return s.router.AddAlias(url, endpoints...)
}
// AddAliasesWithReadLock registers aliases to the server assuming the http read
// lock is currently held.
func (s *Server) AddAliasesWithReadLock(endpoint string, aliases ...string) error {
// This is safe, as the read lock doesn't actually need to be held once the
// http handler is called. However, it is unlocked later, so this function
// must end with the lock held.
s.router.lock.RUnlock()
defer s.router.lock.RLock()
return s.AddAliases(endpoint, aliases...)
}
// Call ...
func (s *Server) Call(
writer http.ResponseWriter,
method,
base,
endpoint string,
body io.Reader,
headers map[string]string,
) error {
url := fmt.Sprintf("%s/vm/%s", baseURL, base)
handler, err := s.router.GetHandler(url, endpoint)
if err != nil {
return err
}
req, err := http.NewRequest("POST", "*", body)
if err != nil {
return err
}
for key, value := range headers {
req.Header.Set(key, value)
}
handler.ServeHTTP(writer, req)
return nil
}
// Shutdown this server
func (s *Server) Shutdown() error {
if s.srv == nil {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), serverShutdownTimeout)
defer cancel()
return s.srv.Shutdown(ctx)
}