From 19a548064f0a81359cbb96851dca272e6ef0f8be Mon Sep 17 00:00:00 2001 From: wowissu Date: Wed, 23 Aug 2023 13:55:43 +0800 Subject: [PATCH 1/4] feat: allow to read sub spec by SpecDir --- redoc.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/redoc.go b/redoc.go index f70de33..fd64cd0 100644 --- a/redoc.go +++ b/redoc.go @@ -6,6 +6,7 @@ import ( "errors" "io/ioutil" "net/http" + "path/filepath" "strings" "text/template" ) @@ -18,6 +19,7 @@ type Redoc struct { DocsPath string SpecPath string SpecFile string + SpecDir string SpecFS *embed.FS Title string Description string @@ -69,6 +71,10 @@ func (r Redoc) Handler() http.HandlerFunc { r.SpecPath = "/openapi.json" } + if r.SpecDir == "" { + r.SpecDir = "components" + } + var spec []byte if r.SpecFS == nil { spec, err = ioutil.ReadFile(specFile) @@ -102,5 +108,17 @@ func (r Redoc) Handler() http.HandlerFunc { _, _ = w.Write(data) w.WriteHeader(200) } + + // load sub spec + p := filepath.Join(r.SpecDir, filepath.FromSlash(req.URL.Path)) + subSpec, err := ioutil.ReadFile(p) + header.Set("Content-Type", "application/json") + + if err != nil { + w.WriteHeader(404) + } else { + _, _ = w.Write(subSpec) + w.WriteHeader(200) + } } } From f3cd0e88959b733e862fa3e0208df217b7b93e67 Mon Sep 17 00:00:00 2001 From: wowissu Date: Wed, 23 Aug 2023 13:59:12 +0800 Subject: [PATCH 2/4] feat: only allow .yaml and .json --- redoc.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/redoc.go b/redoc.go index fd64cd0..0a80b00 100644 --- a/redoc.go +++ b/redoc.go @@ -107,18 +107,29 @@ func (r Redoc) Handler() http.HandlerFunc { header.Set("Content-Type", "text/html") _, _ = w.Write(data) w.WriteHeader(200) + + return } - // load sub spec - p := filepath.Join(r.SpecDir, filepath.FromSlash(req.URL.Path)) - subSpec, err := ioutil.ReadFile(p) - header.Set("Content-Type", "application/json") + // load spec files + ext := filepath.Ext(req.URL.Path) + if ext == ".yaml" || ext == ".json" { + header.Set("Content-Type", "application/json") + p := filepath.Join(r.SpecDir, filepath.FromSlash(req.URL.Path)) + subSpec, err := ioutil.ReadFile(p) + + if err != nil { + _, _ = w.Write([]byte("file not found.")) + w.WriteHeader(404) + + return + } - if err != nil { - w.WriteHeader(404) - } else { _, _ = w.Write(subSpec) w.WriteHeader(200) + + return } + } } From e9ce10d41e00b6e2638e4b7319d272a97d703464 Mon Sep 17 00:00:00 2001 From: wowissu Date: Fri, 1 Sep 2023 16:02:19 +0800 Subject: [PATCH 3/4] feat: use cmd not local file --- assets/index.html | 7 ++++--- redoc.go | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/assets/index.html b/assets/index.html index 09a1ef5..062a546 100644 --- a/assets/index.html +++ b/assets/index.html @@ -5,6 +5,8 @@ {{ .title }} + + -
- - + + diff --git a/redoc.go b/redoc.go index 0a80b00..7dca215 100644 --- a/redoc.go +++ b/redoc.go @@ -33,7 +33,7 @@ var HTML string // JavaScript represents the redoc standalone javascript // //go:embed assets/redoc.standalone.js -var JavaScript string +// var JavaScript string // Body returns the final html with the js in the body func (r Redoc) Body() ([]byte, error) { @@ -44,7 +44,7 @@ func (r Redoc) Body() ([]byte, error) { } if err = tpl.Execute(buf, map[string]string{ - "body": JavaScript, + // "body": JavaScript, "title": r.Title, "url": r.SpecPath, "description": r.Description, From 4ba456288643921ea1fd6d0562af49022ff4f744 Mon Sep 17 00:00:00 2001 From: wowissu Date: Mon, 4 Sep 2023 00:03:39 +0800 Subject: [PATCH 4/4] fix: misplaced go:embed directive --- redoc.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redoc.go b/redoc.go index 7dca215..0a80b00 100644 --- a/redoc.go +++ b/redoc.go @@ -33,7 +33,7 @@ var HTML string // JavaScript represents the redoc standalone javascript // //go:embed assets/redoc.standalone.js -// var JavaScript string +var JavaScript string // Body returns the final html with the js in the body func (r Redoc) Body() ([]byte, error) { @@ -44,7 +44,7 @@ func (r Redoc) Body() ([]byte, error) { } if err = tpl.Execute(buf, map[string]string{ - // "body": JavaScript, + "body": JavaScript, "title": r.Title, "url": r.SpecPath, "description": r.Description,