From 43680ece164e5ae6d44be2cde26d310ad66f58c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sun, 4 Jan 2026 15:37:09 +0100 Subject: [PATCH] Add NATS deployment and service configuration, integrate environment variables, and update server to support NATS and Redis connections. --- .github/workflows/deploy.yaml | 1 + cmd/server/main.go | 14 +++++++-- k8s/app.yaml | 5 ++++ k8s/nats.yaml | 55 +++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 k8s/nats.yaml diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 60f2f85..60c3f41 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -104,6 +104,7 @@ jobs: # Anwenden echo "Deploying Resources to Namespace: ${{ env.TARGET_NS }}" kubectl apply -f k8s/pvc.yaml -n ${{ env.TARGET_NS }} + kubectl apply -f k8s/nats.yaml -n ${{ env.TARGET_NS }} kubectl apply -f k8s/redis.yaml -n ${{ env.TARGET_NS }} kubectl apply -f k8s/app.yaml -n ${{ env.TARGET_NS }} kubectl apply -f k8s/ingress.yaml -n ${{ env.TARGET_NS }} diff --git a/cmd/server/main.go b/cmd/server/main.go index d66ee4b..7a71c1a 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -4,6 +4,7 @@ import ( "encoding/json" "io/ioutil" "log" + "os" "path/filepath" "sync" @@ -29,12 +30,14 @@ func main() { loadServerAssets(globalWorld) // 1b. Redis-Leaderboard initialisieren - if err := server.InitLeaderboard("localhost:6379"); err != nil { + redisAddr := getEnv("REDIS_ADDR", "localhost:6379") + if err := server.InitLeaderboard(redisAddr); err != nil { log.Fatal("❌ Konnte nicht zu Redis verbinden: ", err) } + log.Printf("✅ Verbunden mit Redis: %s", redisAddr) // 2. NATS VERBINDUNG - natsURL := "nats://localhost:4222" + natsURL := getEnv("NATS_URL", "nats://localhost:4222") nc, err := nats.Connect(natsURL) if err != nil { log.Fatal("❌ Konnte nicht zu NATS verbinden: ", err) @@ -179,6 +182,13 @@ func main() { } } +func getEnv(key, defaultValue string) string { + if value := os.Getenv(key); value != "" { + return value + } + return defaultValue +} + func loadServerAssets(w *game.World) { assetDir := "./cmd/client/web/assets" chunkDir := filepath.Join(assetDir, "chunks") diff --git a/k8s/app.yaml b/k8s/app.yaml index b5e4695..a3fef9a 100644 --- a/k8s/app.yaml +++ b/k8s/app.yaml @@ -22,6 +22,11 @@ spec: - containerPort: 8080 name: http protocol: TCP + env: + - name: REDIS_ADDR + value: "redis-service:6379" + - name: NATS_URL + value: "nats://nats-service:4222" livenessProbe: httpGet: path: /health diff --git a/k8s/nats.yaml b/k8s/nats.yaml new file mode 100644 index 0000000..62335dd --- /dev/null +++ b/k8s/nats.yaml @@ -0,0 +1,55 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nats + labels: + app: nats +spec: + replicas: 1 + selector: + matchLabels: + app: nats + template: + metadata: + labels: + app: nats + spec: + containers: + - name: nats + image: nats:2-alpine + ports: + - containerPort: 4222 + name: client + - containerPort: 8222 + name: monitoring + resources: + requests: + memory: "64Mi" + cpu: "100m" + limits: + memory: "256Mi" + cpu: "500m" + args: + - "-js" + - "-m" + - "8222" +--- +apiVersion: v1 +kind: Service +metadata: + name: nats-service + labels: + app: nats +spec: + type: ClusterIP + selector: + app: nats + ports: + - port: 4222 + targetPort: 4222 + name: client + protocol: TCP + - port: 8222 + targetPort: 8222 + name: monitoring + protocol: TCP