2fas-server/internal/api/support/service/service.go
2023-01-30 19:59:42 +01:00

106 lines
3.7 KiB
Go

package service
import (
"database/sql"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/twofas/2fas-server/config"
"github.com/twofas/2fas-server/internal/api/support/adapters"
"github.com/twofas/2fas-server/internal/api/support/app"
"github.com/twofas/2fas-server/internal/api/support/app/command"
"github.com/twofas/2fas-server/internal/api/support/app/queries"
"github.com/twofas/2fas-server/internal/api/support/domain"
"github.com/twofas/2fas-server/internal/api/support/ports"
"github.com/twofas/2fas-server/internal/common/aws"
"github.com/twofas/2fas-server/internal/common/clock"
"github.com/twofas/2fas-server/internal/common/db"
httpsec "github.com/twofas/2fas-server/internal/common/http"
"github.com/twofas/2fas-server/internal/common/storage"
"gorm.io/gorm"
)
type SupportModule struct {
Cqrs *app.Cqrs
RoutesHandler *ports.RoutesHandler
Config config.Configuration
}
func NewSupportModule(config config.Configuration, gorm *gorm.DB, database *sql.DB, validate *validator.Validate) *SupportModule {
queryBuilder := db.NewQueryBuilder(database)
debugLogsConfig := domain.LoadDebugLogsConfig()
var debugLogsStorage storage.FileSystemStorage
if config.IsTestingEnv() {
debugLogsStorage = storage.NewTmpFileSystem()
} else {
debugLogsStorage = aws.NewAwsS3(debugLogsConfig.AwsRegion, debugLogsConfig.AwsAccessKeyId, debugLogsConfig.AwsSecretAccessKey)
}
debugLogsAuditRepository := adapters.NewDebugLogsAuditMysqlRepository(gorm)
cqrs := &app.Cqrs{
Commands: app.Commands{
CreateDebugLogsAudit: &command.CreateDebugLogsAuditHandler{
DebugLogsAuditRepository: debugLogsAuditRepository,
FileSystem: debugLogsStorage,
Config: debugLogsConfig,
Clock: clock.New(),
},
CreateDebugLogsAuditClain: &command.CreateDebugLogsAuditClaimHandler{
DebugLogsAuditRepository: debugLogsAuditRepository,
DebugLogsAuditConfig: debugLogsConfig,
Clock: clock.New(),
},
UpdateDebugLogsAudit: &command.UpdateDebugLogsAuditHandler{
DebugLogsAuditRepository: debugLogsAuditRepository,
},
DeleteDebugLogsAudit: &command.DeleteDebugLogsAuditHandler{
DebugLogsAuditRepository: debugLogsAuditRepository,
},
DeleteAllDebugLogsAudit: &command.DeleteAllDebugLogsAuditHandler{
Database: gorm,
Qb: queryBuilder,
},
},
Queries: app.Queries{
DebugLogsAuditQuery: &queries.DebugLogsAuditQueryHandler{
Database: gorm,
Qb: queryBuilder,
},
},
}
routesHandler := ports.NewRoutesHandler(cqrs, validate)
module := &SupportModule{
Cqrs: cqrs,
RoutesHandler: routesHandler,
Config: config,
}
return module
}
func (m *SupportModule) RegisterRoutes(router *gin.Engine) {
// internal/admin
adminRouter := router.Group("/")
adminRouter.Use(httpsec.IPWhitelistMiddleware(m.Config.Security))
adminRouter.POST("/mobile/support/debug_logs/audit/claim", m.RoutesHandler.CreateDebugLogsAuditClaim)
adminRouter.PUT("/mobile/support/debug_logs/audit/claim/:audit_id", m.RoutesHandler.UpdateDebugLogsAuditClaim)
adminRouter.DELETE("/mobile/support/debug_logs/audit/:audit_id", m.RoutesHandler.DeleteDebugLogsAudit)
adminRouter.GET("/mobile/support/debug_logs/audit/:audit_id", m.RoutesHandler.GetDebugLogsAudit)
adminRouter.GET("/mobile/support/debug_logs/audit", m.RoutesHandler.GetDebugAllLogsAudit)
if m.Config.IsTestingEnv() {
adminRouter.DELETE("/mobile/support/debug_logs/audit", m.RoutesHandler.DeleteAllDebugLogsAudit)
}
// public
publicRouter := router.Group("/")
publicRouter.POST("/mobile/support/debug_logs/audit/:audit_id", m.RoutesHandler.CreateDebugLogsAudit)
}