refactor: [shitty claude AI first try] restructure server and user services, add new test cases, and improve error handling

This commit is contained in:
2025-08-10 21:40:15 +03:00
parent 588576b82f
commit f503e45be1
23 changed files with 2568 additions and 134 deletions

12
internal/server/fx.go Normal file
View File

@ -0,0 +1,12 @@
package server
import "go.uber.org/fx"
// Module provides the server dependencies and lifecycle management
var Module = fx.Options(
fx.Provide(
NewFiberApp,
NewAPIRouter,
),
fx.Invoke(StartServer),
)

75
internal/server/server.go Normal file
View File

@ -0,0 +1,75 @@
package server
import (
"context"
"strconv"
"git.logidex.ru/fakz9/logidex-id/internal/config"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
fiberlogger "github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"go.uber.org/fx"
)
// NewFiberApp creates a new Fiber application with common middleware
func NewFiberApp(cfg config.Config) *fiber.App {
app := fiber.New(fiber.Config{
AppName: "Logidex ID API",
ServerHeader: "Logidex ID",
ErrorHandler: func(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{
"error": true,
"message": err.Error(),
})
},
})
// Add middleware
app.Use(recover.New())
app.Use(fiberlogger.New(fiberlogger.Config{
Format: "[${time}] ${status} - ${latency} ${method} ${path}\n",
}))
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowMethods: "GET,POST,HEAD,PUT,DELETE,PATCH,OPTIONS",
AllowHeaders: "Origin,Content-Type,Accept,Authorization",
}))
// Health check endpoint
app.Get("/health", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"status": "ok",
"service": "logidex-id-api",
})
})
return app
}
// NewAPIRouter creates the main API router group
func NewAPIRouter(app *fiber.App) fiber.Router {
return app.Group("/api")
}
// StartServer handles the server lifecycle
func StartServer(lifecycle fx.Lifecycle, app *fiber.App, cfg config.Config) {
lifecycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
go func() {
addr := ":" + strconv.Itoa(cfg.App.Port)
if err := app.Listen(addr); err != nil {
}
}()
return nil
},
OnStop: func(ctx context.Context) error {
return app.Shutdown()
},
})
}

View File

@ -0,0 +1,48 @@
package server
import (
"net/http/httptest"
"testing"
"git.logidex.ru/fakz9/logidex-id/internal/config"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
)
func TestNewFiberApp(t *testing.T) {
cfg := config.Config{}
cfg.App.Port = 8080
app := NewFiberApp(cfg)
assert.NotNil(t, app)
// Test health check endpoint
req := httptest.NewRequest("GET", "/health", nil)
resp, err := app.Test(req)
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
}
func TestNewAPIRouter(t *testing.T) {
cfg := config.Config{}
cfg.App.Port = 8080
app := NewFiberApp(cfg)
router := NewAPIRouter(app)
assert.NotNil(t, router)
// Test that we can add routes to the router
router.Get("/test", func(c *fiber.Ctx) error {
return c.JSON(map[string]string{"message": "test"})
})
// Test the route works
req := httptest.NewRequest("GET", "/api/test", nil)
resp, err := app.Test(req)
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
}