Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(starter-game-template): Make template use new swagger routes and various other changes #11

Merged
merged 8 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 6 additions & 0 deletions .run/Remote Debug Cardinal port 40000.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<component name="ProjectRunConfigurationManager">
pyrofolium marked this conversation as resolved.
Show resolved Hide resolved
<configuration default="false" name="Remote Debug Cardinal port 40000" type="GoRemoteDebugConfigurationType" factoryName="Go Remote" port="40000">
<option name="disconnectOption" value="ASK" />
<method v="2" />
</configuration>
</component>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ A persona tag is essentially a cardinal based user. To create a persona tag in y
endpoint from the dropdown. Make sure to paste in a valid User ID into the User ID field. Set the request body to:
```json
{
"persona_tag": "some-persona-tag",
"persona_tag": "some-persona-tag"
}
```

Expand Down
2 changes: 1 addition & 1 deletion cardinal/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20 AS builder
FROM golang:1.21 AS builder

WORKDIR /usr/src/app

Expand Down
17 changes: 17 additions & 0 deletions cardinal/Dockerfile.debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.21 AS build

# Build Delve
RUN go install github.com/go-delve/delve/cmd/dlv@latest

pyrofolium marked this conversation as resolved.
Show resolved Hide resolved
WORKDIR /usr/src/app

copy . .
copy vendor vendor/

RUN go build -gcflags="all=-N -l" -v -o /usr/local/bin/app

FROM ubuntu:22.04
COPY --from=build /go/bin/dlv /usr/local/bin
COPY --from=build /usr/local/bin/app /usr/local/bin/

CMD ["dlv", "--listen=:40000", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "/usr/local/bin/app"]
pyrofolium marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion cardinal/component/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ type HealthComponent struct {
HP int
}

var Health = ecs.NewComponentType[HealthComponent]()
var Health = ecs.NewComponentType[HealthComponent]("Health")
4 changes: 2 additions & 2 deletions cardinal/component/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package component
import "pkg.world.dev/world-engine/cardinal/ecs"

type PlayerComponent struct {
Nickname string
Nickname string `json:"nickname"`
}

var Player = ecs.NewComponentType[PlayerComponent]()
var Player = ecs.NewComponentType[PlayerComponent]("Player")
5 changes: 3 additions & 2 deletions cardinal/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ replace (
require github.com/rs/zerolog v1.30.0

require (
github.com/alecthomas/participle/v2 v2.1.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.3 // indirect
Expand Down Expand Up @@ -130,8 +131,8 @@ require (
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
pkg.world.dev/world-engine/cardinal v0.1.33-alpha
pkg.world.dev/world-engine/cardinal v0.1.36-alpha
pkg.world.dev/world-engine/chain v0.1.11-alpha // indirect
pkg.world.dev/world-engine/sign v0.1.7-alpha // indirect
pkg.world.dev/world-engine/sign v0.1.8-alpha // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
16 changes: 12 additions & 4 deletions cardinal/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/alecthomas/assert/v2 v2.3.0 h1:mAsH2wmvjsuvyBvAmCtm7zFsBlb8mIHx5ySLVdDZXL0=
github.com/alecthomas/assert/v2 v2.3.0/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/participle/v2 v2.1.0 h1:z7dElHRrOEEq45F2TG5cbQihMtNTv8vwldytDj7Wrz4=
github.com/alecthomas/participle/v2 v2.1.0/go.mod h1:Y1+hAs8DHPmc3YUFzqllV+eSQ9ljPTk0ZkPMtEdAx2c=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down Expand Up @@ -866,6 +872,8 @@ github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/hdevalence/ed25519consensus v0.1.0 h1:jtBwzzcHuTmFrQN6xQZn6CQEO/V9f7HsjsjeEZ6auqU=
github.com/hdevalence/ed25519consensus v0.1.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/holiman/uint256 v1.2.3 h1:K8UWO1HUJpRMXBxbmaY1Y8IAMZC/RsKB+ArEnnK4l5o=
github.com/holiman/uint256 v1.2.3/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down Expand Up @@ -1903,12 +1911,12 @@ nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
pkg.world.dev/world-engine/cardinal v0.1.33-alpha h1:lSqsYIz+JsHCiZozqvlmEc+XOjB5ZIY5vH93JWne6wg=
pkg.world.dev/world-engine/cardinal v0.1.33-alpha/go.mod h1:cGVvoACq1bWWy13te3BRJW7+SE5MK4hUYa+0K/69imU=
pkg.world.dev/world-engine/cardinal v0.1.36-alpha h1:HsxCwTqBLA+43mG/8fZodoyLSsM+WvT8CiLBG8lltyI=
pkg.world.dev/world-engine/cardinal v0.1.36-alpha/go.mod h1:lT+hsjIGdT0rbjSmdCL4m3Mkl5ag27Lc+HkID5yVWDA=
pkg.world.dev/world-engine/chain v0.1.11-alpha h1:a+a+eZUIG2XuO+PL5WBr+IgQZoGRnm8plk5bP1KKoR4=
pkg.world.dev/world-engine/chain v0.1.11-alpha/go.mod h1:qpm1QXHj2RyXIiwkEolaZMMqeNVcMX+hH4OQ9nE0/5M=
pkg.world.dev/world-engine/sign v0.1.7-alpha h1:7Oaoc0OUBP0fAPGna+/fw5fbrNw88sIVzr0SujY0wgQ=
pkg.world.dev/world-engine/sign v0.1.7-alpha/go.mod h1:JcKXhdeYvCPRAgsCUSFEDcAJgf67VgMWWrA2Y2VZJSg=
pkg.world.dev/world-engine/sign v0.1.8-alpha h1:MZNsYdmQVMy1Fc29d1XSgtVl2T4HDRbdmQ3pruLyQwM=
pkg.world.dev/world-engine/sign v0.1.8-alpha/go.mod h1:vSLK2OoAN+ZVJsZK58YiLXLvQZOJwuhT9R05J664ypE=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
9 changes: 8 additions & 1 deletion cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"time"

"github.com/argus-labs/starter-game-template/cardinal/component"
Expand Down Expand Up @@ -73,9 +74,15 @@ func main() {
world.StartGameLoop(context.Background(), time.Second)

// TODO: When launching to production, you should enable signature verification.
fmt.Println("Serving Cardinal at: ", cfg.CardinalPort)
h, err := server.NewHandler(world, server.WithPort(cfg.CardinalPort), server.DisableSignatureVerification())
if err != nil {
panic(err)
}
h.Serve()
err = h.Serve()
if err != nil {
log.Fatal().Err(err)
return
}

}
91 changes: 91 additions & 0 deletions docker-compose-debug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
version: "3"
pyrofolium marked this conversation as resolved.
Show resolved Hide resolved
services:
postgres:
command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all
environment:
- POSTGRES_DB=nakama
- POSTGRES_PASSWORD=localdb
expose:
- "8080"
- "5432"
image: postgres:12.2-alpine
ports:
- "5432:5432"
- "8080:8080"
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres", "-d", "nakama"]
interval: 3s
timeout: 3s
retries: 5
volumes:
- data:/var/lib/postgresql/data
redis: # This doesn't have the correct persistence settings. Don't use on for prod.
image: redis:latest
command: redis-server # TODO: This runs without password. Don't use for prod.
expose:
- "6379"
ports:
- "6379:6379"
restart: always
cardinal:
build:
context: ./cardinal
dockerfile: Dockerfile.debug
pyrofolium marked this conversation as resolved.
Show resolved Hide resolved
depends_on:
- redis
expose:
- "3333"
- "40000"
ports:
- "3333:3333"
- "40000:40000"
environment:
- CARDINAL_PORT=3333
- REDIS_ADDR=redis:6379
- REDIS_MODE=normal
cap_add:
- SYS_PTRACE
security_opt:
- "seccomp:unconfined"
pyrofolium marked this conversation as resolved.
Show resolved Hide resolved
nakama:
platform: linux/amd4
build: ./nakama
depends_on:
- postgres
- cardinal
environment:
- CARDINAL_ADDR=${CARDINAL_ADDR:-http://cardinal:3333}
- CARDINAL_NAMESPACE=world
entrypoint:
- "/bin/sh"
- "-ecx"
- >
/nakama/nakama migrate up --database.address postgres:localdb@postgres:5432/nakama &&
exec /nakama/nakama --config /nakama/data/local.yml --database.address postgres:localdb@postgres:5432/nakama
extra_hosts:
- "host.docker.internal:host-gateway"
expose:
- "7349"
- "7350"
- "7351"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:7350/"]
interval: 10s
timeout: 5s
retries: 5
links:
- "postgres:db"
ports:
- "7349:7349"
- "7350:7350"
- "7351:7351"
restart: unless-stopped
testsuite:
build: ./testsuite
depends_on:
- cardinal
- nakama
environment:
- NAKAMA_ADDRESS=http://nakama:7350
volumes:
data:
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ services:
- REDIS_ADDR=redis:6379
- REDIS_MODE=normal
nakama:
platform: linux/amd4
build: ./nakama
pyrofolium marked this conversation as resolved.
Show resolved Hide resolved
depends_on:
- postgres
Expand Down
13 changes: 13 additions & 0 deletions magefiles/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ func Start() error {
return nil
}

// Start starts Nakama and cardinal in debug mode with cardinal debugger listening on port 40000
// Note Cardinal server will not run until a debugger is attached port 40000
func StartDebug() error {
mg.Deps(exitMagefilesDir)
if err := prepareDirs("cardinal", "nakama"); err != nil {
return err
}
if err := sh.RunV("docker", "compose", "-f", "docker-compose-debug.yml", "up", "--build", "cardinal", "nakama"); err != nil {
return err
}
return nil
}

// StartDetach starts Nakama and cardinal with detach and wait-timeout 60s (suit for CI workflow)
func StartDetach() error {
mg.Deps(exitMagefilesDir)
Expand Down
50 changes: 32 additions & 18 deletions nakama/cardinal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import (
)

var (
listTxEndpointsEndpoint = "list/tx-endpoints"
listReadEndpoints = "list/read-endpoints"
createPersonaEndpoint = "tx-create-persona"
readPersonaSignerEndpoint = "read-persona-signer"
transactionReceiptsEndpoint = "transaction-receipts"
listEndpoints = "query/http/endpoints"
createPersonaEndpoint = "tx/persona/create-persona"
readPersonaSignerEndpoint = "query/persona/signer"
transactionReceiptsEndpoint = "query/receipts/list"

readPersonaSignerStatusUnknown = "unknown"
readPersonaSignerStatusAvailable = "available"
Expand Down Expand Up @@ -50,9 +49,14 @@ func makeURL(resource string) string {
return fmt.Sprintf("%s/%s", globalCardinalAddress, resource)
}

func cardinalListEndpoints(path string) ([]string, error) {
url := makeURL(path)
resp, err := http.Get(url)
type endpoints struct {
TxEndpoints []string `json:"tx_endpoints"`
QueryEndpoints []string `json:"query_endpoints"`
}

func cardinalGetEndpointsStruct() (*endpoints, error) {
pyrofolium marked this conversation as resolved.
Show resolved Hide resolved
url := makeURL(listEndpoints)
resp, err := http.Post(url, "", nil)
if err != nil {
return nil, err
}
Expand All @@ -61,27 +65,35 @@ func cardinalListEndpoints(path string) ([]string, error) {
return nil, fmt.Errorf("list endpoints (at %q) failed with status code %d: %v", url, resp.StatusCode, string(buf))
}
dec := json.NewDecoder(resp.Body)
var endpoints []string
if err := dec.Decode(&endpoints); err != nil {
var endpointsStruct endpoints
if err := dec.Decode(&endpointsStruct); err != nil {
return nil, err
}
return endpoints, nil
return &endpointsStruct, nil
}

func cardinalListQueryEndpoints() ([]string, error) {
endpointsStruct, err := cardinalGetEndpointsStruct()
if err != nil {
return nil, err
}
return endpointsStruct.QueryEndpoints, nil
}

func cardinalListAllEndpoints() ([]string, error) {
var endpoints []string
txs, err := cardinalListEndpoints(listTxEndpointsEndpoint)
func cardinalListTxEndpoints() ([]string, error) {
endpointsStruct, err := cardinalGetEndpointsStruct()
if err != nil {
return nil, err
}
endpoints = append(endpoints, txs...)
reads, err := cardinalListEndpoints(listReadEndpoints)
return endpointsStruct.QueryEndpoints, nil
}

func cardinalListAllEndpoints() ([]string, error) {
pyrofolium marked this conversation as resolved.
Show resolved Hide resolved
endpointsStruct, err := cardinalGetEndpointsStruct()
if err != nil {
return nil, err
}
endpoints = append(endpoints, reads...)
return endpoints, nil
return append(endpointsStruct.TxEndpoints, endpointsStruct.QueryEndpoints...), nil
}

func doRequest(req *http.Request) (*http.Response, error) {
Expand Down Expand Up @@ -124,6 +136,7 @@ func cardinalCreatePersona(ctx context.Context, nk runtime.NakamaModule, persona
if err != nil {
return "", 0, fmt.Errorf("unable to make request to %q: %w", createPersonaEndpoint, err)
}
req.Header.Set("Content-Type", "application/json")

resp, err := doRequest(req)
if err != nil {
Expand Down Expand Up @@ -162,6 +175,7 @@ func cardinalQueryPersonaSigner(ctx context.Context, personaTag string, tick uin
if err != nil {
return "", err
}
httpReq.Header.Set("Content-Type", "application/json")
httpResp, err := doRequest(httpReq)
if err != nil {
return "", err
Expand Down
1 change: 1 addition & 0 deletions nakama/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (r *receiptsDispatcher) getBatchOfReceiptsFromCardinal(startTick uint64) (r
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := doRequest(req)
if err != nil {
return nil, fmt.Errorf("failed to query %q: %w", url, err)
Expand Down
Loading
Loading