Skip to content

Commit

Permalink
Merge pull request #15 from maxfelker/TM-145-cors-issues
Browse files Browse the repository at this point in the history
Fixing CORS issue
  • Loading branch information
maxfelker authored Aug 4, 2024
2 parents b5caf69 + bce1c4b commit e9ecf17
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ Persistence cloud API to store locations of objects in Unity 3D space.
First, create a `.env` file in the root of the directory with the following values:

```bash
ALLOWED_ORIGINS=http://localhost:5173,https://localhost:5173,http://localhost,https://localhost

POSTGRES_HOST=postgres
POSTGRES_USER=tmdbuser
POSTGRES_PASSWORD=bigland
POSTGRES_DATABASE=terramajor
POSTGRES_SSL=disable

COSMOS_DB_HOST=cosmos
COSMOS_DB_PORT=8081
COSMOS_DB_PRIMARY_KEY=xxx
COSMOS_DB_NAME=terramajor
```

Next, run the local database:
Expand Down
10 changes: 7 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.9"


x-db-variables: &db-variables
POSTGRES_HOST: $POSTGRES_HOST
Expand All @@ -11,6 +11,10 @@ x-db-variables: &db-variables
COSMOS_DB_PRIMARY_KEY: $COSMOS_DB_PRIMARY_KEY
COSMOS_DB_NAME: $COSMOS_DB_NAME

x-env-variables: &env-variables
<<: *db-variables
ALLOWED_ORIGINS: $ALLOWED_ORIGINS

networks:
local:

Expand Down Expand Up @@ -60,7 +64,7 @@ services:
context: .
dockerfile: ./Dockerfile.dev
environment:
<<: *db-variables
<<: *env-variables
PORT: 8000
networks:
local:
Expand All @@ -74,7 +78,7 @@ services:
context: .
dockerfile: ./Dockerfile
environment:
<<: *db-variables
<<: *env-variables
PORT: 80
networks:
local:
Expand Down
23 changes: 19 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"net/http"
"os"
"strings"

"github.com/gorilla/handlers"

Expand Down Expand Up @@ -38,6 +40,21 @@ func main() {
http.ListenAndServe(":"+PORT, nil)
}

// Create a CORS handler with allowed origins
func createCorsHandler() func(http.Handler) http.Handler {
orginsArray := os.Getenv("ALLOWED_ORIGINS")
fmt.Println("Allowed origins: " + orginsArray)
allowedOrigins := strings.Split(orginsArray, ",")
allowedMethods := []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}
allheaders := []string{"X-Requested-With", "Content-Type", "Authorization"}

return handlers.CORS(
handlers.AllowedMethods(allowedMethods),
handlers.AllowedOrigins(allowedOrigins),
handlers.AllowedHeaders(allheaders),
)
}

func registerMuxHandlers(app *core.App) http.Handler {
// Auth
app.Router.HandleFunc("/login", accounts.Login(app)).Methods("POST")
Expand Down Expand Up @@ -75,9 +92,7 @@ func registerMuxHandlers(app *core.App) http.Handler {

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"}))
corsHandler := createCorsHandler()

return corsObj(app.Router)
return corsHandler(app.Router)
}

0 comments on commit e9ecf17

Please sign in to comment.