diff options
author | 2025-04-24 08:18:27 +0530 | |
---|---|---|
committer | 2025-04-24 08:18:27 +0530 | |
commit | 50d5e6534f5e593297a09323e683c7c8b850117b (patch) | |
tree | 339d6e8b123c5d4caa4129971e2cb1b960b12a89 /backend/internal/router/router.go | |
parent | 76066679b5bdab53419492066c4e80d2ed3be518 (diff) | |
download | finance-50d5e6534f5e593297a09323e683c7c8b850117b.tar.gz finance-50d5e6534f5e593297a09323e683c7c8b850117b.tar.bz2 finance-50d5e6534f5e593297a09323e683c7c8b850117b.zip |
feat: added basic backend features to it
- Set up API framework (Gin Gonic)
- Set up ORM/DB library (GORM)
- Design database schema (Users, Accounts, Transactions, Loans, Goals)
- Set up database connection and migrations
Diffstat (limited to 'backend/internal/router/router.go')
-rw-r--r-- | backend/internal/router/router.go | 88 |
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 +} |