add env files
This commit is contained in:
52
main.go
52
main.go
@@ -2,24 +2,23 @@ package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Load environment variables
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Println("No .env file found, using system environment variables")
|
||||
}
|
||||
// Load configuration with environment file precedence
|
||||
config := LoadConfig()
|
||||
|
||||
// Initialize JWT
|
||||
InitJWT()
|
||||
// Set Gin mode based on configuration
|
||||
gin.SetMode(config.GinMode)
|
||||
|
||||
// Initialize database
|
||||
if err := InitDB(); err != nil {
|
||||
// Initialize JWT with configuration
|
||||
InitJWTWithConfig(config)
|
||||
|
||||
// Initialize database with configuration
|
||||
if err := InitDBWithConfig(config); err != nil {
|
||||
log.Fatal("Failed to initialize database:", err)
|
||||
}
|
||||
|
||||
@@ -28,21 +27,16 @@ func main() {
|
||||
log.Fatal("Failed to create tables:", err)
|
||||
}
|
||||
|
||||
// Set Gin mode
|
||||
if os.Getenv("GIN_MODE") == "release" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
|
||||
// Create Gin router
|
||||
r := gin.Default()
|
||||
|
||||
// Configure CORS
|
||||
config := cors.DefaultConfig()
|
||||
config.AllowOrigins = []string{"http://localhost:3000", "http://localhost:5173"} // React dev servers
|
||||
config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}
|
||||
config.AllowHeaders = []string{"Origin", "Content-Type", "Accept", "Authorization"}
|
||||
config.AllowCredentials = true
|
||||
r.Use(cors.New(config))
|
||||
corsConfig := cors.DefaultConfig()
|
||||
corsConfig.AllowOrigins = []string{"http://localhost:3000", "http://localhost:5173"} // React dev servers
|
||||
corsConfig.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}
|
||||
corsConfig.AllowHeaders = []string{"Origin", "Content-Type", "Accept", "Authorization"}
|
||||
corsConfig.AllowCredentials = true
|
||||
r.Use(cors.New(corsConfig))
|
||||
|
||||
// Health check endpoint
|
||||
r.GET("/health", func(c *gin.Context) {
|
||||
@@ -83,11 +77,17 @@ func main() {
|
||||
})
|
||||
|
||||
// Start server
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
port := config.Port
|
||||
|
||||
log.Printf("")
|
||||
log.Printf("🚀 Starting Counter Application Server...")
|
||||
log.Printf(" 🌐 Listening on: http://localhost:%s", port)
|
||||
log.Printf(" 📊 Health check: http://localhost:%s/health", port)
|
||||
log.Printf(" 🔗 API endpoint: http://localhost:%s/api/v1", port)
|
||||
log.Printf(" 🎨 Frontend: http://localhost:%s/", port)
|
||||
log.Printf("")
|
||||
log.Printf("✅ Server is ready and accepting connections!")
|
||||
log.Printf("")
|
||||
|
||||
log.Printf("Server starting on port %s", port)
|
||||
log.Fatal(r.Run(":" + port))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user