package middlewares import ( "git.secnex.io/secnex/idp-api/db" "github.com/gofiber/fiber/v2" "gorm.io/gorm" ) // DatabaseMiddleware injects the database connection into the fiber context func DatabaseMiddleware() fiber.Handler { return func(c *fiber.Ctx) error { // Get the global database instance database := db.GetDB() // Store the database connection in the context c.Locals("db", database) return c.Next() } } // GetDBFromContext retrieves the database connection from the fiber context func GetDBFromContext(c *fiber.Ctx) *gorm.DB { if db, ok := c.Locals("db").(*gorm.DB); ok { return db } // Fallback to global instance if not found in context return db.GetDB() }