87 lines
2.6 KiB
Go
87 lines
2.6 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/sav"
|
|
"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
|
|
ContactH *sav.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)
|
|
api.POST("/send/message", httpx.RateLimit(1, 3), d.ContactH.CallSupport)
|
|
// 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)
|
|
}
|
|
|
|
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)
|
|
admin.GET("/messages", d.ContactH.GetMessage)
|
|
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)
|
|
}
|
|
}
|
|
|
|
return r
|
|
}
|