Inicial
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package server
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
|
||||
const appVersion = "0.1.0"
|
||||
|
||||
func RegisterHealthRoutes(app *fiber.App) {
|
||||
app.Get("/api/v1/health", handleHealth)
|
||||
}
|
||||
|
||||
func handleHealth(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{
|
||||
"status": "ok",
|
||||
"version": appVersion,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package server_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/techxcar/backend/internal/server"
|
||||
)
|
||||
|
||||
func TestHealthEndpoint_returnsOK(t *testing.T) {
|
||||
app := fiber.New()
|
||||
server.RegisterHealthRoutes(app)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/v1/health", nil)
|
||||
resp, err := app.Test(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, 200, resp.StatusCode)
|
||||
|
||||
var body map[string]any
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&body))
|
||||
assert.Equal(t, "ok", body["status"])
|
||||
assert.NotEmpty(t, body["version"])
|
||||
}
|
||||
|
||||
func TestHealthEndpoint_wrongMethod(t *testing.T) {
|
||||
app := fiber.New()
|
||||
server.RegisterHealthRoutes(app)
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/v1/health", nil)
|
||||
resp, err := app.Test(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, 405, resp.StatusCode)
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
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/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)
|
||||
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(),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user