Skip to content

Commit

Permalink
feat: update dependent redoc js for swagger ui (gogf#3217)
Browse files Browse the repository at this point in the history
  • Loading branch information
hailaz authored Dec 28, 2023
1 parent 9f7ce42 commit 984cca8
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 12 deletions.
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

0 comments on commit 984cca8

Please sign in to comment.