aboutsummaryrefslogtreecommitdiffstats
path: root/backend/internal/router/router.go
diff options
context:
space:
mode:
Diffstat (limited to 'backend/internal/router/router.go')
-rw-r--r--backend/internal/router/router.go88
1 files changed, 88 insertions, 0 deletions
diff --git a/backend/internal/router/router.go b/backend/internal/router/router.go
new file mode 100644
index 0000000..b82e1af
--- /dev/null
+++ b/backend/internal/router/router.go
@@ -0,0 +1,88 @@
+package router
+
+import (
+ "finance/backend/internal/api/auth"
+ "finance/backend/internal/api/v1/users"
+ "finance/backend/internal/config"
+ "finance/backend/internal/database"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+)
+
+// SetupRouter configures the API routes
+func SetupRouter(cfg *config.Config) *gin.Engine {
+ r := gin.Default()
+
+ // Enable CORS
+ r.Use(func(c *gin.Context) {
+ c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
+ c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
+ c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
+ c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
+
+ if c.Request.Method == "OPTIONS" {
+ c.AbortWithStatus(204)
+ return
+ }
+
+ c.Next()
+ })
+
+ // Public utility endpoints
+ r.GET("/ping", func(c *gin.Context) {
+ c.JSON(http.StatusOK, gin.H{
+ "message": "pong",
+ })
+ })
+
+ // Add database status endpoint
+ r.GET("/db-status", func(c *gin.Context) {
+ // Try to get a connection from the pool
+ sqlDB, err := database.DB.DB()
+ if err != nil {
+ c.JSON(http.StatusInternalServerError, gin.H{
+ "status": "error",
+ "message": "Failed to get database connection",
+ "error": err.Error(),
+ })
+ return
+ }
+
+ // Check if database is reachable
+ err = sqlDB.Ping()
+ if err != nil {
+ c.JSON(http.StatusInternalServerError, gin.H{
+ "status": "error",
+ "message": "Database is not reachable",
+ "error": err.Error(),
+ })
+ return
+ }
+
+ c.JSON(http.StatusOK, gin.H{
+ "status": "success",
+ "message": "Database connection is healthy",
+ })
+ })
+
+ // API v1 routes
+ v1 := r.Group("/api/v1")
+ {
+ // Auth routes (public)
+ v1.POST("/auth/signup", auth.Signup(cfg))
+ v1.POST("/auth/login", auth.Login(cfg))
+
+ // Protected routes
+ protected := v1.Group("")
+ protected.Use(auth.AuthMiddleware(cfg))
+ {
+ // User routes
+ protected.GET("/users/me", users.GetCurrentUser)
+
+ // Add other protected routes here
+ }
+ }
+
+ return r
+}