Skip to content

Commit

Permalink
修复swagger路径问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Oct 13, 2024
1 parent 9bd14d8 commit 2770635
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
16 changes: 13 additions & 3 deletions ioc/apps/apidoc/restful/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package restful

import (
"fmt"
"path/filepath"

restfulspec "github.com/emicklei/go-restful-openapi/v2"
"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/application"
"github.com/infraboard/mcube/v2/ioc/config/gorestful"
"github.com/infraboard/mcube/v2/ioc/config/http"
"github.com/infraboard/mcube/v2/ioc/config/log"
Expand Down Expand Up @@ -50,11 +52,19 @@ func (h *SwaggerApiDoc) Meta() ioc.ObjectMeta {
}

func (h *SwaggerApiDoc) ApiDocPath() string {
return fmt.Sprintf("%s%s", http.Get().ApiObjectAddr(h), h.JsonPath)
if application.Get().Domain != "" {
return application.Get().Endpoint() + filepath.Join(http.Get().ApiObjectPathPrefix(h), h.JsonPath)
}

return http.Get().ApiObjectAddr(h) + h.JsonPath
}

func (h *SwaggerApiDoc) ApiUIPath() string {
return fmt.Sprintf("%s%s", http.Get().ApiObjectAddr(h), h.UIPath)
if application.Get().Domain != "" {
return application.Get().Endpoint() + filepath.Join(http.Get().ApiObjectPathPrefix(h), h.UIPath)
}

return http.Get().ApiObjectAddr(h) + h.UIPath
}

func (h *SwaggerApiDoc) Registry() {
Expand Down Expand Up @@ -87,7 +97,7 @@ func (h *SwaggerApiDoc) SwaggerUI(r *restful.Request, w *restful.Response) {
// API Doc
func (h *SwaggerApiDoc) SwaggerDocConfig() restfulspec.Config {
return restfulspec.Config{
Host: http.Get().Host,
Host: http.Get().Addr(),
WebServices: restful.RegisteredWebServices(),
APIPath: http.Get().ApiObjectPathPrefix(h),
PostBuildSwaggerObjectHandler: http.Get().SwagerDocs,
Expand Down
14 changes: 12 additions & 2 deletions ioc/apps/apidoc/swaggo/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package swaggo

import (
"fmt"
"path/filepath"

"github.com/gin-gonic/gin"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/apps/apidoc"
"github.com/infraboard/mcube/v2/ioc/config/application"
ioc_gin "github.com/infraboard/mcube/v2/ioc/config/gin"
"github.com/infraboard/mcube/v2/ioc/config/http"
"github.com/infraboard/mcube/v2/ioc/config/log"
Expand Down Expand Up @@ -60,11 +62,19 @@ func (h *SwaggerApiDoc) Registry() {
}

func (h *SwaggerApiDoc) ApiDocPath() string {
return fmt.Sprintf("%s%s", http.Get().ApiObjectAddr(h), h.JsonPath)
if application.Get().Domain != "" {
return application.Get().Endpoint() + filepath.Join(http.Get().ApiObjectPathPrefix(h), h.JsonPath)
}

return http.Get().ApiObjectAddr(h) + h.JsonPath
}

func (h *SwaggerApiDoc) ApiUIPath() string {
return fmt.Sprintf("%s%s", http.Get().ApiObjectAddr(h), h.UIPath)
if application.Get().Domain != "" {
return application.Get().Endpoint() + filepath.Join(http.Get().ApiObjectPathPrefix(h), h.UIPath)
}

return http.Get().ApiObjectAddr(h) + h.UIPath
}

func (h *SwaggerApiDoc) SwaggerJson(ctx *gin.Context) {
Expand Down
4 changes: 2 additions & 2 deletions ioc/config/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func init() {
var defaultConfig = &Application{
AppName: "",
AppGroup: "default",
Domain: "localhost",
Domain: "",
Security: false,
EncryptKey: "defualt app encrypt key",
CipherPrefix: "@ciphered@",
Expand Down Expand Up @@ -49,7 +49,7 @@ func (i *Application) GetAppName() string {
}

func (i *Application) IsInternalIP() bool {
return i.Domain != "localhost"
return i.Domain != "localhost" && i.Domain != "" && i.Domain != "127.0.0.1"
}

func (i *Application) Endpoint() string {
Expand Down

0 comments on commit 2770635

Please sign in to comment.