beego_swagger

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2021 License: MIT Imports: 10 Imported by: 1

README

beego-swagger

beego 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  
$ go get -u github.com/weblfe/swag2/cmd/swag # swag2 support write comment in interface and show scan file path
  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 beego-swagger by using:
$ go get -u github.com/weblfe/beego-swagger

And import following in your code:

import "github.com/weblfe/beego-swagger" // beego-swagger middleware

Canonical example:
package main

import (
	swagger "github.com/weblfe/beego-swagger"
	"github.com/astaxie/beego"

	// docs are generated by Swag CLI, you have to import them.
	_ "github.com/weblfe/beego-swagger/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 beego@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /
func main() {
	

	beego.Get("/swagger/*", swagger.Handler) // default

	beego.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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Handler = New()

Handler default

Functions

func New

func New(config ...Config) beego.FilterFunc

New returns custom handler

Types

type Config

type Config struct {
	// Enable deep linking for tags and operations, default is true
	DeepLinking bool

	// Controls the default expansion setting for the operations and tags.
	// 'list' (default, expands only the tags),
	// 'full' (expands the tags and operations),
	// 'none' (expands nothing)
	DocExpansion string

	// Configuration information for OAuth2, optional if using OAuth2
	OAuth *OAuthConfig

	// Custom OAuth redirect URL
	OAuth2RedirectUrl string

	// URL pointing to API definition
	URL string
}

Config stores SwaggerUI configuration variables

type OAuthConfig

type OAuthConfig struct {
	// application name, displayed in authorization popup
	AppName string

	// ID of the client sent to the OAuth2 Provider, default is clientId
	ClientId string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL