-
Notifications
You must be signed in to change notification settings - Fork 175
/
server.go
67 lines (57 loc) · 1.74 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
package rest
import (
"net/http"
"time"
"github.com/rs/cors"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rest/routes"
"github.com/onflow/flow-go/engine/access/state_stream"
"github.com/onflow/flow-go/engine/access/state_stream/backend"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
)
const (
// DefaultReadTimeout is the default read timeout for the HTTP server
DefaultReadTimeout = time.Second * 15
// DefaultWriteTimeout is the default write timeout for the HTTP server
DefaultWriteTimeout = time.Second * 30
// DefaultIdleTimeout is the default idle timeout for the HTTP server
DefaultIdleTimeout = time.Second * 60
)
type Config struct {
ListenAddress string
WriteTimeout time.Duration
ReadTimeout time.Duration
IdleTimeout time.Duration
}
// NewServer returns an HTTP server initialized with the REST API handler
func NewServer(serverAPI access.API,
config Config,
logger zerolog.Logger,
chain flow.Chain,
restCollector module.RestMetrics,
stateStreamApi state_stream.API,
stateStreamConfig backend.Config,
) (*http.Server, error) {
builder := routes.NewRouterBuilder(logger, restCollector).AddRestRoutes(serverAPI, chain)
if stateStreamApi != nil {
builder.AddWsRoutes(stateStreamApi, chain, stateStreamConfig)
}
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedHeaders: []string{"*"},
AllowedMethods: []string{
http.MethodGet,
http.MethodPost,
http.MethodOptions,
http.MethodHead},
})
return &http.Server{
Handler: c.Handler(builder.Build()),
Addr: config.ListenAddress,
WriteTimeout: config.WriteTimeout,
ReadTimeout: config.ReadTimeout,
IdleTimeout: config.IdleTimeout,
}, nil
}