79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"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) {
|
|
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"})
|
|
})
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8080"
|
|
}
|
|
r.Run(":" + port)
|
|
}
|