golang 整合 swagger

快速开始

安装相关依赖

go get -u github.com/swaggo/swag/cmd/swag@latest
go get github.com/swaggo/gin-swagger
go get github.com/swaggo/files

在main.go文件的main函数上加注解

package main

import (
	_ "golang/study/docs"
	"golang/study/myvalidator"
	"golang/study/routers"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	gs "github.com/swaggo/gin-swagger"
)

// gin-swagger middleware
// swagger embed files

// @title           Swagger Example API
// @version         1.0
// @description     This is a sample server celler server.
// @termsOfService  http://swagger.io/terms/

// @contact.name   API Support
// @contact.url    http://www.swagger.io/support
// @contact.email  support@swagger.io

// @license.name  Apache 2.0
// @license.url   http://www.apache.org/licenses/LICENSE-2.0.html

// @securityDefinitions.basic  BasicAuth
func main() {
   

	r := gin.Default()
	myvalidator.Init()

	routers.StudyRouterInit(r)
	routers.MysqlRouterInit(r)
	routers.ValidatorRouterInit(r)

	r.GET("/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))
	r.Run(":8000") // listen and serve on 0.0.0.0:8080
}

wagger主文件注解-通用API信息

注释 说明 是否必填 示例
title 应用程序的名称 必填 // @title Swagger Example API
version 提供应用程序API的版本。 必填 // @version 1.0
description 应用程序的简短描述。 // @description This is a sample server celler server.
tag.name 标签的名称 // @tag.name This is the name of the tag
tag.description 标签的描述 // @tag.description Cool Description
tag.docs.url 标签的外部文档的URL // @tag.docs.url https://example.com
tag.docs.description 标签的外部文档说明 // @tag.docs.description Best example documentation
termsOfService API的服务条款 // @termsOfService http://swagger.io/terms/
contact.name 公开的API的联系信息 // @contact.name API Support
contact.url 联系信息的URL。 必须采用网址格式 // @contact.url http://www.swagger.io/support
contact.email 联系人/组织的电子邮件地址。 必须采用电子邮件地址的格式 // @contact.email support@swagger.io
license.name 用于API的许可证名称 必填 // @license.name Apache 2.0
license.url 用于API的许可证的URL。 必须采用网址格式 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html
host 运行API的主机(主机名或IP地址)。 // @host localhost:8080
BasePath 运行API的基本路径 // @BasePath /api/v1
accept API 可以使用的 MIME 类型列表。 请注意,Accept 仅影响具有请求正文的操作,例如 POST、PUT 和 PATCH。 值必须如“Mime类型”中所述 // @accept json
produce API可以生成的MIME类型的列表。值必须如“Mime类型”中所述 // @produce json
query.collection.format 请求URI query里数组参数的默认格式:csv,multi,pipes,tsv,ssv。 如果未设置,则默认为csv。 // @query.collection.format multi
schemes 用空格分隔的请求的传输协议 // @schemes http https
x-name 扩展的键必须以x-开头,并且只能使用json值 // @
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值