1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package cmd
- import (
- "context"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/net/ghttp"
- "github.com/gogf/gf/v2/net/goai"
- "github.com/gogf/gf/v2/os/gcmd"
- "go-gpt/internal/consts"
- "go-gpt/router"
- )
- const (
- swaggerUIPageContent = `
- <!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://unpkg.com/swagger-ui-dist@latest/swagger-ui.css" />
- </head>
- <body>
- <div id="swagger-ui"></div>
- <script src="https://unpkg.com/swagger-ui-dist@latest/swagger-ui-bundle.js" crossorigin></script>
- <script>
- window.onload = () => {
- window.ui = SwaggerUIBundle({
- url: '/api.json',
- dom_id: '#swagger-ui',
- });
- };
- </script>
- </body>
- </html>
- `
- )
- var (
- Main = gcmd.Command{
- Name: "main",
- Usage: "main",
- Brief: "start http server",
- Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
- s := g.Server()
- s.Group("/", func(group *ghttp.RouterGroup) {
- group.GET("/swagger", func(r *ghttp.Request) {
- r.Response.Write(swaggerUIPageContent)
- })
- router.BindController(group)
- })
- //enhanceOpenAPIDoc(s)
- s.SetOpenApiPath("/api.json")
- s.Run()
- return nil
- },
- }
- )
- func enhanceOpenAPIDoc(s *ghttp.Server) {
- openapi := s.GetOpenApi()
- openapi.Config.CommonResponse = ghttp.DefaultHandlerResponse{}
- openapi.Config.CommonResponseDataField = `Data`
- // API description.
- openapi.Info = goai.Info{
- Title: consts.OpenAPITitle,
- Description: consts.OpenAPIDescription,
- Contact: &goai.Contact{
- Name: "GoFrame",
- URL: "https://goframe.org",
- },
- }
- }
|