-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (67 loc) · 2.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gin-contrib/cors"
_ "feature-flag-service/docs"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"feature-flag-service/internal/config"
"feature-flag-service/internal/handlers"
"feature-flag-service/internal/middleware"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
// @title Feature Flag Service API
// @version 1.0
// @description API for managing feature flags
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
// @description Enter your token with "Bearer " prefix: Bearer <your_token>
// @host localhost:8080
// @BasePath /
func main() {
// Load environment variables
if err := godotenv.Load(); err != nil {
log.Println("⚠️ No .env file found, using system environment variables")
}
// Initialize database and Redis
config.Init()
// Create a new Gin router
r := gin.Default()
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"}, // Allow all origins (Change this to specific domains in production)
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
// Swagger endpoint
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
// Public routes
r.POST("/register", handlers.Register)
r.POST("/login", handlers.Login)
// Health check endpoint
r.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
api := r.Group("/api")
api.Use(middleware.AuthMiddleware())
{
api.POST("/flags", handlers.CreateFeatureFlag)
api.GET("/flags", handlers.GetFeatureFlags)
api.GET("/flags/:id", handlers.GetFeatureFlag)
api.PUT("/flags/:id", handlers.UpdateFeatureFlag)
api.DELETE("/flags/:id", handlers.DeleteFeatureFlag)
}
// Get port from environment
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
fmt.Printf("🚀 Server running on port %s\n", port)
r.Run(":" + port)
}