From cd5387e36764c4a3459d7d8d499c93a32244922a Mon Sep 17 00:00:00 2001 From: kooksee Date: Wed, 20 Mar 2024 10:24:08 +0800 Subject: [PATCH] fix: 2024-03-20 10:24:08 --- opendoc/config.go | 23 +++++++++++++---------- opendoc/swagger.go | 1 + templates/rapidoc.html | 36 ++++++++++++++++++++++++++++++++++++ templates/template.go | 14 ++++++++++++++ 4 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 templates/rapidoc.html diff --git a/opendoc/config.go b/opendoc/config.go index 1c65c73..2f4a004 100644 --- a/opendoc/config.go +++ b/opendoc/config.go @@ -3,21 +3,24 @@ package opendoc import "github.com/pubgo/funk/version" // https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ +// https://rapidocweb.com/api.html#att-general type Config struct { - Title string `yaml:"title"` - OpenapiRouter string `yaml:"path"` - OpenapiRedocRouter string `yaml:"redoc-path"` - OpenapiUrl string `yaml:"openapi-path"` - OpenapiOpt map[string]interface{} `yaml:"options"` + Title string `yaml:"title"` + OpenapiRouter string `yaml:"path"` + OpenapiRedocRouter string `yaml:"redoc-path"` + OpenapiRApiDocRouter string `yaml:"rapidoc-path"` + OpenapiUrl string `yaml:"openapi-path"` + OpenapiOpt map[string]interface{} `yaml:"options"` } func DefaultCfg() *Config { return &Config{ - Title: version.Project() + " openapi docs", - OpenapiRouter: "/debug/docs", - OpenapiRedocRouter: "/debug/redocs", - OpenapiUrl: "/debug/docs/openapi.yaml", - OpenapiOpt: make(map[string]interface{}), + Title: version.Project() + " openapi docs", + OpenapiRouter: "/debug/docs", + OpenapiRedocRouter: "/debug/redocs", + OpenapiRApiDocRouter: "/debug/apidocs", + OpenapiUrl: "/debug/docs/openapi.yaml", + OpenapiOpt: make(map[string]interface{}), } } diff --git a/opendoc/swagger.go b/opendoc/swagger.go index 077257a..5b61d83 100644 --- a/opendoc/swagger.go +++ b/opendoc/swagger.go @@ -82,6 +82,7 @@ func (s *Swagger) buildSwagger() *openapi3.T { func (s *Swagger) InitRouter(r *http.ServeMux) { r.Handle(s.Config.OpenapiRouter, templates.SwaggerHandler(s.Config.Title, s.Config.OpenapiUrl)) r.Handle(s.Config.OpenapiRedocRouter, templates.ReDocHandler(s.Config.Title, s.Config.OpenapiUrl)) + r.Handle(s.Config.OpenapiRApiDocRouter, templates.RApiDocHandler(s.Config.OpenapiUrl)) r.Handle(s.Config.OpenapiUrl, s.OpenapiDataHandler()) } diff --git a/templates/rapidoc.html b/templates/rapidoc.html new file mode 100644 index 0000000..fc8a227 --- /dev/null +++ b/templates/rapidoc.html @@ -0,0 +1,36 @@ + + + + + + + + + + \ No newline at end of file diff --git a/templates/template.go b/templates/template.go index f332023..79d4fe5 100644 --- a/templates/template.go +++ b/templates/template.go @@ -14,8 +14,22 @@ var reDocFile string //go:embed swagger.html var swaggerFile string +//go:embed rapidoc.html +var rApiDocFile string + var reDocTemplate = assert.Exit1(template.New("").Parse(reDocFile)) var swaggerTemplate = assert.Exit1(template.New("").Parse(swaggerFile)) +var rApiDocFileTemplate = assert.Exit1(template.New("").Parse(rApiDocFile)) + +func RApiDocHandler(url string) http.HandlerFunc { + return func(writer http.ResponseWriter, request *http.Request) { + writer.Header().Set("Content-Type", "text/html") + assert.Must(rApiDocFileTemplate.Execute(writer, map[string]string{ + "openapi_url": url, + "openapi_options": `{}`, + })) + } +} func ReDocHandler(title, url string) http.HandlerFunc { return func(writer http.ResponseWriter, request *http.Request) {