Files
Volleyball/backend/cmd/server/main.go

84 lines
1.9 KiB
Go

package main
import (
"log"
"os"
"volleyball/internal/auth"
"volleyball/internal/database"
"volleyball/internal/player"
"volleyball/internal/tournament"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
/**
* @brief Main function to start the server
*/
func main() {
var host = "localhost"
var DBport = 5432
var user = "volleyball"
db := database.New(host, DBport, user, "volleyball", "volleyball")
db.Connect()
// Setup the database and tables
if err := db.SetupTables(); err != nil {
os.Exit(1)
}
r := gin.Default()
// CORS
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:3000"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowHeaders: []string{"Authorization", "Content-Type"},
AllowCredentials: true,
}))
// Public
r.POST("/api/login", func(c *gin.Context) {
auth.LoginHandler(c, db.GetDB())
})
r.GET("/api/tournaments", tournament.ListTournaments)
// Protected API
api := r.Group("/api")
api.Use(auth.AuthMiddleware())
api.GET("/tournaments/:id", tournament.GetTournament)
api.POST("/tournaments/:id/join", tournament.JoinTournament)
api.PUT("/tournaments/:id", tournament.UpdateTournament)
api.GET("/players", func(c *gin.Context) {
player.GetPlayers(c, db.GetDB())
})
api.GET("/players/:id", func(c *gin.Context) {
player.GetPlayer(c, db.GetDB(), c.Param("id"))
})
api.POST("/players", func(c *gin.Context) {
player.CreatePlayer(c, db.GetDB())
})
api.PUT("/players/:id", func(c *gin.Context) {
log.Println("PUT /players/:id called", c.Params)
player.UpdatePlayer(c, db.GetDB())
})
api.DELETE("/players/:id", func(c *gin.Context) {
player.DeletePlayer(c, db.GetDB())
// c.JSON(http.StatusOK, gin.H{"message": "Player deleted successfully"})
})
api.GET("/teams", func(c *gin.Context) {
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
r.Run(":" + port)
}