Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/emulator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ func defaultServiceKey(
}

func main() {
if err := start.Cmd(defaultServiceKey).Execute(); err != nil {
config := start.StartConfig{
GetServiceKey: defaultServiceKey,
RestMiddlewares: []start.HttpMiddleware{},
}

if err := start.Cmd(config).Execute(); err != nil {
start.Exit(1, err.Error())
}
}
15 changes: 13 additions & 2 deletions cmd/emulator/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/hex"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
Expand Down Expand Up @@ -91,7 +92,14 @@ type serviceKeyFunc func(
hashAlgo crypto.HashAlgorithm,
) (crypto.PrivateKey, crypto.SignatureAlgorithm, crypto.HashAlgorithm)

func Cmd(getServiceKey serviceKeyFunc) *cobra.Command {
type HttpMiddleware func(http.Handler) http.Handler

type StartConfig struct {
GetServiceKey serviceKeyFunc
RestMiddlewares []HttpMiddleware
}

func Cmd(config StartConfig) *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Starts the Flow emulator server",
Expand Down Expand Up @@ -123,7 +131,7 @@ func Cmd(getServiceKey serviceKeyFunc) *cobra.Command {

servicePublicKey = servicePrivateKey.PublicKey()
} else { // if we don't provide any config values use the serviceKeyFunc to obtain the key
servicePrivateKey, serviceKeySigAlgo, serviceKeyHashAlgo = getServiceKey(
servicePrivateKey, serviceKeySigAlgo, serviceKeyHashAlgo = config.GetServiceKey(
conf.Init,
serviceKeySigAlgo,
serviceKeyHashAlgo,
Expand Down Expand Up @@ -216,6 +224,9 @@ func Cmd(getServiceKey serviceKeyFunc) *cobra.Command {

emu := server.NewEmulatorServer(logger, serverConf)
if emu != nil {
for _, middleware := range config.RestMiddlewares {
emu.UseRestMiddleware(middleware)
}
emu.Start()
} else {
Exit(-1, "")
Expand Down
6 changes: 6 additions & 0 deletions server/access/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func (r *RestServer) Stop() {
_ = r.server.Shutdown(context.Background())
}

func (r *RestServer) UseMiddleware(middleware func(http.Handler) http.Handler) {
if r.server != nil {
r.server.Handler = middleware(r.server.Handler)
}
}

func NewRestServer(logger *zerolog.Logger, blockchain *emulator.Blockchain, adapter *adapters.AccessAdapter, chain flow.Chain, host string, port int, debug bool) (*RestServer, error) {

debugLogger := zerolog.Logger{}
Expand Down
7 changes: 7 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package server

import (
"fmt"
"net/http"
"os"
"sort"
"time"
Expand Down Expand Up @@ -511,3 +512,9 @@ func sanitizeConfig(conf *Config) *Config {

return conf
}

func (s *EmulatorServer) UseRestMiddleware(middleware func(http.Handler) http.Handler) {
if s.rest != nil {
s.rest.UseMiddleware(middleware)
}
}
Loading