diff --git a/contrib/rpc/grpcx/grpcx_grpc_server_config.go b/contrib/rpc/grpcx/grpcx_grpc_server_config.go
index 47ab655da92..f5e7c878ef1 100644
--- a/contrib/rpc/grpcx/grpcx_grpc_server_config.go
+++ b/contrib/rpc/grpcx/grpcx_grpc_server_config.go
@@ -8,6 +8,7 @@ package grpcx
import (
"context"
+
"google.golang.org/grpc"
"github.com/gogf/gf/v2/frame/g"
diff --git a/example/httpserver/swagger/main.go b/example/httpserver/swagger/main.go
index 2105fcba7be..b494c73d2e3 100644
--- a/example/httpserver/swagger/main.go
+++ b/example/httpserver/swagger/main.go
@@ -8,17 +8,21 @@ import (
"github.com/gogf/gf/v2/net/ghttp"
)
+// HelloReq hello request
type HelloReq struct {
g.Meta `path:"/hello" method:"get" sort:"1"`
Name string `v:"required" dc:"Your name"`
}
+// HelloRes hello response
type HelloRes struct {
Reply string `dc:"Reply content"`
}
+// Hello Controller
type Hello struct{}
+// Say function
func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) {
g.Log().Debugf(ctx, `receive say: %+v`, req)
res = &HelloRes{
diff --git a/example/httpserver/swagger_set_template/config.yaml b/example/httpserver/swagger_set_template/config.yaml
new file mode 100644
index 00000000000..b15f8d00786
--- /dev/null
+++ b/example/httpserver/swagger_set_template/config.yaml
@@ -0,0 +1,4 @@
+server:
+ address: ":8199"
+ openapiPath: "/api.json"
+ swaggerPath: "/swagger"
\ No newline at end of file
diff --git a/example/httpserver/swagger_set_template/main.go b/example/httpserver/swagger_set_template/main.go
new file mode 100644
index 00000000000..1277de461d4
--- /dev/null
+++ b/example/httpserver/swagger_set_template/main.go
@@ -0,0 +1,71 @@
+package main
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/gogf/gf/v2/frame/g"
+ "github.com/gogf/gf/v2/net/ghttp"
+)
+
+// HelloReq hello request
+type HelloReq struct {
+ g.Meta `path:"/hello" method:"get" sort:"1"`
+ Name string `v:"required" dc:"Your name"`
+}
+
+// HelloRes hello response
+type HelloRes struct {
+ Reply string `dc:"Reply content"`
+}
+
+// Hello Controller
+type Hello struct{}
+
+// Say function
+func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) {
+ g.Log().Debugf(ctx, `receive say: %+v`, req)
+ res = &HelloRes{
+ Reply: fmt.Sprintf(`Hi %s`, req.Name),
+ }
+ return
+}
+
+const (
+ MySwaggerUITemplate = `
+
+
+
+
+
+
+ SwaggerUI
+
+
+
+
+
+
+
+
+`
+)
+
+func main() {
+ s := g.Server()
+ s.Use(ghttp.MiddlewareHandlerResponse)
+ s.Group("/", func(group *ghttp.RouterGroup) {
+ group.Bind(
+ new(Hello),
+ )
+ })
+ s.SetSwaggerUITemplate(MySwaggerUITemplate)
+ s.Run()
+}
diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go
index 1319aff6b55..4d3355f7bb4 100644
--- a/net/ghttp/ghttp_server_config.go
+++ b/net/ghttp/ghttp_server_config.go
@@ -222,8 +222,9 @@ type ServerConfig struct {
// API & Swagger.
// ======================================================================================================
- OpenApiPath string `json:"openapiPath"` // OpenApiPath specifies the OpenApi specification file path.
- SwaggerPath string `json:"swaggerPath"` // SwaggerPath specifies the swagger UI path for route registering.
+ OpenApiPath string `json:"openapiPath"` // OpenApiPath specifies the OpenApi specification file path.
+ SwaggerPath string `json:"swaggerPath"` // SwaggerPath specifies the swagger UI path for route registering.
+ SwaggerUITemplate string `json:"swaggerUITemplate"` // SwaggerUITemplate specifies the swagger UI custom template
// ======================================================================================================
// Other.
diff --git a/net/ghttp/ghttp_server_config_mess.go b/net/ghttp/ghttp_server_config_mess.go
index 23d23e8b345..44113877885 100644
--- a/net/ghttp/ghttp_server_config_mess.go
+++ b/net/ghttp/ghttp_server_config_mess.go
@@ -32,6 +32,11 @@ func (s *Server) SetSwaggerPath(path string) {
s.config.SwaggerPath = path
}
+// SetSwaggerUITemplate sets the Swagger template for server.
+func (s *Server) SetSwaggerUITemplate(swaggerUITemplate string) {
+ s.config.SwaggerUITemplate = swaggerUITemplate
+}
+
// SetOpenApiPath sets the OpenApiPath for server.
func (s *Server) SetOpenApiPath(path string) {
s.config.OpenApiPath = path
diff --git a/net/ghttp/ghttp_server_swagger.go b/net/ghttp/ghttp_server_swagger.go
index 2068e89c1f8..7ef7a19181e 100644
--- a/net/ghttp/ghttp_server_swagger.go
+++ b/net/ghttp/ghttp_server_swagger.go
@@ -7,16 +7,12 @@
package ghttp
import (
- "fmt"
-
"github.com/gogf/gf/v2/text/gstr"
)
const (
- swaggerUIDocName = `redoc.standalone.js`
- swaggerUIDocNamePlaceHolder = `{SwaggerUIDocName}`
- swaggerUIDocURLPlaceHolder = `{SwaggerUIDocUrl}`
- swaggerUITemplate = `
+ swaggerUIDocURLPlaceHolder = `{SwaggerUIDocUrl}`
+ swaggerUITemplate = `
@@ -32,7 +28,7 @@ const (
-
+
`
@@ -44,10 +40,14 @@ func (s *Server) swaggerUI(r *Request) {
if s.config.OpenApiPath == "" {
return
}
+ var templateContent = swaggerUITemplate
+ if s.config.SwaggerUITemplate != "" {
+ templateContent = s.config.SwaggerUITemplate
+ }
+
if r.StaticFile != nil && r.StaticFile.File != nil && r.StaticFile.IsDir {
- content := gstr.ReplaceByMap(swaggerUITemplate, map[string]string{
- swaggerUIDocURLPlaceHolder: s.config.OpenApiPath,
- swaggerUIDocNamePlaceHolder: gstr.TrimRight(fmt.Sprintf(`//%s%s`, r.Host, r.Server.config.SwaggerPath), "/") + "/" + swaggerUIDocName,
+ content := gstr.ReplaceByMap(templateContent, map[string]string{
+ swaggerUIDocURLPlaceHolder: s.config.OpenApiPath,
})
r.Response.Write(content)
r.ExitAll()