From 35ab696143df50ca80729a3926e7c423a3005904 Mon Sep 17 00:00:00 2001 From: Brian Yeh Date: Tue, 7 Nov 2023 14:00:30 -0800 Subject: [PATCH 1/6] Added new WithCors option and associated test. --- cardinal/go.mod | 1 + cardinal/option.go | 6 ++++++ cardinal/server/option.go | 6 ++++++ cardinal/server/server.go | 14 ++++++++++---- cardinal/server/server_test.go | 16 +++++++++++----- 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/cardinal/go.mod b/cardinal/go.mod index 7bc25602f..66f25e8ee 100644 --- a/cardinal/go.mod +++ b/cardinal/go.mod @@ -25,6 +25,7 @@ require ( github.com/invopop/jsonschema v0.7.0 github.com/mitchellh/mapstructure v1.5.0 github.com/redis/go-redis/v9 v9.0.2 + github.com/rs/cors v1.10.1 github.com/rs/zerolog v1.30.0 github.com/stretchr/testify v1.8.4 google.golang.org/grpc v1.58.3 diff --git a/cardinal/option.go b/cardinal/option.go index 23e819bfd..9f6882d17 100644 --- a/cardinal/option.go +++ b/cardinal/option.go @@ -82,3 +82,9 @@ func WithPrettyLog() WorldOption { ecsOption: ecs.WithPrettyLog(), } } + +func WithCORS() WorldOption { + return WorldOption{ + serverOption: nil, + } +} diff --git a/cardinal/server/option.go b/cardinal/server/option.go index 2705c448f..dfb01f39d 100644 --- a/cardinal/server/option.go +++ b/cardinal/server/option.go @@ -21,3 +21,9 @@ func WithAdapter(a shard.Adapter) Option { th.adapter = a } } + +func WithCORS() Option { + return func(th *Handler) { + th.withCORS = true + } +} diff --git a/cardinal/server/server.go b/cardinal/server/server.go index 938f14355..a30ba867d 100644 --- a/cardinal/server/server.go +++ b/cardinal/server/server.go @@ -15,6 +15,7 @@ import ( "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/runtime/middleware/untyped" "github.com/mitchellh/mapstructure" + "github.com/rs/cors" "github.com/rs/zerolog/log" "pkg.world.dev/world-engine/cardinal/ecs" "pkg.world.dev/world-engine/cardinal/shard" @@ -27,6 +28,7 @@ type Handler struct { server *http.Server disableSigVerification bool Port string + withCORS bool // plugins adapter shard.WriteAdapter @@ -61,8 +63,9 @@ var swaggerData []byte func newSwaggerHandlerEmbed(w *ecs.World, builder middleware.Builder, opts ...Option) (*Handler, error) { th := &Handler{ - w: w, - Mux: http.NewServeMux(), + w: w, + Mux: http.NewServeMux(), + withCORS: false, } for _, opt := range opts { opt(th) @@ -94,8 +97,11 @@ func newSwaggerHandlerEmbed(w *ecs.World, builder middleware.Builder, opts ...Op } app := middleware.NewContext(specDoc, api, nil) - - th.Mux.Handle("/", app.APIHandler(builder)) + var handler = app.APIHandler(builder) + if th.withCORS { + handler = cors.AllowAll().Handler(handler) + } + th.Mux.Handle("/", handler) th.Initialize() return th, nil diff --git a/cardinal/server/server_test.go b/cardinal/server/server_test.go index 5cab5fdc1..2e5b1d83e 100644 --- a/cardinal/server/server_test.go +++ b/cardinal/server/server_test.go @@ -10,12 +10,13 @@ import ( "net/http" "os" "os/exec" - "pkg.world.dev/world-engine/cardinal/testutils" "reflect" "strconv" "testing" "time" + "pkg.world.dev/world-engine/cardinal/testutils" + "github.com/gorilla/websocket" "pkg.world.dev/world-engine/cardinal/ecs/component" "pkg.world.dev/world-engine/cardinal/shard" @@ -39,7 +40,7 @@ type SendEnergyTx struct { type SendEnergyTxResult struct{} func TestHealthEndpoint(t *testing.T) { - testutils.SetTestTimeout(t, 10*time.Second) + //testutils.SetTestTimeout(t, 10*time.Second) w := ecs.NewTestWorld(t) assert.NilError(t, w.LoadGameState()) testutils.MakeTestTransactionHandler(t, w, server.DisableSignatureVerification()) @@ -153,10 +154,15 @@ func TestCanListTransactionEndpoints(t *testing.T) { betaTx := ecs.NewTransactionType[SendEnergyTx, SendEnergyTxResult]("beta") gammaTx := ecs.NewTransactionType[SendEnergyTx, SendEnergyTxResult]("gamma") assert.NilError(t, w.RegisterTransactions(alphaTx, betaTx, gammaTx)) - txh := testutils.MakeTestTransactionHandler(t, w, server.DisableSignatureVerification()) - - resp, err := http.Post(txh.MakeHTTPURL("query/http/endpoints"), "application/json", nil) + txh := testutils.MakeTestTransactionHandler(t, w, server.DisableSignatureVerification(), server.WithCORS()) + client := &http.Client{} + req, err := http.NewRequest("POST", txh.MakeHTTPURL("query/http/endpoints"), nil) + assert.NilError(t, err) + req.Header.Set("Origin", "http://www.bullshit.com") // test CORS + resp, err := client.Do(req) assert.NilError(t, err) + v := resp.Header.Get("Access-Control-Allow-Origin") + assert.Equal(t, v, "*") assert.Equal(t, resp.StatusCode, 200) var gotEndpoints map[string][]string assert.NilError(t, json.NewDecoder(resp.Body).Decode(&gotEndpoints)) From 7bb60c5b3ad6180521fb9266fa069f291c36e0af Mon Sep 17 00:00:00 2001 From: Brian Yeh Date: Tue, 7 Nov 2023 14:34:17 -0800 Subject: [PATCH 2/6] uncommented thing. --- cardinal/server/server_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cardinal/server/server_test.go b/cardinal/server/server_test.go index 6dc1b3bc5..f52eaf623 100644 --- a/cardinal/server/server_test.go +++ b/cardinal/server/server_test.go @@ -40,7 +40,7 @@ type SendEnergyTx struct { type SendEnergyTxResult struct{} func TestHealthEndpoint(t *testing.T) { - //testutils.SetTestTimeout(t, 10*time.Second) + testutils.SetTestTimeout(t, 10*time.Second) w := ecs.NewTestWorld(t) assert.NilError(t, w.LoadGameState()) testutils.MakeTestTransactionHandler(t, w, server.DisableSignatureVerification()) @@ -205,7 +205,7 @@ func TestCanListTransactionEndpoints(t *testing.T) { assert.NilError(t, w.RegisterTransactions(alphaTx, betaTx, gammaTx)) txh := testutils.MakeTestTransactionHandler(t, w, server.DisableSignatureVerification(), server.WithCORS()) client := &http.Client{} - req, err := http.NewRequest("POST", txh.MakeHTTPURL("query/http/endpoints"), nil) + req, err := http.NewRequest(http.MethodPost, txh.MakeHTTPURL("query/http/endpoints"), nil) assert.NilError(t, err) req.Header.Set("Origin", "http://www.bullshit.com") // test CORS resp, err := client.Do(req) From 6d4abcf46360a89405990b0315e86c485a231e4c Mon Sep 17 00:00:00 2001 From: Brian Yeh Date: Tue, 7 Nov 2023 15:01:16 -0800 Subject: [PATCH 3/6] add option. --- cardinal/option.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cardinal/option.go b/cardinal/option.go index 9f6882d17..8c8d295fb 100644 --- a/cardinal/option.go +++ b/cardinal/option.go @@ -85,6 +85,6 @@ func WithPrettyLog() WorldOption { func WithCORS() WorldOption { return WorldOption{ - serverOption: nil, + serverOption: server.WithCORS(), } } From 1abd74ae9ae2ec7c02ab207291bc0dca0ab91beb Mon Sep 17 00:00:00 2001 From: Brian Yeh Date: Tue, 7 Nov 2023 15:11:34 -0800 Subject: [PATCH 4/6] added coverage for cardinal WithCORS() --- cardinal/cardinal_test.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/cardinal/cardinal_test.go b/cardinal/cardinal_test.go index a4eab7501..73f55b894 100644 --- a/cardinal/cardinal_test.go +++ b/cardinal/cardinal_test.go @@ -1,6 +1,7 @@ package cardinal_test import ( + "net/http" "os" "os/exec" "strconv" @@ -28,9 +29,8 @@ func TestNewWorld(t *testing.T) { func TestCanQueryInsideSystem(t *testing.T) { testutils.SetTestTimeout(t, 10*time.Second) - world, doTick := testutils.MakeWorldAndTicker(t) + world, doTick := testutils.MakeWorldAndTicker(t, cardinal.WithCORS()) assert.NilError(t, cardinal.RegisterComponent[Foo](world)) - wantNumOfEntities := 10 world.Init(func(worldCtx cardinal.WorldContext) { _, err := cardinal.CreateMany(worldCtx, wantNumOfEntities, Foo{}) @@ -59,7 +59,7 @@ func TestShutdownViaSignal(t *testing.T) { // If this test is frozen then it failed to shut down, create a failure with panic. var wg sync.WaitGroup testutils.SetTestTimeout(t, 10*time.Second) - world, err := cardinal.NewMockWorld() + world, err := cardinal.NewMockWorld(cardinal.WithCORS()) assert.NilError(t, cardinal.RegisterComponent[Foo](world)) assert.NilError(t, err) wantNumOfEntities := 10 @@ -77,6 +77,19 @@ func TestShutdownViaSignal(t *testing.T) { // wait until game loop is running time.Sleep(500 * time.Millisecond) } + //for { + // time.Sleep(1 * time.Second) + //} + //test CORS with cardinal + client := &http.Client{} + req, err := http.NewRequest(http.MethodPost, "http://localhost:4040/query/http/endpoints", nil) + assert.NilError(t, err) + req.Header.Set("Origin", "http://www.bullshit.com") // test CORS + resp, err := client.Do(req) + assert.NilError(t, err) + v := resp.Header.Get("Access-Control-Allow-Origin") + assert.Equal(t, v, "*") + assert.Equal(t, resp.StatusCode, 200) conn, _, err := websocket.DefaultDialer.Dial("ws://localhost:4040/events", nil) assert.NilError(t, err) From ccf71211ef493e6829e5f7f3b8048a8af4b9c857 Mon Sep 17 00:00:00 2001 From: Brian Yeh Date: Tue, 7 Nov 2023 15:12:47 -0800 Subject: [PATCH 5/6] add cors test. --- cardinal/cardinal_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/cardinal/cardinal_test.go b/cardinal/cardinal_test.go index 73f55b894..16ead6f7a 100644 --- a/cardinal/cardinal_test.go +++ b/cardinal/cardinal_test.go @@ -77,9 +77,6 @@ func TestShutdownViaSignal(t *testing.T) { // wait until game loop is running time.Sleep(500 * time.Millisecond) } - //for { - // time.Sleep(1 * time.Second) - //} //test CORS with cardinal client := &http.Client{} req, err := http.NewRequest(http.MethodPost, "http://localhost:4040/query/http/endpoints", nil) From d25333b15f0936fe4e1de3f80b58081d724bb1cf Mon Sep 17 00:00:00 2001 From: Brian Yeh Date: Tue, 7 Nov 2023 16:39:03 -0800 Subject: [PATCH 6/6] lint bs. --- cardinal/cardinal_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cardinal/cardinal_test.go b/cardinal/cardinal_test.go index 16ead6f7a..355c1f8f0 100644 --- a/cardinal/cardinal_test.go +++ b/cardinal/cardinal_test.go @@ -77,7 +77,7 @@ func TestShutdownViaSignal(t *testing.T) { // wait until game loop is running time.Sleep(500 * time.Millisecond) } - //test CORS with cardinal + // test CORS with cardinal client := &http.Client{} req, err := http.NewRequest(http.MethodPost, "http://localhost:4040/query/http/endpoints", nil) assert.NilError(t, err)