Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<title>{{ .title }}</title>
<meta name="description" content="{{ .description }}">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">

<style>
body {
margin: 0;
Expand All @@ -13,8 +15,7 @@
</style>
</head>
<body>
<div id="main"></div>
<script>{{ .body }}</script>
<script>Redoc.init("{{ .url }}", {}, document.getElementById("main"))</script>
<redoc spec-url="{{ .url }}"></redoc>
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"> </script>
</body>
</html>
29 changes: 29 additions & 0 deletions redoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"text/template"
)
Expand All @@ -18,6 +19,7 @@ type Redoc struct {
DocsPath string
SpecPath string
SpecFile string
SpecDir string
SpecFS *embed.FS
Title string
Description string
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -101,6 +107,29 @@ func (r Redoc) Handler() http.HandlerFunc {
header.Set("Content-Type", "text/html")
_, _ = w.Write(data)
w.WriteHeader(200)

return
}

// 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
}

_, _ = w.Write(subSpec)
w.WriteHeader(200)

return
}

}
}