Files
online-backend/internal/handlers/manager/manager.go
GuwandykA 28509eb4ca
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled
Add backups, currencies and dashboard APIs, and scope access by guard_name.
Web users may only CRUD their own records, operators stay in assigned branches, and admin/manager/superadmin keep full access.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-31 18:14:47 +05:00

85 lines
2.4 KiB
Go
Executable File

package handlermanager
import (
"net/http"
"path/filepath"
"online-back/internal/backup"
backupdb "online-back/internal/backup/db"
"online-back/internal/cards"
"online-back/internal/config"
"online-back/internal/currency"
currencydb "online-back/internal/currency/db"
"online-back/internal/dashboard"
dashboarddb "online-back/internal/dashboard/db"
"online-back/internal/identity"
"online-back/internal/identity/activitylog"
activitylogdb "online-back/internal/identity/activitylog/db"
"online-back/internal/identity/rbac"
"online-back/internal/lending"
"online-back/internal/org"
"online-back/pkg/logging"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4/pgxpool"
)
const adminAPIPrefix = "/api/admin"
func New(cfg *config.Config, client *pgxpool.Pool, logger *logging.Logger) *gin.Engine {
if cfg.IsDebug != nil && !*cfg.IsDebug {
gin.SetMode(gin.ReleaseMode)
}
router := gin.New()
// Trust only loopback and private networks (Docker/K8s/nginx), not all proxies.
_ = router.SetTrustedProxies([]string{
"127.0.0.1",
"::1",
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
})
router.Use(gin.Recovery(), gin.Logger())
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodDelete,
},
AllowHeaders: []string{
"Content-Type",
"Authorization",
"Cache-Control",
},
ExposeHeaders: []string{"Content-Length", "Content-Disposition"},
}))
adminGroup := router.Group(adminAPIPrefix)
adminGroup.Use(activitylog.Record(activitylogdb.NewRepository(client, logger), logger))
adminGroup.Use(rbac.Guard(rbac.NewService(client)))
// Each bounded context registers its own modules' handlers.
// Add new modules inside the relevant context's router.go rather than here.
identity.Register(adminGroup, router, client, logger)
org.Register(adminGroup, client, logger)
lending.Register(adminGroup, client, logger)
cards.Register(adminGroup, client, logger)
currency.NewHandler(logger, currencydb.NewRepository(client, logger)).Register(adminGroup)
dashboard.NewHandler(logger, dashboarddb.NewRepository(client, logger)).Register(adminGroup)
backupDir := filepath.Join(cfg.PublicFilePath, "backups")
backupSvc := backup.NewService(
backupdb.NewRepository(client, logger),
backup.NewPgDumper(cfg.Storage),
backupDir,
logger,
)
backup.NewHandler(logger, backupSvc).Register(adminGroup)
return router
}