ADD: added team management

This commit is contained in:
hwinkel
2025-11-28 14:31:12 +01:00
parent 3a6c3a86e3
commit aac5a3c21d
14 changed files with 587 additions and 166 deletions

View File

@@ -1,11 +1,14 @@
package main
import (
"fmt"
"log"
"os"
"time"
"volleyball/internal/auth"
"volleyball/internal/database"
"volleyball/internal/player"
"volleyball/internal/team"
"volleyball/internal/tournament"
"github.com/gin-contrib/cors"
@@ -29,25 +32,49 @@ func main() {
os.Exit(1)
}
r := gin.Default()
router := gin.Default()
// 2. CORS-Konfiguration
// Lese die Frontend-URL aus den Umgebungsvariablen
frontendURL := os.Getenv("FRONTEND_URL")
// Lokaler Fallback (wichtig für die Entwicklung)
allowedOrigins := []string{
"http://localhost:3000", // Gängiger React-Dev-Port
// "http://localhost:5173", // Gängiger Vite-Dev-Port
}
if frontendURL != "" {
allowedOrigins = append(allowedOrigins, frontendURL)
fmt.Printf("CORS: Erlaubte Produktiv-URL hinzugefügt: %s\n", frontendURL)
} else {
log.Println("ACHTUNG: FRONTEND_URL fehlt in den Umgebungsvariablen. Nur lokale URLs erlaubt.")
}
// CORS
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:3000"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowHeaders: []string{"Authorization", "Content-Type"},
// Konfiguriere die CORS-Middleware
config := cors.Config{
// Setze die erlaubten Ursprünge (deine React-URLs)
AllowOrigins: allowedOrigins,
// Erlaube die notwendigen HTTP-Methoden
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
// Erlaube Header (z.B. für JSON und Authentifizierung)
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"},
// Erlaube Cookies und Credentials (falls du Tokens oder Sessions nutzt)
AllowCredentials: true,
}))
// Wie lange die Preflight-Anfrage (OPTIONS) gecacht werden darf
MaxAge: 12 * time.Hour,
}
router.Use(cors.New(config))
// Public
r.POST("/api/login", func(c *gin.Context) {
router.POST("/api/login", func(c *gin.Context) {
auth.LoginHandler(c, db.GetDB())
})
r.GET("/api/tournaments", tournament.ListTournaments)
router.GET("/api/tournaments", tournament.ListTournaments)
// Protected API
api := r.Group("/api")
api := router.Group("/api")
api.Use(auth.AuthMiddleware())
api.GET("/tournaments/:id", tournament.GetTournament)
@@ -74,12 +101,21 @@ func main() {
// c.JSON(http.StatusOK, gin.H{"message": "Player deleted successfully"})
})
api.GET("/teams", func(c *gin.Context) {
log.Println("get Teams called")
team.GetTeams(c, db.GetDB())
})
api.POST("/teams", func(c *gin.Context) {
log.Println("create teams called")
team.CreateTeam(c, db.GetDB())
})
api.PUT("/teams/:uuid", func(c *gin.Context) {
log.Println("update teams called")
team.UpdateTeam(c, db.GetDB())
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
r.Run(":" + port)
router.Run(":" + port)
}