Golang iris API文档生成器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangxiaodong88/article/details/88776430

官方文档

https://github.com/betacraft/yaag

这里有各个 框架的 使用方式

安装使用

1、 下载YAAG 中间件

go get github.com/betacraft/yaag/...

2、引入对应的包

Import github.com/betacraft/yaag/yaag
Import github.com/betacraft/yaag/irisyaag

3、初始化

Initialize yaag yaag.Init(&yaag.Config(On: true, DocTile: "Iris", DocPath: "apidoc.html"))

4、注册中间件

Register yaag middleware like app.Use(irisyaag.New())

具体使用代码

package main

import (
	"adminInterface/handler"
	"github.com/betacraft/yaag/irisyaag"
	"github.com/betacraft/yaag/yaag"
	"github.com/kataras/iris"
	"log"

	"github.com/kataras/iris/middleware/logger"
	"github.com/kataras/iris/middleware/recover"
)

func main() {
	app := iris.New()

	// yaag api 为文档生成器
	yaag.Init(&yaag.Config{
		On:       true,
		DocTitle: "Iris",
		DocPath:  "apidoc.html",
		BaseUrls: map[string]string{"Production": "", "Staging": ""},
	})
	app.Use(irisyaag.New())
	////
	app.Logger().SetLevel("debug")
	// Optionally, add two built'n handlers
	// that can recover from any http-relative panics
	// and log the requests to the terminal.
	app.Use(recover.New())
	app.Use(logger.New())

	// Method:   GET
	// Resource: http://localhost:8080
	app.Handle("GET", "/", func(ctx iris.Context) {
		ctx.HTML("<h1>Welcome</h1>")
	})

	// same as app.Handle("GET", "/ping", [...])
	// Method:   GET
	// Resource: http://localhost:8080/ping
	app.Get("/ping", func(ctx iris.Context) {
		ctx.WriteString("pong")
	})

	// Method:   GET
	// Resource: http://localhost:8080/hello
	app.Get("/hello", func(ctx iris.Context) {
		ctx.JSON(iris.Map{"message": "Hello Iris!"})
	})
	app.Post("/myDemo", func(ctx iris.Context) {
		log.Printf("你好")
		//var user bodyTest.User
		//if err := ctx.ReadJSON(&user); err != nil {
		//	// Handle error.
		//}
		//log.Println(user)
		//log.Println(user.Name)
		//log.Println(user.Age)
		aa := ctx.GetHeader("name")
		log.Println(aa)

		ctx.JSON(iris.Map{"message": "Hello MyDemo!"})
	})
	handler.App(app)
	// http://localhost:8080
	// http://localhost:8080/ping
	// http://localhost:8080/hello
	app.Run(iris.Addr(":8089"), iris.WithoutServerError(iris.ErrServerClosed))
}

操作 查看效果

启动 main.go 文件, 访问家口 /, /hello ./ myDemo
项目目录下面会生成这样的两个文件

在这里插入图片描述
在 浏览器打开 apidoc.html 看效果

如图
在这里插入图片描述

这样就生成了 api 文档并返回对应的值, 请求响应的值都可以看到。

猜你喜欢

转载自blog.csdn.net/yangxiaodong88/article/details/88776430