photum/backend/cmd/api/main.go

167 lines
5.3 KiB
Go

package main
import (
"log"
"photum-backend/docs"
"photum-backend/internal/anos_formaturas"
"photum-backend/internal/auth"
"photum-backend/internal/config"
"photum-backend/internal/cursos"
"photum-backend/internal/db"
"photum-backend/internal/empresas"
"photum-backend/internal/funcoes"
"photum-backend/internal/profissionais"
"photum-backend/internal/tipos_eventos"
"photum-backend/internal/tipos_servicos"
"strings"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
_ "photum-backend/docs" // Import generated docs
)
// @title Photum Backend API
// @version 1.0
// @description Backend authentication service for Photum.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
func main() {
cfg := config.LoadConfig()
log.Printf("Loaded DSN: %s", cfg.DBDsn)
queries, pool := db.Connect(cfg)
defer pool.Close()
// Run Migrations
db.Migrate(pool)
// Initialize services
profissionaisService := profissionais.NewService(queries)
authService := auth.NewService(queries, profissionaisService, cfg)
funcoesService := funcoes.NewService(queries)
cursosService := cursos.NewService(queries)
empresasService := empresas.NewService(queries)
anosFormaturasService := anos_formaturas.NewService(queries)
tiposServicosService := tipos_servicos.NewService(queries)
tiposEventosService := tipos_eventos.NewService(queries)
// Initialize handlers
authHandler := auth.NewHandler(authService)
profissionaisHandler := profissionais.NewHandler(profissionaisService)
funcoesHandler := funcoes.NewHandler(funcoesService)
cursosHandler := cursos.NewHandler(cursosService)
empresasHandler := empresas.NewHandler(empresasService)
anosFormaturasHandler := anos_formaturas.NewHandler(anosFormaturasService)
tiposServicosHandler := tipos_servicos.NewHandler(tiposServicosService)
tiposEventosHandler := tipos_eventos.NewHandler(tiposEventosService)
r := gin.Default()
// CORS Middleware
configCors := cors.DefaultConfig()
if cfg.CorsAllowedOrigins == "*" {
configCors.AllowAllOrigins = true
} else {
configCors.AllowOrigins = strings.Split(cfg.CorsAllowedOrigins, ",")
}
configCors.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"}
r.Use(cors.New(configCors))
// Swagger
// Dynamically update Swagger Info
docs.SwaggerInfo.Host = cfg.SwaggerHost
docs.SwaggerInfo.Schemes = []string{"https", "http"}
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
// Public Routes
authGroup := r.Group("/auth")
{
authGroup.POST("/register", authHandler.Register)
authGroup.POST("/login", authHandler.Login)
authGroup.POST("/refresh", authHandler.Refresh)
authGroup.POST("/logout", authHandler.Logout)
}
// Public API Routes (Data Lists)
r.GET("/api/funcoes", funcoesHandler.List)
r.GET("/api/cursos", cursosHandler.List)
r.GET("/api/empresas", empresasHandler.List)
r.GET("/api/anos-formaturas", anosFormaturasHandler.List)
r.GET("/api/tipos-servicos", tiposServicosHandler.List)
r.GET("/api/tipos-eventos", tiposEventosHandler.List)
r.GET("/api/tipos-eventos/:id/precos", tiposEventosHandler.ListPrices)
// Protected Routes
api := r.Group("/api")
api.Use(auth.AuthMiddleware(cfg))
{
api.GET("/me", func(c *gin.Context) {
userID, _ := c.Get("userID")
role, _ := c.Get("role")
c.JSON(200, gin.H{
"user_id": userID,
"role": role,
"message": "You are authenticated",
})
})
profGroup := api.Group("/profissionais")
{
profGroup.POST("", profissionaisHandler.Create)
profGroup.GET("", profissionaisHandler.List)
profGroup.GET("/:id", profissionaisHandler.Get)
profGroup.PUT("/:id", profissionaisHandler.Update)
profGroup.DELETE("/:id", profissionaisHandler.Delete)
}
funcoesGroup := api.Group("/funcoes")
{
funcoesGroup.POST("", funcoesHandler.Create)
funcoesGroup.PUT("/:id", funcoesHandler.Update)
funcoesGroup.DELETE("/:id", funcoesHandler.Delete)
}
// protected CRUD (create/update/delete)
api.POST("/cursos", cursosHandler.Create)
api.PUT("/cursos/:id", cursosHandler.Update)
api.DELETE("/cursos/:id", cursosHandler.Delete)
api.POST("/empresas", empresasHandler.Create)
api.PUT("/empresas/:id", empresasHandler.Update)
api.DELETE("/empresas/:id", empresasHandler.Delete)
api.POST("/anos-formaturas", anosFormaturasHandler.Create)
api.PUT("/anos-formaturas/:id", anosFormaturasHandler.Update)
api.DELETE("/anos-formaturas/:id", anosFormaturasHandler.Delete)
api.POST("/tipos-servicos", tiposServicosHandler.Create)
api.PUT("/tipos-servicos/:id", tiposServicosHandler.Update)
api.DELETE("/tipos-servicos/:id", tiposServicosHandler.Delete)
api.POST("/tipos-eventos", tiposEventosHandler.Create)
api.POST("/tipos-eventos/precos", tiposEventosHandler.SetPrice)
}
log.Printf("Swagger Host Configured: %s", cfg.SwaggerHost)
log.Printf("Server running on port %s", cfg.AppPort)
r.Run(":" + cfg.AppPort)
}