feat: 优化web
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// corsLocalDev 允许本机前端(localhost / 127.0.0.1 任意端口)跨域访问 API 与 OAuth;生产同域部署时可关闭或改为配置白名单。
|
||||
func corsLocalDev() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
o := c.GetHeader("Origin")
|
||||
if o != "" && isLocalDevOrigin(o) {
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", o)
|
||||
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Tenant-ID, X-User-ID, X-Grantor-User-ID")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
||||
}
|
||||
if c.Request.Method == http.MethodOptions {
|
||||
c.AbortWithStatus(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func isLocalDevOrigin(o string) bool {
|
||||
return strings.HasPrefix(o, "http://localhost:") ||
|
||||
strings.HasPrefix(o, "http://127.0.0.1:") ||
|
||||
strings.HasPrefix(o, "http://[::1]:")
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"giter.top/smart/pkg/config"
|
||||
)
|
||||
|
||||
type GrpcServer struct {
|
||||
addr string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func NewGrpcServer(cfg *config.Config) *GrpcServer {
|
||||
return &GrpcServer{
|
||||
addr: cfg.Server.Grpc.Addr,
|
||||
timeout: cfg.Server.Grpc.Timeout,
|
||||
}
|
||||
}
|
||||
func (s *GrpcServer) Run() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *GrpcServer) Stop() error {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"giter.top/smart/pkg/config"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
var ProviderSet = wire.NewSet(
|
||||
NewHttpEngine,
|
||||
ProvideServers,
|
||||
NewHttpRouteRegistrars,
|
||||
)
|
||||
|
||||
type Server interface {
|
||||
Run() error
|
||||
Stop() error
|
||||
}
|
||||
|
||||
func ProvideServers(cfg *config.Config, engine *gin.Engine) []Server {
|
||||
return []Server{
|
||||
NewHttpServer(cfg, engine),
|
||||
NewGrpcServer(cfg),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user