/*
* This file is part of laurelin_api
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
package main
import (
"api/db"
"api/handlers"
"api/middlewares"
"log"
"os"
"github.com/gin-gonic/gin"
)
func main() {
// stores various errors during startup
var err error
dbConnectionString := os.Getenv("GORM_DB_STRING")
if dbConnectionString == "" {
log.Fatal("environment variable 'GORM_DB_STRING' is not set")
}
err = db.Connect(dbConnectionString)
if err != nil {
log.Fatal(err)
}
db.Migrate()
log.Print("creating router and launching API")
router := createRouter()
router.Run(":3000")
}
func createRouter() *gin.Engine {
router := gin.Default()
api := router.Group("/api")
{
api.GET("/info", handlers.Info)
user := api.Group("/user")
{
user.POST("/", handlers.CreateUser)
user.POST("/token", handlers.GenerateToken)
user.GET("/:id", handlers.UserInfo)
userp := user.Group("/_").Use(middlewares.Auth())
{
userp.GET("/:id", handlers.UserInfoP)
}
}
game := api.Group("/game").Use(middlewares.Auth())
{
game.GET("/:id", handlers.GameInfo)
game.GET("/all_forming", handlers.FormingGames)
game.POST("/", handlers.CreateGame)
game.PATCH("/:id/state", handlers.PatchGameState)
game.GET("/my_games", handlers.MyGames)
game.POST("/:id/join", handlers.JoinGame)
}
action := api.Group("/game/action").Use(middlewares.Auth())
{
action.POST("/", handlers.CreateAction)
}
}
return router
}