github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/server.go (about) 1 package rest 2 3 import ( 4 "net/http" 5 "time" 6 7 "github.com/rs/cors" 8 "github.com/rs/zerolog" 9 10 "github.com/onflow/flow-go/access" 11 "github.com/onflow/flow-go/engine/access/rest/routes" 12 "github.com/onflow/flow-go/engine/access/state_stream" 13 "github.com/onflow/flow-go/engine/access/state_stream/backend" 14 "github.com/onflow/flow-go/model/flow" 15 "github.com/onflow/flow-go/module" 16 ) 17 18 const ( 19 // DefaultReadTimeout is the default read timeout for the HTTP server 20 DefaultReadTimeout = time.Second * 15 21 22 // DefaultWriteTimeout is the default write timeout for the HTTP server 23 DefaultWriteTimeout = time.Second * 30 24 25 // DefaultIdleTimeout is the default idle timeout for the HTTP server 26 DefaultIdleTimeout = time.Second * 60 27 ) 28 29 type Config struct { 30 ListenAddress string 31 WriteTimeout time.Duration 32 ReadTimeout time.Duration 33 IdleTimeout time.Duration 34 } 35 36 // NewServer returns an HTTP server initialized with the REST API handler 37 func NewServer(serverAPI access.API, 38 config Config, 39 logger zerolog.Logger, 40 chain flow.Chain, 41 restCollector module.RestMetrics, 42 stateStreamApi state_stream.API, 43 stateStreamConfig backend.Config, 44 ) (*http.Server, error) { 45 builder := routes.NewRouterBuilder(logger, restCollector).AddRestRoutes(serverAPI, chain) 46 if stateStreamApi != nil { 47 builder.AddWsRoutes(stateStreamApi, chain, stateStreamConfig) 48 } 49 50 c := cors.New(cors.Options{ 51 AllowedOrigins: []string{"*"}, 52 AllowedHeaders: []string{"*"}, 53 AllowedMethods: []string{ 54 http.MethodGet, 55 http.MethodPost, 56 http.MethodOptions, 57 http.MethodHead}, 58 }) 59 60 return &http.Server{ 61 Handler: c.Handler(builder.Build()), 62 Addr: config.ListenAddress, 63 WriteTimeout: config.WriteTimeout, 64 ReadTimeout: config.ReadTimeout, 65 IdleTimeout: config.IdleTimeout, 66 }, nil 67 }