Skip to content

Commit

Permalink
Merge pull request #11 from maxfelker/generate-heightmaps
Browse files Browse the repository at this point in the history
Create TerrainChunkConfigs in database and allow clients to  query chunks by sandboxId + position
  • Loading branch information
maxfelker authored Dec 16, 2023
2 parents 9764bd8 + 4231474 commit 0c12e35
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 15 deletions.
26 changes: 26 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Build and run dev service locally",
"type": "shell",
"command": "docker compose up --build dev",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Build and run release service locally",
"type": "shell",
"command": "docker compose up --build release",
"problemMatcher": [],
"group": {
"kind": "test",
"isDefault": false
}
}
]
}

5 changes: 4 additions & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
FROM golang:alpine
WORKDIR /app
ENV GO111MODULE=on
RUN go install github.com/cosmtrek/air@latest
COPY main.go .
COPY pkg/ pkg/
COPY go.sum go.sum
COPY go.mod go.mod
RUN go mod tidy
RUN go mod download
RUN mkdir keys/
RUN go run pkg/auth/pem/main.go /app/keys terra-major-client
ENTRYPOINT go run main.go
ENTRYPOINT air
31 changes: 30 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,40 @@ x-db-variables: &db-variables
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
POSTGRES_DATABASE: $POSTGRES_DATABASE
POSTGRES_SSL: $POSTGRES_SSL
COSMOS_DB_HOST: $COSMOS_DB_HOST
COSMOS_DB_PORT: $COSMOS_DB_PORT
COSMOS_DB_PRIMARY_KEY: $COSMOS_DB_PRIMARY_KEY
COSMOS_DB_NAME: $COSMOS_DB_NAME

networks:
local:

services:

cosmos:
container_name: cosmos
image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
tty: true
restart: always
mem_limit: 3G
cpu_count: 4
environment:
- AZURE_COSMOS_EMULATOR_PARTITION_COUNT=5
- AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true
ports:
- ${COSMOS_DB_PORT}:${COSMOS_DB_PORT}
- 10250-10255:10250-10255
hostname: cosmos
networks:
local:
aliases:
- cosmos
entrypoint: /bin/bash -c
command:
- |
apt-get update -y && apt-get install -y curl
/usr/local/bin/cosmos/start.sh
postgres:
image: postgres:15.3-alpine
restart: always
Expand All @@ -33,7 +61,6 @@ services:
dockerfile: ./Dockerfile.dev
environment:
<<: *db-variables
PKG: characters
PORT: 8000
networks:
local:
Expand All @@ -56,4 +83,6 @@ services:

volumes:
postgres:
driver: local
cosmos:
driver: local
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@ module github.com/mw-felker/terra-major-api
go 1.20

require (
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.5
github.com/aquilax/go-perlin v1.1.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/uuid v1.3.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
golang.org/x/crypto v0.8.0
golang.org/x/crypto v0.12.0
gorm.io/driver/postgres v1.5.2
gorm.io/gorm v1.25.1
)

require (
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.3.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
golang.org/x/text v0.9.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/text v0.12.0 // indirect
)
30 changes: 25 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.0 h1:8q4SaHjFsClSvuVne0ID/5Ka8u3fcIHyqkLjcFpNRHQ=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.0/go.mod h1:bjGvMhVMb+EEm3VRNQawDMUyMMjo+S5ewNjflkep/0Q=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0 h1:vcYCAze6p19qBW7MhZybIsqD8sMV8js0NyQM8JDnVtg=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0/go.mod h1:OQeznEEkTZ9OrhHJoDD8ZDq51FHgXjqtP9z6bEwBq9U=
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.5 h1:qS0Bp4do0cIvnuQgSGeO6ZCu/q/HlRKl4NPfv1eJ2p0=
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.5/go.mod h1:Beh5cHIXJ0oWEDWk9lNFtuklCojLLQ5hl+LqSNTTs0I=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 h1:sXr+ck84g/ZlZUOZiNELInmMgOsuGwdjjVkEIde0OtY=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0/go.mod h1:okt5dMMTOFjX/aovMlrjvvXoPMBVSPzk9185BT0+eZM=
github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0 h1:OBhqkivkhkMqLPymWEppkm7vgPQY2XsHoEkaMQ0AdZY=
github.com/aquilax/go-perlin v1.1.0 h1:Gg+3jQ24wT4Y5GI7TCRLmYarzUG0k+n/JATFqOimb7s=
github.com/aquilax/go-perlin v1.1.0/go.mod h1:z9Rl7EM4BZY0Ikp2fEN1I5mKSOJ26HQpk0O2TBdN2HE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
Expand All @@ -20,16 +34,22 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
core "github.com/mw-felker/terra-major-api/pkg/core"
sandboxes "github.com/mw-felker/terra-major-api/pkg/sandboxes/handlers"
sandboxModels "github.com/mw-felker/terra-major-api/pkg/sandboxes/models"
terrains "github.com/mw-felker/terra-major-api/pkg/terrains/handlers"
terrainModels "github.com/mw-felker/terra-major-api/pkg/terrains/models"
utils "github.com/mw-felker/terra-major-api/pkg/utils"
)

Expand All @@ -23,6 +25,7 @@ func seedDb(app *core.App) {
app.DB.AutoMigrate(&characterModels.Character{})
app.DB.AutoMigrate(&sandboxModels.Sandbox{})
app.DB.AutoMigrate(&sandboxModels.Instance{})
app.DB.AutoMigrate(&terrainModels.TerrainChunkConfig{})
}

func main() {
Expand Down Expand Up @@ -65,6 +68,8 @@ func main() {
app.Router.HandleFunc("/sandboxes/{sandboxId}/instances/{instanceId}", sandboxes.UpdateInstance(app)).Methods("PATCH")
app.Router.HandleFunc("/sandboxes/{sandboxId}/instances/{instanceId}", sandboxes.ArchiveInstance(app)).Methods("DELETE")

app.Router.HandleFunc("/sandboxes/{sandboxId}/chunks", terrains.GetChunksBySandboxId(app)).Methods("GET")

corsObj := handlers.CORS(handlers.AllowedMethods([]string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}),
handlers.AllowedOrigins([]string{"*"}),
handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}))
Expand Down
9 changes: 9 additions & 0 deletions pkg/characters/handlers/CreateCharacter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
characters "github.com/mw-felker/terra-major-api/pkg/characters/models"
"github.com/mw-felker/terra-major-api/pkg/core"
sandboxes "github.com/mw-felker/terra-major-api/pkg/sandboxes/models"
"github.com/mw-felker/terra-major-api/pkg/terrains"
utils "github.com/mw-felker/terra-major-api/pkg/utils"
)

Expand Down Expand Up @@ -56,6 +57,14 @@ func CreateCharacter(app *core.App) http.HandlerFunc {
return
}

chunks := terrains.GenerateChunksForSandbox(newSandbox.ID)

chunkCreateResult := app.DB.Create(&chunks)
if chunkCreateResult.Error != nil {
http.Error(writer, chunkCreateResult.Error.Error(), http.StatusInternalServerError)
return
}

response, e := json.Marshal(newCharacter)
if e != nil {
utils.ReturnError(writer, e.Error())
Expand Down
43 changes: 43 additions & 0 deletions pkg/core/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package core

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/gorilla/mux"
"github.com/mw-felker/terra-major-api/pkg/utils"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

type App struct {
DB *gorm.DB
NoSQL *azcosmos.DatabaseClient
Router *mux.Router
}

Expand Down Expand Up @@ -49,14 +53,53 @@ func connectToDb(dbName string) *gorm.DB {
return db
}

func connectToCosmos() (*azcosmos.DatabaseClient, error) {
COSMOS_DB_PORT := utils.GetEnv("COSMOS_DB_PORT", "8081")
COSMOS_DB_HOST := utils.GetEnv("COSMOS_DB_HOST", "cosmos")
url := "https://" + COSMOS_DB_HOST + ":" + COSMOS_DB_PORT
COSMOS_DB_PRIMARY_KEY := utils.GetEnv("COSMOS_DB_PRIMARY_KEY")
COSMOS_DB_NAME := utils.GetEnv("COSMOS_DB_NAME", "terramajor")

cred, err := azcosmos.NewKeyCredential(COSMOS_DB_PRIMARY_KEY)
if err != nil {
log.Println("Failed to create Cosmos DB key credential:", err)
return nil, err
}

client, err := azcosmos.NewClientWithKey(url, cred, nil)
if err != nil {
log.Println("Failed to create Cosmos client:", err)
return nil, err
}

database, err := client.NewDatabase(COSMOS_DB_NAME)
if err != nil {
log.Println("Failed to create Cosmos DB database client:", err)
return nil, err
}

return database, nil
}

func CreateApp() *App {
fmt.Println("Starting up app...")
var dbConnection = connectToDb("postgres")
var POSTGRES_DATABASE = utils.GetEnv("POSTGRES_DATABASE", "terramajor")
checkDbExistsAndCreate(dbConnection, POSTGRES_DATABASE)

db := connectToDb(POSTGRES_DATABASE)
db.Logger = logger.Default.LogMode(logger.Silent)
//db.Logger.LogMode(logger.Silent)

noSqlClient, error := connectToCosmos()
if error != nil {
log.Println("Failed to get Cosmos DB database client:", error)
return nil
}

return &App{
DB: connectToDb(POSTGRES_DATABASE),
NoSQL: noSqlClient,
Router: mux.NewRouter(),
}
}
34 changes: 30 additions & 4 deletions pkg/sandboxes/handlers/CreateSandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@ package handlers

import (
"encoding/json"
"fmt"
"net/http"
"strings"

authClient "github.com/mw-felker/terra-major-api/pkg/auth/client"
"github.com/mw-felker/terra-major-api/pkg/core"
models "github.com/mw-felker/terra-major-api/pkg/sandboxes/models"
"github.com/mw-felker/terra-major-api/pkg/terrains"
utils "github.com/mw-felker/terra-major-api/pkg/utils"
)

func CreateSandbox(app *core.App) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {

claims, claimsError := authClient.ParseAndValidateToken(request)
if claimsError != nil {
utils.ReturnError(writer, claimsError.Error(), http.StatusUnauthorized)
return
}

accountId := claims.AccountId
//characterId := claims.CharacterId

decoder := json.NewDecoder(request.Body)
var newSandbox models.Sandbox
err := decoder.Decode(&newSandbox)
if err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
decoderError := decoder.Decode(&newSandbox)
if decoderError != nil {
http.Error(writer, decoderError.Error(), http.StatusBadRequest)
return
}

Expand All @@ -24,16 +38,28 @@ func CreateSandbox(app *core.App) http.HandlerFunc {
return
}

newSandbox.AccountId = accountId

result := app.DB.Create(&newSandbox)
if result.Error != nil {
if strings.Contains(result.Error.Error(), "23505") {
http.Error(writer, "A sandbox for this characterId already exists", http.StatusConflict)
} else {
http.Error(writer, result.Error.Error(), http.StatusInternalServerError)
}
return
}

chunks := terrains.GenerateChunksForSandbox(newSandbox.ID)

// Print the chunks to the command line
fmt.Println(chunks)

chunkCreateResult := app.DB.Create(&chunks)
if chunkCreateResult.Error != nil {
http.Error(writer, chunkCreateResult.Error.Error(), http.StatusInternalServerError)
return
}

response, e := json.Marshal(newSandbox)
if e != nil {
http.Error(writer, e.Error(), http.StatusInternalServerError)
Expand Down
Loading

0 comments on commit 0c12e35

Please sign in to comment.