Yimaru-BackEnd/internal/web_server/app.go
2025-04-23 03:44:17 +03:00

104 lines
3.4 KiB
Go

package httpserver
import (
"fmt"
"log/slog"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/authentication"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/bet"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/branch"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/company"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/event"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/odds"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/ticket"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/transaction"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/user"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/wallet"
jwtutil "github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/jwt"
customvalidator "github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/validator"
notificationservice "github.com/SamuelTariku/FortuneBet-Backend/internal/services/notfication"
"github.com/bytedance/sonic"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
)
type App struct {
fiber *fiber.App
logger *slog.Logger
NotidicationStore notificationservice.NotificationStore
port int
authSvc *authentication.Service
userSvc *user.Service
ticketSvc *ticket.Service
betSvc *bet.Service
walletSvc *wallet.Service
transactionSvc *transaction.Service
branchSvc *branch.Service
companySvc *company.Service
validator *customvalidator.CustomValidator
JwtConfig jwtutil.JwtConfig
Logger *slog.Logger
prematchSvc *odds.ServiceImpl
eventSvc event.Service
}
func NewApp(
port int, validator *customvalidator.CustomValidator,
authSvc *authentication.Service,
logger *slog.Logger,
JwtConfig jwtutil.JwtConfig,
userSvc *user.Service,
ticketSvc *ticket.Service,
betSvc *bet.Service,
walletSvc *wallet.Service,
transactionSvc *transaction.Service,
branchSvc *branch.Service,
companySvc *company.Service,
notidicationStore notificationservice.NotificationStore,
prematchSvc *odds.ServiceImpl,
eventSvc event.Service,
) *App {
app := fiber.New(fiber.Config{
CaseSensitive: true,
DisableHeaderNormalizing: true,
JSONEncoder: sonic.Marshal,
JSONDecoder: sonic.Unmarshal,
})
app.Use(cors.New(cors.Config{
AllowOrigins: "*", // Specify your frontend's origin
AllowMethods: "GET,POST,PUT,DELETE,OPTIONS", // Specify the allowed HTTP methods
AllowHeaders: "Content-Type,Authorization,platform", // Specify the allowed headers
// AllowCredentials: true,
}))
s := &App{
fiber: app,
port: port,
authSvc: authSvc,
validator: validator,
logger: logger,
JwtConfig: JwtConfig,
userSvc: userSvc,
ticketSvc: ticketSvc,
betSvc: betSvc,
walletSvc: walletSvc,
transactionSvc: transactionSvc,
branchSvc: branchSvc,
companySvc: companySvc,
NotidicationStore: notidicationStore,
Logger: logger,
prematchSvc: prematchSvc,
eventSvc: eventSvc,
}
s.initAppRoutes()
return s
}
func (a *App) Run() error {
return a.fiber.Listen(fmt.Sprintf(":%d", a.port))
}