Skip to content

Commit

Permalink
fix: 2024-03-20 10:24:08
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Mar 20, 2024
1 parent 6bed1ed commit cd5387e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
23 changes: 13 additions & 10 deletions opendoc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
}
}
1 change: 1 addition & 0 deletions opendoc/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
36 changes: 36 additions & 0 deletions templates/rapidoc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!doctype html> <!-- Important: must specify -->
<html>
<head>
<meta charset="utf-8"> <!-- Important: rapi-doc uses utf8 characters -->
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
</head>
<body>
<rapi-doc
spec-url="{{ .openapi_url }}"
id="r-api-doc"
theme = "dark"
primary-color = "#f54c47"
bg-color = "#2e3746"
text-color = "#bacdee"
show-header = "true"
schema-style="table"
show-method-in-nav-bar="true"
use-path-in-nav-bar="true"
show-components="true"
show-info="true"
allow-search="true"
allow-advanced-search="true"
allow-spec-url-load="true"
allow-spec-file-download="true"
allow-server-selection="true"
allow-authentication="true"
update-route="true"
match-type="regex"
persist-auth="true"
scroll-behavior="smooth"

schema-style="tree"
render-style="read"
></rapi-doc>
</body>
</html>
14 changes: 14 additions & 0 deletions templates/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit cd5387e

Please sign in to comment.