Files
omnex/control-plane/api/internal/router/router.go
T
Nuxgrid 5d2a132c3c
ci-api / test (push) Successful in 23m32s
ci-web / test (push) Successful in 13m52s
fix: multiple error
2026-08-01 17:24:34 +02:00

88 lines
2.7 KiB
Go

// Package router assemble les routes de l'API Omnex.
package router
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/omnex/control-plane/api/internal/auth"
"github.com/omnex/control-plane/api/internal/config"
"github.com/omnex/control-plane/api/internal/demos"
"github.com/omnex/control-plane/api/internal/httpx"
"github.com/omnex/control-plane/api/internal/leads"
"github.com/omnex/control-plane/api/internal/profile"
"github.com/omnex/control-plane/api/internal/session"
"github.com/omnex/control-plane/api/internal/sub"
)
// Deps : dépendances injectées (facilite les tests).
type Deps struct {
Cfg config.Config
Issuer *auth.Issuer
Sessions session.Manager
AuthH *auth.Handler
LeadsH *leads.Handler
DemosH *demos.Handler
SubH *sub.Handler
ProfileH *profile.Handler
}
// New construit l'engine Gin avec toute la chaîne de sécurité.
func New(d Deps) *gin.Engine {
if d.Cfg.Env == "prod" {
gin.SetMode(gin.ReleaseMode)
}
r := gin.New()
r.Use(gin.Recovery())
r.Use(httpx.SecurityHeaders())
r.Use(httpx.CORS(d.Cfg.AllowedOrigins))
r.GET("/healthz", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": "ok"}) })
api := r.Group("/api/v1")
// Public : login, inscription + formulaire de contact, rate-limités.
api.POST("/auth/login", httpx.RateLimit(1, 5), d.AuthH.Login)
api.POST("/auth/register", httpx.RateLimit(0.2, 3), d.AuthH.Register)
api.POST("/leads", httpx.RateLimit(1, 3), d.LeadsH.Create)
// Toute route authentifiée (session valide, n'importe quel rôle).
authed := api.Group("")
authed.Use(auth.RequireAuth(d.Issuer, d.Sessions))
{
authed.POST("/auth/logout", d.AuthH.Logout)
authed.GET("/auth/me", d.AuthH.Me)
authed.POST("/profile/username", d.ProfileH.UpdateUsernameById)
authed.POST("/profile/password", d.ProfileH.UpdatePasswordById)
authed.GET("/profile/telegram", d.ProfileH.GetTelegramById)
authed.POST("/profile/telegram", d.ProfileH.SetTelegramById)
}
client := authed.Group("")
client.Use(auth.RequireRole(auth.RoleClient))
{
client.GET("/leads", d.LeadsH.List)
client.PATCH("/leads/:id/status", d.LeadsH.SetStatus)
client.POST("/subscription", d.SubH.AddCode)
if d.DemosH != nil {
client.GET("/demos/mine", d.DemosH.ListMine)
}
}
// Espace admin : provisioning des démos (admin uniquement).
admin := authed.Group("")
admin.Use(auth.RequireRole(auth.RoleAdmin))
{
admin.GET("/codes", d.SubH.ListCodes)
admin.POST("/codes", d.SubH.CreateCodeForBuy)
if d.DemosH != nil {
admin.POST("/demos", d.DemosH.Create)
admin.GET("/demos", d.DemosH.List)
admin.GET("/demos/:id", d.DemosH.Get)
admin.DELETE("/demos/:id", d.DemosH.Delete)
admin.POST("/demos/:id/extend", d.DemosH.Extend)
admin.POST("/demos/details", d.DemosH.ListDetails)
}
}
return r
}