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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,36 @@ r.Use(fiberredoc.New(doc))
```

See [examples](/_examples)


## Configuration Options


```go
r := redoc.Redoc{
SpecFile: "testdata/spec.json",
SpecFS: &spec,
SpecPath: "/openapi.json", // "/openapi.yaml" Title: "Test API",
Description: "Meta Description"
Options: map[string]any{
"disableSearch": true,
"theme": map[string]any{
"colors": map[string]any{"primary": map[string]any{"main": "#297b21"}},
"typography": map[string]any{"headings": map[string]any{"fontWeight": "600"}}
"sidebar": map[string]any{"backgroundColor": "#cae6c6"},
},
},
}
```

`Title` : The head title of your html page - Shown on search engine.

`Description` : The head meta description of your html page - Shown on search engine.

`Options`: redoc option see [Redoc Configuration Documentation](https://github.com/Redocly/redoc/blob/main/docs/config.md)

`SpecFile`: file path to your openapi/swagger file from your project.

`SpecPath`: url path to call your openapi/swagger file from redoc documentation. Must be aligned with your web server configuration.

`DocsPath` : url path to call your generated API documentation. Must be aligned with your web server configuration.
2 changes: 1 addition & 1 deletion assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
<body>
<div id="main"></div>
<script>{{ .body }}</script>
<script>Redoc.init("{{ .url }}", {}, document.getElementById("main"))</script>
<script>Redoc.init("{{ .url }}", {{ .options }}, document.getElementById("main"))</script>
</body>
</html>
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mvrilo/go-redoc

go 1.17
go 1.18

require github.com/stretchr/testify v1.8.4

Expand Down
13 changes: 13 additions & 0 deletions redoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import (
"bytes"
"embed"
"encoding/json"
"errors"
"log"
"net/http"
"os"
"strings"
Expand All @@ -21,6 +23,7 @@
SpecFS *embed.FS
Title string
Description string
Options map[string]any

Check failure on line 26 in redoc.go

View workflow job for this annotation

GitHub Actions / test (1.17, ubuntu-latest)

undefined: any
}

// HTML represents the redoc index.html page
Expand All @@ -41,11 +44,21 @@
return nil, err
}

var optionsString = "{}"

var optionsByte, errM = json.Marshal(r.Options)
if errM == nil {
optionsString = string(optionsByte)
} else {
log.Printf("Invalid json options provided, using default options instead.")
}

if err = tpl.Execute(buf, map[string]string{
"body": JavaScript,
"title": r.Title,
"url": r.SpecPath,
"description": r.Description,
"options": optionsString,
}); err != nil {
return nil, err
}
Expand Down
56 changes: 56 additions & 0 deletions redoc_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package redoc_test

Check failure on line 1 in redoc_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21, ubuntu-latest)

: # github.com/mvrilo/go-redoc_test [github.com/mvrilo/go-redoc.test]

import (
"embed"
Expand Down Expand Up @@ -60,3 +60,59 @@
})
})
}

func TestRedocWithOptions(t *testing.T) {
r := redoc.Redoc{
SpecFile: "testdata/spec.json",
SpecFS: &spec,
SpecPath: "/openapi.json", // "/openapi.yaml"
Title: "Test API",
Options: map[string]any{
"disableSearch": true,
"theme": map[string]any{
"colors": map[string]any{"primary": map[string]any{"main": "#297b21"}},
"typography": map[string]any{"headings": map[string]any{"fontWeight": "600"}},
"sidebar": map[string]any{"backgroundColor": "#cae6c6"},
},
},
}

t.Run("Body", func(t *testing.T) {
body, err := r.Body()
assert.NoError(t, err)
assert.Contains(t, string(body), r.Title)
})

t.Run("Handler", func(t *testing.T) {
handler := r.Handler()

t.Run("Spec", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/openapi.json", nil)
w := httptest.NewRecorder()
handler(w, req)

resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))

body, err := ioutil.ReadAll(resp.Body)

Check failure on line 98 in redoc_test.go

View workflow job for this annotation

GitHub Actions / lint (1.17, ubuntu-latest)

undefined: ioutil (typecheck)

Check failure on line 98 in redoc_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21, ubuntu-latest)

undefined: ioutil

Check failure on line 98 in redoc_test.go

View workflow job for this annotation

GitHub Actions / test (1.21, ubuntu-latest)

undefined: ioutil
assert.NoError(t, err)
assert.Contains(t, string(body), `"swagger":"2.0"`)
})

t.Run("Docs", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
handler(w, req)

resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "text/html", resp.Header.Get("Content-Type"))

body, err := ioutil.ReadAll(resp.Body)

Check failure on line 112 in redoc_test.go

View workflow job for this annotation

GitHub Actions / lint (1.17, ubuntu-latest)

undefined: ioutil (typecheck)

Check failure on line 112 in redoc_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21, ubuntu-latest)

undefined: ioutil (typecheck)

Check failure on line 112 in redoc_test.go

View workflow job for this annotation

GitHub Actions / test (1.21, ubuntu-latest)

undefined: ioutil
assert.NoError(t, err)
assert.Contains(t, string(body), r.Title)
assert.Contains(t, string(body), `{"disableSearch`)
})
})
}
Loading