All Projects → swaggo → Gin Swagger

swaggo / Gin Swagger

Licence: mit
gin middleware to automatically generate RESTful API documentation with Swagger 2.0.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Gin Swagger

Ginrpc
gin auto binding,grpc, and annotated route,gin 注解路由, grpc,自动参数绑定工具
Stars: ✭ 157 (-92.15%)
Mutual labels:  swagger, middleware, gin
Go Gin Api
基于 Gin 进行模块化设计的 API 框架,封装了常用功能,使用简单,致力于进行快速的业务研发。比如,支持 cors 跨域、jwt 签名验证、zap 日志收集、panic 异常捕获、trace 链路追踪、prometheus 监控指标、swagger 文档生成、viper 配置文件解析、gorm 数据库组件、gormgen 代码生成工具、graphql 查询语言、errno 统一定义错误码、gRPC 的使用 等等。
Stars: ✭ 730 (-63.52%)
Mutual labels:  swagger, gin
Snake
🐍 一款小巧的基于Go构建的开发框架,可以快速构建API服务或者Web网站进行业务开发,遵循SOLID设计原则
Stars: ✭ 615 (-69.27%)
Mutual labels:  swagger, gin
Gin Glog
Gin middleware to use glog
Stars: ✭ 53 (-97.35%)
Mutual labels:  middleware, gin
Go Admin
基于Gin + Vue + Element UI的前后端分离权限管理系统脚手架(包含了:多租户的支持,基础用户管理功能,jwt鉴权,代码生成器,RBAC资源控制,表单构建,定时任务等)3分钟构建自己的中后台项目;文档:https://doc.go-admin.dev Demo: https://www.go-admin.dev Antd beta版本:https://preview.go-admin.dev
Stars: ✭ 5,439 (+171.81%)
Mutual labels:  swagger, gin
Go Gin Example
An example of gin
Stars: ✭ 4,992 (+149.48%)
Mutual labels:  swagger, gin
Gin Stats
Gin's middleware for request stats
Stars: ✭ 24 (-98.8%)
Mutual labels:  middleware, gin
golang-gin-restfulAPI-example-app
An example Golang Restful API with [Gin, MongoDB, gin-jwt, gin-sessions, gin-authz, gin-swagger, validate.v9, casbin, go-ini]
Stars: ✭ 104 (-94.8%)
Mutual labels:  gin, gin-swagger
Speedbump
A Redis-backed rate limiter in Go
Stars: ✭ 107 (-94.65%)
Mutual labels:  middleware, gin
Gin Cors
Cross Origin Resource Sharing middleware for gin-gonic
Stars: ✭ 107 (-94.65%)
Mutual labels:  middleware, gin
Gin
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
Stars: ✭ 53,971 (+2597.2%)
Mutual labels:  middleware, gin
Meiam.system
.NET 5 / .NET Core 3.1 WebAPI + Vue 2.0 + RBAC 企业级前后端分离权限框架
Stars: ✭ 340 (-83.01%)
Mutual labels:  swagger, middleware
gin-swagger
DRY templates for go-swagger
Stars: ✭ 79 (-96.05%)
Mutual labels:  swagger, gin
Swagger Express Middleware
Swagger 2.0 middlware and mocks for Express.js
Stars: ✭ 543 (-72.86%)
Mutual labels:  swagger, middleware
Go-Gin-Api
基于golang开源框架 gin封装的api框架
Stars: ✭ 42 (-97.9%)
Mutual labels:  swagger, gin
Reitit
A fast data-driven router for Clojure/Script
Stars: ✭ 892 (-55.42%)
Mutual labels:  swagger, middleware
Gzip
💾 Golang gzip middleware for Gin and net/http | Golang gzip中间件,支持Gin和net/http,开箱即用同时可定制
Stars: ✭ 113 (-94.35%)
Mutual labels:  middleware, gin
ginhelper
gin framework helper
Stars: ✭ 16 (-99.2%)
Mutual labels:  gin, gin-middleware
logging
mod: zap logging in golang
Stars: ✭ 44 (-97.8%)
Mutual labels:  gin, gin-middleware
Go Tgbot
Golang telegram bot API wrapper, session-based router and middleware
Stars: ✭ 90 (-95.5%)
Mutual labels:  swagger, middleware

gin-swagger

gin middleware to automatically generate RESTFUL API documentation with Swagger 2.0.

Build Status Codecov branch Go Report Card GoDoc Release

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 at your Go project root path(for instance ~/root/go-peoject-name), Swag will parse comments and generate required files(docs folder and docs/doc.go) at ~/root/go-peoject-name/docs.
swag init
  1. Download gin-swagger by using:
go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files

Import following in your code:

import "github.com/swaggo/gin-swagger" // gin-swagger middleware
import "github.com/swaggo/files" // swagger embed files

Canonical example:

Now assume you have implemented a simple api as following:

// A get function which returns a hello world string by json
func Helloworld(g *gin.Context)  {
	g.JSON(http.StatusOK,"helloworld")
}

So how to use gin-swagger on api above? Just follow the following guide.

  1. Add Comments for apis and main function with gin-swagger rules like following:
// @BasePath /api/v1

// PingExample godoc
// @Summary ping example
// @Schemes
// @Description do ping
// @Tags example
// @Accept json
// @Produce json
// @Success 200 {string} Helloworld
// @Router /example/helloworld [get]
func Helloworld(g *gin.Context)  {
	g.JSON(http.StatusOK,"helloworld")
}
  1. Use swag init command to generate a docs, docs generated will be stored at docs/.
  2. import the docs like this: I assume your project named github.com/go-project-name/docs.
import (
   docs "github.com/go-project-name/docs"
)
  1. build your application and after that, go to http://localhost:8080/swagger/index.html ,you to see your Swagger UI.

  2. The full code and folder relatives here:

package main

import (
   "github.com/gin-gonic/gin"
   docs "github.com/go-project-name/docs"
   swaggerfiles "github.com/swaggo/files"
   ginSwagger "github.com/swaggo/gin-swagger"
   "net/http"
)
// @BasePath /api/v1

// PingExample godoc
// @Summary ping example
// @Schemes
// @Description do ping
// @Tags example
// @Accept json
// @Produce json
// @Success 200 {string} Helloworld
// @Router /example/helloworld [get]
func Helloworld(g *gin.Context)  {
   g.JSON(http.StatusOK,"helloworld")
}

func main()  {
   r := gin.Default()
   docs.SwaggerInfo.BasePath = "/api/v1"
   v1 := r.Group("/api/v1")
   {
      eg := v1.Group("/example")
      {
         eg.GET("/helloworld",Helloworld)
      }
   }
   r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
   r.Run(":8080")

}

Demo project tree, swag init is run at relative .

.
├── docs
│   ├── docs.go
│   ├── swagger.json
│   └── swagger.yaml
├── go.mod
├── go.sum
└── main.go

Configuration

You can configure Swagger using different configuration options

func main() {
	r := gin.New()

	ginSwagger.WrapHandler(swaggerFiles.Handler,
		ginSwagger.URL("http://localhost:8080/swagger/doc.json"),
		ginSwagger.DefaultModelsExpandDepth(-1))

	r.Run()
}
Option Type Default Description
URL string "doc.json" URL pointing to API definition
DocExpantion string "list" Controls the default expansion setting for the operations and tags. It can be 'list' (expands only the tags), 'full' (expands the tags and operations) or 'none' (expands nothing).
DeepLinking bool true If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information.
DefaultModelsExpandDepth int 1 Default expansion depth for models (set to -1 completely hide the models).
InstanceName string "swagger" The instance name of the swagger document. If multiple different swagger instances should be deployed on one gin router, ensure that each instance has a unique name (use the --instanceName parameter to generate swagger documents with swag init).
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].