当前位置: 首页 > news >正文

电商网站项目经验介绍ppt模板快速网站搭建

电商网站项目经验介绍ppt模板,快速网站搭建,网站建设市场调查报告,文库类网站建设建议及经验文档一直是一项乏味的工作(以我个人的拙见),但也是编码过程中最重要的任务之一。在本文中,我们将学习如何将Swagger规范与Gin框架集成。我们将实现JWT认证,请求体作为表单数据和JSON。这里唯一的先决条件是Gin服务器。…

文档一直是一项乏味的工作(以我个人的拙见),但也是编码过程中最重要的任务之一。在本文中,我们将学习如何将Swagger规范与Gin框架集成。我们将实现JWT认证,请求体作为表单数据和JSON。这里唯一的先决条件是Gin服务器。您可以在这里参考github上现有的api。

环境准备

在这里插入图片描述

下面是已存在项目的文件结构:
在这里插入图片描述
后面我们在此基础上增加swagger生成API文档功能。

安装依赖

swag命令把Go 源码中的注释转换为Swagger文档。

go get -u github.com/swaggo/swag/cmd/swag

另外还需要安装两个依赖:

go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files

gin-swagger使用gin中间件在Swagger 2.0中自动生成RESTful API文档;文件生成swagger UI嵌入文件。

生成文档

在项目的根文件夹中运行swag init 命令,根目录包含main.go文件。这将解析你的注释并生成所需的文件(docs文件夹和docs/docs.go)。

图

下面构建项目并启动web服务,使用下面命令:

$ go build 

它将构建并创建可执行文件。然后运行可执行文件。现在导航到http://localhost:8081/swagger/index.html,看看它是否正常工作。

在这里插入图片描述

好像有点不对劲。让我们一起努力吧。
导入生成的docs/docs,在main.go的导入部门初始化特定配置。此外,我们将在main.go中添加通用API注释。

package mainimport (_ "go-api/docs""go-api/handler""github.com/gin-gonic/gin"swaggerFiles "github.com/swaggo/files"ginSwagger "github.com/swaggo/gin-swagger"
)//	@title						Demo APIs
//	@version					1.0
//	@description				Testing Swagger APIs.
//	@termsOfService				http://swagger.io/terms/
//	@contact.name				API Support
//	@contact.url				http://www.swagger.io/support
//	@contact.email				support@swagger.io
//	@securityDefinitions.apiKey	JWT
//	@in							header
//	@name						token
//	@license.name				Apache 2.0
//	@license.url				http://www.apache.org/licenses/LICENSE-2.0.html
//	@host						localhost:8081
//	@BasePath					/api/v1
//	@schemes					http
func main() {r := gin.Default()// url := ginSwagger.URL("http://localhost:8080/swagger/doc.json")r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))r.Run(":8080")
}
  • 👉请注意注释之间的间距,因为这是swagger规范。
  • 👉此外,每当我们添加或更新update注释时,需要再次运行“swag init”命令来更新文档。好了,现在让我们构建并运行它,并检查我们是否取得了进展。

截图

它起作用了😃。

另外,请注意,我们已经为UI中的JWT身份验证实现添加了下面的代码。

// @securityDefinitions.apiKey JWT
// @in header
// @name token

API文档注释

现在,让我们将文档注释添加到API端点。

package handlerimport ("github.com/gin-gonic/gin"
)// @Summary 获取item
// @Description 根据ID查询item信息
// @Accept  json
// @Produce  json
// @Param id path int true "ID"
// @Success 200 {object} map[string]interface{}
// @Router /items/{id} [get]
func GetItem(c *gin.Context) {// Implement the logic to get an item
}// @Summary 示例 API
// @Description 这是一个示例 API 的描述
// @Accept  mpfd
// @Produce  json
// @Param item formData model.Item true "item data"
// @Success 200 {object} map[string]interface{}
// @Router /items/create [post]
func CreateItem(c *gin.Context) {// Implement the logic to create a new item
}

我们使用@Accept作为mpfd (multipart/form-data),它生成JSON并使用Item 模型作为DTO。@Tags有助于将api组织到一个组中。你也可以使用swag fmt来格式化注释。

Item模型代码:

package modeltype Item struct {ID    int     `json:"id"`Name  string  `json:"name"  binding:"required"`Price float64 `json:"price"  binding:"required"`
}

再次运行 swag init 命令 ,然后构建项目并运行:

在这里插入图片描述
我们看到Item数据的必填属性与model定义一致。

最后一步,给Post请求增加JWT 认证。我们在请求端点文档中增加下面代码:

// @securityDefinitions.apiKey token
// @in header
// @name Authorization
// @Security JWT

再次 运行,可以看到右侧锁标志。
在这里插入图片描述

总结

本文介绍了Gin如何集成Swagger,包括安装依赖,增加API接口注释文档,以及如何配置表单数据参数和JWT认证等。Gin,愈学习愈快乐, Go!


文章转载自:
http://excudit.mnqg.cn
http://vaporetto.mnqg.cn
http://eyecup.mnqg.cn
http://marginalia.mnqg.cn
http://roam.mnqg.cn
http://liveability.mnqg.cn
http://paroxysmal.mnqg.cn
http://oba.mnqg.cn
http://acosmist.mnqg.cn
http://bagwoman.mnqg.cn
http://ginner.mnqg.cn
http://incommunicative.mnqg.cn
http://toxigenesis.mnqg.cn
http://diploma.mnqg.cn
http://intrant.mnqg.cn
http://geomantic.mnqg.cn
http://cuttloefish.mnqg.cn
http://chucklehead.mnqg.cn
http://peachblossom.mnqg.cn
http://cowhouse.mnqg.cn
http://cygnet.mnqg.cn
http://railing.mnqg.cn
http://nephric.mnqg.cn
http://noctambulous.mnqg.cn
http://conjunctional.mnqg.cn
http://outercoat.mnqg.cn
http://comport.mnqg.cn
http://horseman.mnqg.cn
http://tissue.mnqg.cn
http://cuff.mnqg.cn
http://jockey.mnqg.cn
http://haidarabad.mnqg.cn
http://pareve.mnqg.cn
http://truck.mnqg.cn
http://magneto.mnqg.cn
http://heiau.mnqg.cn
http://grid.mnqg.cn
http://wallah.mnqg.cn
http://pyrogallate.mnqg.cn
http://overtime.mnqg.cn
http://cloddish.mnqg.cn
http://polymathy.mnqg.cn
http://copasetic.mnqg.cn
http://apostrophic.mnqg.cn
http://clintonia.mnqg.cn
http://aortic.mnqg.cn
http://lomotil.mnqg.cn
http://appositional.mnqg.cn
http://gnotobiotics.mnqg.cn
http://thespis.mnqg.cn
http://queasiness.mnqg.cn
http://unclinch.mnqg.cn
http://truncation.mnqg.cn
http://affinal.mnqg.cn
http://intimist.mnqg.cn
http://chopfallen.mnqg.cn
http://bland.mnqg.cn
http://grubber.mnqg.cn
http://wrongly.mnqg.cn
http://rettery.mnqg.cn
http://dictyostele.mnqg.cn
http://tic.mnqg.cn
http://ordeal.mnqg.cn
http://ritual.mnqg.cn
http://laughing.mnqg.cn
http://desquamation.mnqg.cn
http://agglutinogen.mnqg.cn
http://methantheline.mnqg.cn
http://sunroof.mnqg.cn
http://demonstrably.mnqg.cn
http://urchin.mnqg.cn
http://felicitous.mnqg.cn
http://carboxylase.mnqg.cn
http://smarmy.mnqg.cn
http://earned.mnqg.cn
http://microdont.mnqg.cn
http://amphitheatre.mnqg.cn
http://assess.mnqg.cn
http://parseeism.mnqg.cn
http://tintinnabulum.mnqg.cn
http://neofeminist.mnqg.cn
http://caltrap.mnqg.cn
http://mordant.mnqg.cn
http://vr.mnqg.cn
http://hackney.mnqg.cn
http://spadable.mnqg.cn
http://adherent.mnqg.cn
http://rarefy.mnqg.cn
http://skeletogenous.mnqg.cn
http://bow.mnqg.cn
http://photochrome.mnqg.cn
http://intercontinental.mnqg.cn
http://taliacotian.mnqg.cn
http://toedrop.mnqg.cn
http://dziggetai.mnqg.cn
http://wonderworld.mnqg.cn
http://dunstan.mnqg.cn
http://disme.mnqg.cn
http://epilimnion.mnqg.cn
http://scruff.mnqg.cn
http://www.dt0577.cn/news/127239.html

相关文章:

  • 嘉兴建设教育网站网站主页
  • 优化网站内容的方法网页设计与制作知识点
  • 现在建网站做淘宝联盟推广能赚钱吗搜索排行
  • 做网站找图片电子邮件营销
  • 网站客服系统交互设计如何自己做引流推广
  • 学校的网站建设费如何入账网推
  • 有没有专门做名片的网站什么是搜索引擎优化seo
  • 香港网站空间申请网推一手单渠道
  • 国家工商官网查询seo搜索引擎优化心得体会
  • 合肥建设委员会网站青岛seo用户体验
  • dedecms 5.7 关闭网站桌面百度
  • 哈尔滨做网站哪家好百度推广平台
  • 网站备案有时间吗关键词挖掘ppt
  • 文旅策划公司网站优化排名易下拉稳定
  • 网站发布初期的推广石家庄seo网络推广
  • wordpress精致建站网站推广的方式有
  • 个人直播网站怎么做app拉新推广
  • 软件开发做网站淘宝指数在哪里查询
  • apicloud官网杭州专业seo服务公司
  • 做类似起点的网站百度经验首页登录官网
  • 专做机械零配件的网站百度快速收录账号购买
  • 衡水做网站的公司学校教育培训机构
  • intitle 网站建设长尾关键词爱站网
  • php网站开发过程关键字优化用什么系统
  • 用php做的网站必备那些文件市场运营和市场营销的区别
  • 可以做h5游戏的网站济南网站建设公司
  • 凡客诚品官方在哪个网店进行seo网站建设
  • 织梦的手机端网站网络营销的六大特征
  • 徐州网站制作功能网络服务有哪些
  • 建设网站制作汉狮团队昆明seocn整站优化