Skip to content
Draft
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: 30 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
lint:
strategy:
matrix:
go: ['1.17','1.21']
go: ['1.17','1.22']
os: ['ubuntu-latest']
name: lint
runs-on: ${{ matrix.os }}
Expand All @@ -21,11 +21,12 @@ jobs:
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v3
- run: make lint
- uses: golangci/golangci-lint-action@v3
test:
strategy:
matrix:
go: ['1.17','1.21']
go: ['1.17','1.22']
os: ['ubuntu-latest']
name: test
runs-on: ${{ matrix.os }}
Expand All @@ -34,4 +35,30 @@ jobs:
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v3
- run: go test -race ./...
- run: make test
middlewares:
strategy:
matrix:
go: ['1.17','1.22']
os: ['ubuntu-latest']
name: middlewares
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v3
- run: make middlewares
examples:
strategy:
matrix:
go: ['1.17','1.22']
os: ['ubuntu-latest']
name: examples
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v3
- run: make examples
24 changes: 21 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
.PHONY: all lint test deps
.PHONY: all lint golangci-lint test deps examples middlewares

REDOC_PATH=assets/redoc.standalone.js
REDOC_URL=https://cdn.jsdelivr.net/npm/redoc/bundles/redoc.standalone.js

all: $(REDOC_PATH) lint test
all: $(REDOC_PATH) lint test examples middlewares

lint:
go fmt ./...
go vet ./...

golangci-lint:
golangci-lint run ./...

test:
go test -race ./...
go test -cover -race ./...

deps:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

$(REDOC_PATH):
curl -sL -o $(REDOC_PATH) $(REDOC_URL)

examples:
for f in $(shell ls _examples); do ( \
cd _examples/$$f && \
go mod tidy && \
go vet ./... && \
go build -o /dev/null \
); done

middlewares:
for f in gin echo fiber; do ( \
cd $$f && \
go mod tidy && \
go vet ./... && \
go test -cover -race \
); done
2 changes: 2 additions & 0 deletions _examples/echo/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/mvrilo/go-redoc/_examples/echo

go 1.21.5

toolchain go1.22.2

replace github.com/mvrilo/go-redoc => ../../

require (
Expand Down
2 changes: 2 additions & 0 deletions _examples/fiber/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/mvrilo/go-redoc/_examples/fiber

go 1.21.5

toolchain go1.22.2

replace github.com/mvrilo/go-redoc => ../../

require (
Expand Down
2 changes: 2 additions & 0 deletions _examples/gen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/mvrilo/go-redoc/_examples/gen

go 1.21.5

toolchain go1.22.2

replace github.com/mvrilo/go-redoc => ../../

require (
Expand Down
2 changes: 2 additions & 0 deletions _examples/gin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/mvrilo/go-redoc/_examples/gin

go 1.21.5

toolchain go1.22.2

replace github.com/mvrilo/go-redoc => ../../

require (
Expand Down
2 changes: 1 addition & 1 deletion _examples/gorilla/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mvrilo/go-redoc/_examples/fiber

go 1.21.5
go 1.17

replace github.com/mvrilo/go-redoc => ../../

Expand Down
8 changes: 8 additions & 0 deletions _examples/gorilla/go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion _examples/http/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mvrilo/go-redoc/_examples/fiber

go 1.21.5
go 1.17

replace github.com/mvrilo/go-redoc => ../../

Expand Down
8 changes: 8 additions & 0 deletions _examples/http/go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion echo/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mvrilo/go-redoc/echo

go 1.21.5
go 1.17

require (
github.com/labstack/echo/v4 v4.11.4
Expand Down
2 changes: 1 addition & 1 deletion fiber/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mvrilo/go-redoc/fiber

go 1.21.5
go 1.17

require (
github.com/gofiber/adaptor/v2 v2.2.1
Expand Down
87 changes: 87 additions & 0 deletions gin/gin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ginredoc_test

import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"testing/fstest"

"github.com/gin-gonic/gin"
"github.com/mvrilo/go-redoc"
ginredoc "github.com/mvrilo/go-redoc/gin"
)

const openapimock = `{"openapi":"3.0.2","info":{"title":"Swagger Petstore - OpenAPI 3.0","description":"This is a sample Pet Store Server.","termsOfService":"http://swagger.io/terms/","contact":{"email":"apiteam@swagger.io"},"license":{"name":"Apache 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0.html"},"version":"1.0.5"}}`

func TestNew(t *testing.T) {
fs := fstest.MapFS{
"openapi.json": {
Data: []byte(openapimock),
},
}

doc := redoc.Redoc{
Title: "Example API",
Description: "Example API Description",
SpecFS: fs,
SpecFile: "openapi.json",
SpecPath: "/openapi.json",
DocsPath: "/docs",
}

gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.GET("/before", func(ctx *gin.Context) {
ctx.Status(http.StatusNoContent)
})
r.Use(ginredoc.New(doc))
r.GET("/after", func(ctx *gin.Context) {
ctx.String(http.StatusOK, "ok after")
})

tests := []struct {
path string
status int
contentType string
}{
{
path: "/before",
status: http.StatusNoContent,
},
{
path: "/after",
status: http.StatusOK,
},
{
path: "/notfound",
status: http.StatusNotFound,
},
{
path: "/docs",
status: http.StatusOK,
contentType: "text/html",
},
{
path: "/openapi.json",
status: http.StatusOK,
contentType: "application/json",
},
}

for _, tt := range tests {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", tt.path, nil)
r.ServeHTTP(w, req)

if tt.status != w.Code {
t.Errorf(fmt.Sprintf("Got status %d, expected %d", w.Code, tt.status))
}

contentType := strings.ToLower(w.Result().Header.Get("content-type"))
if tt.contentType != "" && !strings.Contains(tt.contentType, contentType) {
t.Errorf(fmt.Sprintf("Got header content-type %s, expected %s", contentType, tt.contentType))
}
}
}
2 changes: 1 addition & 1 deletion gin/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mvrilo/go-redoc/gin

go 1.21.5
go 1.17

require (
github.com/gin-gonic/gin v1.9.1
Expand Down
Loading