Git Product home page Git Product logo

fiber-swagger's Introduction

fiber-swagger

fiber middleware to automatically generate RESTful API documentation with Swagger 2.0.

Usage

Start using it

  1. Add comments to your API source code, See Declarative Comments Format.
  2. Download Swag for Go by using:
$ go get -u github.com/swaggo/swag/cmd/swag
  1. Run the Swag in your Go project root folder which contains main.go file, Swag will parse comments and generate required files(docs folder and docs/doc.go).
$ swag init
  1. Download fiber-swagger by using:
$ go get -u github.com/arsmn/fiber-swagger/v2

And import following in your code:

import "github.com/arsmn/fiber-swagger/v2" // fiber-swagger middleware

Canonical example:

package main

import (
	swagger "github.com/arsmn/fiber-swagger/v2"
	"github.com/gofiber/fiber/v2"

	// docs are generated by Swag CLI, you have to import them.
	// replace with your own docs folder, usually "github.com/username/reponame/docs"
	_ "github.com/arsmn/fiber-swagger/v2/example/docs"
)

// @title Fiber Example API
// @version 1.0
// @description This is a sample swagger for Fiber
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /
func main() {
	app := fiber.New()

	app.Get("/swagger/*", swagger.HandlerDefault) // default

	app.Get("/swagger/*", swagger.New(swagger.Config{ // custom
		URL: "http://example.com/doc.json",
		DeepLinking: false,
		// Expand ("list") or Collapse ("none") tag groups by default
		DocExpansion: "none",
		// Prefill OAuth ClientId on Authorize popup
		OAuth: &swagger.OAuthConfig{
			AppName:  "OAuth Provider",
			ClientId: "21bb4edc-05a7-4afc-86f1-2e151e4ba6e2",
		},
		// Ability to change OAuth2 redirect uri location
		OAuth2RedirectUrl: "http://localhost:8080/swagger/oauth2-redirect.html",
	}))

	app.Listen(":8080")
}
  1. Run it, and browser to http://localhost:8080/swagger, you can see Swagger 2.0 Api documents.

fiber-swagger's People

Contributors

arsmn avatar azat-k avatar ewenquim avatar gkampitakis avatar mlukasik-dev avatar moritzgruber avatar sixcolors avatar techrazor 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  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

fiber-swagger's Issues

Manage errors

Hi,
how can I add error handling in swagger ?
regards

cannot find type definition: address.Address

2020/11/17 11:46:44 Generating wallet.Address
2020/11/17 11:46:44 ParseComment error in file api/handlers_transfer.go :cannot find type definition: address.Address

code

package wallet

import(
	"github.com/filecoin-project/go-address"  // package address
)

type AddressBalance struct {
	Addr    address.Address
	Balance types.BigInt
	Default bool
}

License missing

Hi! Could you please add a license to you repository? Otherwise we cannot use it in our company. Thanks in advance!

After updating from v2.2.0 to v2.2.4 get javascript error on load

After updating our project to use 2.2.4 eg:
go get: upgraded github.com/arsmn/fiber-swagger/v2 v2.2.0 => v2.24.0

the swagger WebApp started giving a javascript error. I also noticed it did not download the .json file that it normally does, which makes sense with the error as it fails on the downloadSpec() func.

image

Cannot make Swagger route use the correct doc.json

Hi !

I am trying to use your extension in order set-up correctly a swagger endpoint.

I am very new to Go so it's kind of difficult to debug what's happening as I would like. Feel free to give me any advice.

I have carefully followed the instructions provided in your README file and was not able to get the swagger interface to display my project schema. It keeps displaying the default schema instead.

What I did :

  • Installed swag
  • Ran swag init to initialize the ./doc directory with docs.go, swagger.json, swagger.yaml
  • Added imports to my main.go file
  • Added declarative comments to my main.go file
  • Added the default handler to my main.go file
  • Ran swag init to update the ./doc directory files

main.go:

package main

import (
	"fmt"
	swagger "github.com/arsmn/fiber-swagger"
	_ "github.com/arsmn/fiber-swagger/example/docs"
	"github.com/gofiber/fiber"
	configuration "gitlab.com/xxx/api-media-manager/pkg"
	"gitlab.com/xxx/api-media-manager/pkg/handlers"
)

// @title Media Manager API
// @version 1.0
// @description Media Manager service
func main() {
	/* Get configuration */

	mediaManagerConfiguration := configuration.New()

	/* Set-up server */

	server := fiber.New()

	server.Use("/swagger", swagger.Handler)

	server.Get("/*", handlers.ReadImage)

	/* Run server */

	address := fmt.Sprintf("%s:%d", mediaManagerConfiguration.Host, mediaManagerConfiguration.Port)

	server.Listen(address)
}

Taking a look at ./doc directory files, they are up to date and contain the information defined in my main.go file.

Thanks for your help !

Provide swagger.json with application

	var (
		port = envInt("PORT", 4321)
		host = envString("HOST", fmt.Sprintf("http://localhost:%d", port))
	)

	app := fiber.New()
	docs.SwaggerInfo.Host = host
	app.Use("/docs", swagger.New(swagger.Config{ // custom
		URL:         host + "/swagger.json",
		DeepLinking: true,
	}))
	setUpRoutes(app)

unknown revision codec/v1.1.5-pre

go: github.com/arsmn/fiber-swagger/[email protected] requires
	github.com/swaggo/[email protected] requires
	github.com/swaggo/[email protected] requires
	github.com/ugorji/[email protected] requires
	github.com/ugorji/go/[email protected]: reading github.com/ugorji/go/codec/codec/go.mod at revision codec/v1.1.5-pre: unknown revision codec/v1.1.5-pre

To do this, clear the cache and click sync in Goland. It is strange that it is bad at pulling packages automatically :/

defaultDocURL

Why do we have defaultDocURL = "doc.json" while "swag init " generates swagger.json by default?

Unable to install. Possible dependency issue

On my local computer go get ends with an error. Did not work for 2.1.2 either.

$ go get github.com/arsmn/fiber-swagger/v2
go: downloading github.com/arsmn/fiber-swagger/v2 v2.1.3
go: github.com/arsmn/fiber-swagger/v2 upgrade => v2.1.3
go get: github.com/arsmn/fiber-swagger/[email protected] requires
        github.com/swaggo/[email protected] requires
        github.com/swaggo/[email protected] requires
        github.com/ugorji/[email protected] requires
        github.com/ugorji/go/[email protected]: reading github.com/ugorji/go/codec/codec/go.mod at revision codec/v1.1.5-pre: unknown revision codec/v1.1.5-pre
$ go version
go version go1.15.3 linux/amd64

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.