mvrilo / go-redoc

go-redoc is an embedded OpenAPI/Swagger documentation ui for Go using ReDoc
MIT License
66 stars 28 forks source link

net/http not working with mux routers #5

Closed rcebrian closed 1 year ago

rcebrian commented 1 year ago

Registering the net/http handler under a mux router causes a 404 error serving de docs image

Using the following code:

func RunInternal() error {
    internal := mux.NewRouter()
    internal.HandleFunc("/health", health.GetHealth().Handler)

    doc := redoc.Redoc{
        Title:       "API Docs",
        Description: "API documentation",
        SpecFile:    "./api/openapi-spec/openapi.yaml",
        SpecPath:    "/openapi.yaml",
        DocsPath:    "/docs",
    }

    internal.HandleFunc("/docs", doc.Handler())

    return http.ListenAndServe(":8079", internal)
}
kokororin commented 1 year ago
internal.HandleFunc("/openapi.yaml", HandlerReturnsOpenapiYamlContent)
rcebrian commented 1 year ago
internal.HandleFunc("/openapi.yaml", HandlerReturnsOpenapiYamlContent)

Thanks, looking at http/main.go I saw that the correct way to call is:

func main() {
    doc := redoc.Redoc{
        Title:       "Example API",
        Description: "Example API Description",
        SpecFile:    "./openapi.json",
        SpecPath:    "/openapi.json",
        DocsPath:    "/docs",
    }

    mux := http.ServeMux{}
    handler := doc.Handler()
    mux.Handle("/docs", handler)
    mux.Handle("/openapi.json", handler)
// ...
}