Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion 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,9 @@ 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

func Cmd(getServiceKey serviceKeyFunc, httpMiddlewares ...HttpMiddleware) *cobra.Command {
Copy link
Copy Markdown
Contributor

@jribbink jribbink Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would prefer that we switch this to a config struct, i.e.

type Config struct {
    GetServiceKey serviceKeyFunc
    HTTPMiddlewares []HttpMiddleware 
}

It is a breaking change, but it shouldn't be a big deal an it keeps things more extensible for the future.

cmd := &cobra.Command{
Use: "start",
Short: "Starts the Flow emulator server",
Expand Down Expand Up @@ -216,6 +219,10 @@ func Cmd(getServiceKey serviceKeyFunc) *cobra.Command {

emu := server.NewEmulatorServer(logger, serverConf)
if emu != nil {
// We use a variadic parameter to make rest middleware optional and avoid a breaking change
if len(httpMiddlewares) > 0 {
emu.UseRestMiddleware(httpMiddlewares[0])
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(httpMiddlewares) > 0 {
emu.UseRestMiddleware(httpMiddlewares[0])
}
for _, middleware := range httpMiddlewares {
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