This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
// "kontor-api-gin/pkg/handler"
|
||||
"kontor-api-gin/pkg/routes"
|
||||
"kontor-api-gin/pkg/schema"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Println("Kontor started")
|
||||
|
||||
// Set Gin mode based on environment
|
||||
// Production mode disables debug output
|
||||
if os.Getenv("GIN_MODE") == "release" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
|
||||
// Connect to database
|
||||
schema.Connect()
|
||||
|
||||
// Run migrations
|
||||
schema.RunMigrations()
|
||||
|
||||
// Create Gin router with default middleware
|
||||
// Default includes Logger and Recovery middleware
|
||||
router := gin.Default()
|
||||
|
||||
// Add CORS middleware for frontend access
|
||||
router.Use(corsMiddleware())
|
||||
|
||||
// Setup application routes
|
||||
api := routes.SetupApiRoutes(router)
|
||||
routes.SetupMediaRoutes(api)
|
||||
|
||||
// Health check endpoint
|
||||
router.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"status": "healthy",
|
||||
})
|
||||
})
|
||||
|
||||
// Start server
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8550"
|
||||
}
|
||||
|
||||
log.Printf("Server starting on port %s", port)
|
||||
if err := router.Run(":" + port); err != nil {
|
||||
log.Fatal("Failed to start server:", err)
|
||||
}
|
||||
|
||||
log.Println("Kontor finished")
|
||||
}
|
||||
|
||||
// corsMiddleware adds CORS headers for cross-origin requests
|
||||
func corsMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(204)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user