Giter Site home page Giter Site logo

mvrilo / go-redoc Goto Github PK

View Code? Open in Web Editor NEW
60.0 4.0 27.0 1.01 MB

go-redoc is an embedded OpenAPI/Swagger documentation ui for Go using ReDoc

License: MIT License

Makefile 5.34% Go 89.35% HTML 5.31%
documentation go redoc openapi swagger echo fiber gin golang http

go-redoc's Introduction

go-redoc

GoDoc Go Report Card

go-redoc is an embedded OpenAPI documentation ui for Go using ReDoc and Go's 1.16+'s embed package, with middleware implementations for: net/http, gin, fiber, and echo.

The template is based on the ReDoc's bundle template with the script already placed in the html instead of depending on a CDN.

This package does not generate openapi spec file. Check this example for using code generation with swag.

Usage

import "github.com/mvrilo/go-redoc"

...

doc := redoc.Redoc{
    Title:       "Example API",
    Description: "Example API Description",
    SpecFile:    "./openapi.json", // "./openapi.yaml"
    SpecPath:    "/openapi.json",  // "/openapi.yaml"
    DocsPath:    "/docs",
}
  • net/http
import (
	"net/http"
	"github.com/mvrilo/go-redoc"
)

...

http.ListenAndServe(address, doc.Handler())
  • gin
import (
	"github.com/gin-gonic/gin"
	"github.com/mvrilo/go-redoc"
	ginredoc "github.com/mvrilo/go-redoc/gin"
)

...

r := gin.New()
r.Use(ginredoc.New(doc))
  • echo
import (
	"github.com/labstack/echo/v4"
	"github.com/mvrilo/go-redoc"
	echoredoc "github.com/mvrilo/go-redoc/echo"
)

...

r := echo.New()
r.Use(echoredoc.New(doc))
  • fiber
import (
	"github.com/gofiber/fiber/v2"
	"github.com/mvrilo/go-redoc"
	fiberredoc "github.com/mvrilo/go-redoc/fiber"
)

...

r := fiber.New()
r.Use(fiberredoc.New(doc))

See examples

go-redoc's People

Contributors

grindlemire avatar mvrilo avatar thiskevinwang avatar ttys3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go-redoc's Issues

load redoc in a path problem

This is example of my code where I try to load the openapi spec in a certain path. Code is not compiled its just
to show how I did it.

Client struct {
    Router *mux.Router
}

client := &Client{}

doc := redoc.Redoc{
	Title:       "",
	Description: "",
	SpecFile:    "./api.yaml",
	SpecPath:    "/api.yaml",
        DocsPath:    "/docs",
}


client.Router.HandleFunc("/hello",HelloHandlerFunc).Methods(http.MethodGet)
client.Router.HandleFunc("/openapi",doc.Handler).Methods(http.MethodGet)

I am trying to access the docs through http://localhost:port/openapi/docs without luck. It just return blank white page

gin response code 404

This is my test demo.

package main

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

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

	r := gin.New()
	r.Use(ginredoc.New(doc))

	println("Documentation served at http://127.0.0.1:8000/docs")
	panic(r.Run(":8000"))
}

When I run and visit http://172.28.73.58:8000/docs Page stuck in initialization (172.28.73.58 is my wsl2 ip)
image
image
And my gin run window has a warning message
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 404 with 200
Can you tell me how to solve it? thank

About Echo implementation

Hi, I really like the project @mvrilo but there's some suggestions that I think could improve the project, between these are:

  • The echo example could be updated for something like this:
    image

    The problem with use a middleware for just show documentation is that overwrites all other endpoints.

    image

  • Other things that I'm sure that goes well to the package is the json/yaml tags for the structs, more than nothing because normally the HTTP Servers have configuration files and directly set everything in that configuration file sounds good.

  • Another improvement could be the favicon.ico implementation, in the same way the openapi.yaml is provided in my case.

I don't know if I can open a merge request to make these changes, ready to hear your opinion, me.

net/http not working with mux routers

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.