Files
online-backend/internal/handlers/manager/manager.go
GuwandykA 6c7c402390
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 pay
2026-09-19 03:54:40 +05:00

99 lines
3.1 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/internal/payment"
"online-back/pkg/logging"
"online-back/pkg/paymentgateway"
"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)
currencyRepo := currencydb.NewRepository(client, logger)
payment.Register(adminGroup, client, logger, currencyRepo, paymentGatewayClient(cfg, logger), cfg.PublicBaseURL)
currency.NewHandler(logger, currencyRepo).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
}
// paymentGatewayClient picks the real GBÜS client outside debug mode, and
// the logging stub in debug mode, so local/dev runs never hit the live
// bank gateway by accident.
func paymentGatewayClient(cfg *config.Config, logger *logging.Logger) paymentgateway.Client {
if cfg.IsDebug != nil && *cfg.IsDebug {
return paymentgateway.NewLogClient(logger)
}
return paymentgateway.NewHTTPClient(logger)
}