cmd.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package cmd
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/net/ghttp"
  6. "github.com/gogf/gf/v2/net/goai"
  7. "github.com/gogf/gf/v2/os/gcmd"
  8. "go-gpt/internal/consts"
  9. "go-gpt/router"
  10. )
  11. const (
  12. swaggerUIPageContent = `
  13. <!DOCTYPE html>
  14. <html lang="en">
  15. <head>
  16. <meta charset="utf-8" />
  17. <meta name="viewport" content="width=device-width, initial-scale=1" />
  18. <meta name="description" content="SwaggerUI"/>
  19. <title>SwaggerUI</title>
  20. <link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@latest/swagger-ui.css" />
  21. </head>
  22. <body>
  23. <div id="swagger-ui"></div>
  24. <script src="https://unpkg.com/swagger-ui-dist@latest/swagger-ui-bundle.js" crossorigin></script>
  25. <script>
  26. window.onload = () => {
  27. window.ui = SwaggerUIBundle({
  28. url: '/api.json',
  29. dom_id: '#swagger-ui',
  30. });
  31. };
  32. </script>
  33. </body>
  34. </html>
  35. `
  36. )
  37. var (
  38. Main = gcmd.Command{
  39. Name: "main",
  40. Usage: "main",
  41. Brief: "start http server",
  42. Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
  43. s := g.Server()
  44. s.Group("/", func(group *ghttp.RouterGroup) {
  45. group.GET("/swagger", func(r *ghttp.Request) {
  46. r.Response.Write(swaggerUIPageContent)
  47. })
  48. router.BindController(group)
  49. })
  50. //enhanceOpenAPIDoc(s)
  51. s.SetOpenApiPath("/api.json")
  52. s.Run()
  53. return nil
  54. },
  55. }
  56. )
  57. func enhanceOpenAPIDoc(s *ghttp.Server) {
  58. openapi := s.GetOpenApi()
  59. openapi.Config.CommonResponse = ghttp.DefaultHandlerResponse{}
  60. openapi.Config.CommonResponseDataField = `Data`
  61. // API description.
  62. openapi.Info = goai.Info{
  63. Title: consts.OpenAPITitle,
  64. Description: consts.OpenAPIDescription,
  65. Contact: &goai.Contact{
  66. Name: "GoFrame",
  67. URL: "https://goframe.org",
  68. },
  69. }
  70. }