67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"time"
|
|
|
|
"giter.top/smart/internal/auth"
|
|
"giter.top/smart/internal/iam"
|
|
"giter.top/smart/internal/system"
|
|
"giter.top/smart/pkg/config"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type HttpServer struct {
|
|
addr string
|
|
timeout time.Duration
|
|
engine *gin.Engine
|
|
}
|
|
|
|
func NewHttpServer(cfg *config.Config,
|
|
engine *gin.Engine,
|
|
) *HttpServer {
|
|
return &HttpServer{
|
|
addr: cfg.Server.Http.Addr,
|
|
timeout: cfg.Server.Http.Timeout,
|
|
engine: engine,
|
|
}
|
|
}
|
|
func (s *HttpServer) Run() error {
|
|
s.engine.Run(s.addr)
|
|
return nil
|
|
}
|
|
|
|
func (s *HttpServer) Stop() error {
|
|
return nil
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////
|
|
type HttpRoutes interface {
|
|
Register(engine *gin.Engine , apiGroup *gin.RouterGroup)
|
|
}
|
|
|
|
func NewHttpEngine(cfg *config.Config,httpRoutes []HttpRoutes) *gin.Engine {
|
|
engine := gin.Default()
|
|
engine.Use(corsLocalDev())
|
|
// 健康检查端点,供负载均衡或编排探活。
|
|
engine.GET("/health", func(c *gin.Context) {
|
|
c.JSON(200, gin.H{"status": "ok"})
|
|
})
|
|
// 处理注册的路由
|
|
apiGroup := engine.Group("/api/v1")
|
|
for _, r := range httpRoutes {
|
|
r.Register(engine, apiGroup)
|
|
}
|
|
return engine
|
|
}
|
|
|
|
func NewHttpRouteRegistrars(
|
|
authRoutes *auth.AuthRoutes,
|
|
systemRoutes *system.SystemRoutes,
|
|
iamRoutes *iam.IamRoutes,
|
|
) []HttpRoutes {
|
|
return []HttpRoutes{
|
|
authRoutes,
|
|
systemRoutes,
|
|
iamRoutes,
|
|
}
|
|
} |