76 lines
2.4 KiB
Go
76 lines
2.4 KiB
Go
package server
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
"github.com/gofiber/fiber/v2/middleware/helmet"
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
|
|
|
"github.com/techxcar/backend/internal/auth"
|
|
"github.com/techxcar/backend/internal/catalog"
|
|
"github.com/techxcar/backend/internal/client"
|
|
"github.com/techxcar/backend/internal/config"
|
|
"github.com/techxcar/backend/internal/expense"
|
|
"github.com/techxcar/backend/internal/invoice"
|
|
"github.com/techxcar/backend/internal/platformsettings"
|
|
"github.com/techxcar/backend/internal/settings"
|
|
"github.com/techxcar/backend/internal/staff"
|
|
"github.com/techxcar/backend/internal/tenant"
|
|
"github.com/techxcar/backend/internal/workorder"
|
|
"github.com/techxcar/backend/pkg/database"
|
|
redispkg "github.com/techxcar/backend/pkg/redis"
|
|
)
|
|
|
|
type Deps struct {
|
|
Config *config.Config
|
|
DB *database.DB
|
|
Redis *redispkg.Redis
|
|
}
|
|
|
|
func New(deps Deps) *fiber.App {
|
|
app := fiber.New(fiber.Config{
|
|
AppName: "TechXCar API",
|
|
ErrorHandler: errorHandler,
|
|
})
|
|
|
|
app.Use(recover.New())
|
|
app.Use(logger.New())
|
|
app.Use(helmet.New())
|
|
app.Use(cors.New(cors.Config{
|
|
AllowOrigins: "http://localhost:3000,http://localhost:5173",
|
|
AllowHeaders: "Origin, Content-Type, Accept, Authorization",
|
|
AllowMethods: "GET, POST, PUT, PATCH, DELETE, OPTIONS",
|
|
AllowCredentials: true,
|
|
}))
|
|
|
|
RegisterHealthRoutes(app)
|
|
|
|
if deps.DB != nil && deps.Redis != nil && deps.Config != nil {
|
|
repo := tenant.NewRepository(deps.DB)
|
|
auth.RegisterRoutes(app, tenant.LoginAdapter(repo), deps.Redis, deps.Config)
|
|
tenant.RegisterRoutes(app, repo, deps.DB, deps.Config)
|
|
client.RegisterRoutes(app, deps.DB, deps.Config.JWTSecret)
|
|
catalog.RegisterRoutes(app, deps.DB, deps.Config.JWTSecret)
|
|
workorder.RegisterRoutes(app, deps.DB, deps.Config.JWTSecret)
|
|
staff.RegisterRoutes(app, deps.DB, deps.Config.JWTSecret)
|
|
expense.RegisterRoutes(app, deps.DB, deps.Config.JWTSecret)
|
|
settings.RegisterRoutes(app, deps.DB, deps.Config.JWTSecret)
|
|
platformsettings.RegisterRoutes(app, deps.DB, deps.Config.JWTSecret)
|
|
invoice.RegisterRoutes(app, deps.DB, deps.Config.JWTSecret)
|
|
}
|
|
|
|
return app
|
|
}
|
|
|
|
func errorHandler(c *fiber.Ctx, err error) error {
|
|
code := fiber.StatusInternalServerError
|
|
if e, ok := err.(*fiber.Error); ok {
|
|
code = e.Code
|
|
}
|
|
return c.Status(code).JSON(fiber.Map{
|
|
"data": nil,
|
|
"error": err.Error(),
|
|
})
|
|
}
|