Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contrib/rpc/grpcx/grpcx_grpc_server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package grpcx

import (
"context"

"google.golang.org/grpc"

"github.com/gogf/gf/v2/frame/g"
Expand Down
4 changes: 4 additions & 0 deletions example/httpserver/swagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 4 additions & 0 deletions example/httpserver/swagger_set_template/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
server:
address: ":8199"
openapiPath: "/api.json"
swaggerPath: "/swagger"
71 changes: 71 additions & 0 deletions example/httpserver/swagger_set_template/main.go
Original file line number Diff line number Diff line change
@@ -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 = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI"/>
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.5/swagger-ui.min.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.5/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '{SwaggerUIDocUrl}',
dom_id: '#swagger-ui',
});
};
</script>
</body>
</html>
`
)

func main() {
s := g.Server()
s.Use(ghttp.MiddlewareHandlerResponse)
s.Group("/", func(group *ghttp.RouterGroup) {
group.Bind(
new(Hello),
)
})
s.SetSwaggerUITemplate(MySwaggerUITemplate)
s.Run()
}
5 changes: 3 additions & 2 deletions net/ghttp/ghttp_server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions net/ghttp/ghttp_server_config_mess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions net/ghttp/ghttp_server_swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
<!DOCTYPE html>
<html>
<head>
Expand All @@ -32,7 +28,7 @@ const (
</head>
<body>
<redoc spec-url="{SwaggerUIDocUrl}" show-object-schema-examples="true"></redoc>
<script src="https://unpkg.com/redoc@2.0.0-rc.70/bundles/redoc.standalone.js"> </script>
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"> </script>
</body>
</html>
`
Expand All @@ -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()
Expand Down