Skip to content

Commit

Permalink
修复中间件
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Mar 15, 2024
1 parent 66700a6 commit 67b8f56
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 15 deletions.
7 changes: 6 additions & 1 deletion examples/http_gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/server"

// 引入Gin Root Router: *gin.Engine
ioc_gin "github.com/infraboard/mcube/v2/ioc/config/gin"

// 引入生成好的API Doc代码
_ "github.com/infraboard/mcube/v2/examples/http_gin/docs"
// 引入集成工程
Expand Down Expand Up @@ -67,8 +70,10 @@ func (h *HelloServiceApiHandler) Version() string {
}

// API路由
func (h *HelloServiceApiHandler) Registry(r gin.IRouter) {
func (h *HelloServiceApiHandler) Init() error {
r := ioc_gin.ObjectRouter(h)
r.GET("/", h.Hello)
return nil
}

// @Summary 修改文章标签
Expand Down
7 changes: 5 additions & 2 deletions examples/http_go_restful/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (

"github.com/emicklei/go-restful/v3"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/config/gorestful"
"github.com/infraboard/mcube/v2/ioc/server"

_ "github.com/infraboard/mcube/v2/ioc/apps/apidoc/restful"
_ "github.com/infraboard/mcube/v2/ioc/apps/health/restful"
_ "github.com/infraboard/mcube/v2/ioc/apps/metric/restful"
_ "github.com/infraboard/mcube/v2/ioc/config/cors"
_ "github.com/infraboard/mcube/v2/ioc/config/cors/gorestful"
)

func main() {
Expand Down Expand Up @@ -40,10 +41,12 @@ func (h *HelloServiceApiHandler) Version() string {
}

// API路由
func (h *HelloServiceApiHandler) Registry(ws *restful.WebService) {
func (h *HelloServiceApiHandler) Init() error {
ws := gorestful.ObjectRouter(h)
ws.Route(ws.GET("/").To(func(r *restful.Request, w *restful.Response) {
w.WriteAsJson(map[string]string{
"data": "hello mcube",
})
}))
return nil
}
5 changes: 4 additions & 1 deletion ioc/apps/apidoc/restful/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/emicklei/go-restful/v3"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/apps/apidoc"
"github.com/infraboard/mcube/v2/ioc/config/gorestful"
"github.com/infraboard/mcube/v2/ioc/config/http"
"github.com/infraboard/mcube/v2/ioc/config/log"
"github.com/rs/zerolog"
Expand All @@ -29,6 +30,7 @@ func (h *SwaggerApiDoc) Name() string {

func (h *SwaggerApiDoc) Init() error {
h.log = log.Sub("api_doc")
h.Registry()
return nil
}

Expand All @@ -42,7 +44,8 @@ func (h *SwaggerApiDoc) Meta() ioc.ObjectMeta {
return meta
}

func (h *SwaggerApiDoc) Registry(ws *restful.WebService) {
func (h *SwaggerApiDoc) Registry() {
ws := gorestful.ObjectRouter(h)
ws.Produces(restful.MIME_JSON)
swagger := restfulspec.BuildSwagger(h.SwaggerDocConfig())
ws.Route(ws.GET("/").To(func(r *restful.Request, w *restful.Response) {
Expand Down
6 changes: 5 additions & 1 deletion ioc/apps/health/restful/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/infraboard/mcube/v2/http/restful/response"
"github.com/infraboard/mcube/v2/ioc"
ioc_health "github.com/infraboard/mcube/v2/ioc/apps/health"
"github.com/infraboard/mcube/v2/ioc/config/gorestful"
"github.com/infraboard/mcube/v2/ioc/config/http"
"github.com/infraboard/mcube/v2/ioc/config/log"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -39,6 +40,7 @@ func (h *HealthChecker) Init() error {
}

h.log = log.Sub("health_check")
h.Registry()
return nil
}

Expand All @@ -48,8 +50,10 @@ func (h *HealthChecker) Meta() ioc.ObjectMeta {
return meta
}

func (h *HealthChecker) Registry(ws *restful.WebService) {
func (h *HealthChecker) Registry() {
tags := []string{"健康检查"}

ws := gorestful.ObjectRouter(h)
ws.Route(ws.GET("/").To(h.HealthHandleFunc).
Doc("查询服务当前状态").
Metadata(restfulspec.KeyOpenAPITags, tags).
Expand Down
5 changes: 4 additions & 1 deletion ioc/apps/metric/restful/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/emicklei/go-restful/v3"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/apps/metric"
"github.com/infraboard/mcube/v2/ioc/config/gorestful"
"github.com/infraboard/mcube/v2/ioc/config/http"
"github.com/infraboard/mcube/v2/ioc/config/log"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -26,6 +27,7 @@ type restfulHandler struct {

func (h *restfulHandler) Init() error {
h.log = log.Sub(metric.AppName)
h.Registry()
return nil
}

Expand All @@ -43,8 +45,9 @@ func (h *restfulHandler) Meta() ioc.ObjectMeta {
return meta
}

func (h *restfulHandler) Registry(ws *restful.WebService) {
func (h *restfulHandler) Registry() {
tags := []string{"健康检查"}
ws := gorestful.ObjectRouter(h)
ws.Route(ws.
GET("/").
To(h.MetricHandleFunc).
Expand Down
2 changes: 1 addition & 1 deletion ioc/config/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func init() {
ioc.Config().Registry(&Application{
AppName: "default",
AppName: "",
Domain: "localhost",
EncryptKey: "defualt app encrypt key",
CipherPrefix: "@ciphered@",
Expand Down
2 changes: 1 addition & 1 deletion ioc/config/cors/gin/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (m *CORS) Init() error {

// 将中间件添加到Router中
if m.Enabled {
r := ioc_gin.Get()
r := ioc_gin.RootRouter()
r.Use(cors.New(cors.Config{
AllowOrigins: m.AllowedDomains,
AllowMethods: m.AllowedMethods,
Expand Down
2 changes: 1 addition & 1 deletion ioc/config/cors/gorestful/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (m *CORS) Init() error {
}

// 将中间件添加到Router中
r := gorestful.Get()
r := gorestful.RootRouter()
if m.Enabled {
cors := restful.CrossOriginResourceSharing{
AllowedHeaders: m.AllowedHeaders,
Expand Down
9 changes: 8 additions & 1 deletion ioc/config/gin/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ package gin
import (
"github.com/gin-gonic/gin"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/config/http"
)

const (
AppName = "gin_webframework"
)

func Get() *gin.Engine {
func RootRouter() *gin.Engine {
return ioc.Config().Get(AppName).(*GinFramework).Engine
}

// 添加对象前缀路径
func ObjectRouter(obj ioc.Object) gin.IRouter {
modulePath := http.Get().ApiObjectPathPrefix(obj)
return ioc.Config().Get(AppName).(*GinFramework).Engine.Group(modulePath)
}
6 changes: 3 additions & 3 deletions ioc/config/gorestful/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ const (
AppName = "go_restful_webframework"
)

func Get() *restful.Container {
func RootRouter() *restful.Container {
return ioc.Config().Get(AppName).(*GoRestfulFramework).Container
}

func NewGoRestfulApiService(obj ioc.Object) *restful.WebService {
func ObjectRouter(obj ioc.Object) *restful.WebService {
ws := new(restful.WebService)
ws.
Path(http.Get().ApiObjectPathPrefix(obj)).
Consumes(restful.MIME_JSON, form.MIME_POST_FORM, form.MIME_MULTIPART_FORM, yaml.MIME_YAML, yamlk8s.MIME_YAML).
Produces(restful.MIME_JSON, yaml.MIME_YAML, yamlk8s.MIME_YAML)

// 添加到Root Container
Get().Add(ws)
RootRouter().Add(ws)
return ws
}
4 changes: 2 additions & 2 deletions ioc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ func (s *Server) waitSign() {
default:
s.log.Info().Msgf("receive signal '%v', start graceful shutdown", v.String())

if *s.grpc.Enable {
if s.grpc.IsEnable() {
if err := s.grpc.Stop(s.ctx); err != nil {
s.log.Error().Msgf("grpc graceful shutdown err: %s, force exit", err)
} else {
s.log.Info().Msg("grpc service stop complete")
}
}

if *s.http.Enable {
if s.http.IsEnable() {
if err := s.http.Stop(s.ctx); err != nil {
s.log.Error().Msgf("http graceful shutdown err: %s, force exit", err)
} else {
Expand Down

0 comments on commit 67b8f56

Please sign in to comment.