忽略不在Golang路由器中的Swagger端点。

huangapple go评论94阅读模式
英文:

Ignoring swagger endpoints which are not in Golang router

问题

我正在使用Swagger来生成Golang中REST API的文档。我的项目中有多个微服务。其中一些API对所有服务都是通用的,所以我将它们保持为通用的。因此,我的Golang代码如下所示:

import othercontroller "github.com/controllerv2"
func Controller() {
	...

	// 版本 V1 gpd
	router.HandleFunc(utils.BasePath+"/entity", c.handleCreate).Methods(http.MethodPost)
	router.HandleFunc(utils.BasePath+"/entity/{id}", c.handleDelete).Methods(http.MethodDelete)

	othercontroller.AttachStatRoutes(utils.BasePath, router)
	router.Serve()
}

"othercontroller"中的API看起来像这样:

func AttachStatRoutes(basepath string, router Router) {
	router.HandleFunc(basepath+"/stats", getStatsHandler).Methods("GET")
}

func AttachHealthRoutes(basepath string, router Router) {
	router.HandleFunc(basepath+"/health", getHealthHandler).Methods("GET")
}

getStatsHandler API的Swagger定义如下:

// swagger:operation GET /stats  Stat
//
//
// ---
// produces:
// - application/json
// parameters:
// - name: ID
//   in: query
//   description: stats ID
//   required: false
//   type: string
// responses:
//   '200':
//     description: Stats response
//     schema:
//       "$ref": "#/definitions/StatsResponse"
//   '400':
//     description: Bad Request
//   '403':
//     description: Forbidden, you are not authorized
//   '500':
//     description: Error occurred while processing the request

func getStatsHandler(w http.ResponseWriter, r *http.Request) {
    // 在这里添加一些逻辑
}

AttachHealthRoutes()函数中的getHealthHandler API也位于同一个包中,并且具有类似的Swagger UI定义。

现在,当我生成Swagger时,我也会得到AttachHealthRoutes()函数中的API的Swagger UI,但我没有将其附加到路由器上。

所以我需要的是,Swagger只应该为附加到路由器上的API显示UI。我假设之所以会得到AttachHealthRoutes()函数中的getHealthHandler API的Swagger,是因为getHealthHandler与getStatHandler位于同一个包中。

请注意,我不能将它们放在不同的包中。

有人可以告诉我如何从Swagger UI中去除getHealthHandler API吗?

英文:

I am using swagger to generate documentation of REST APIs in Golang. I have multiple micro-services in my project. Some of the APIs are common to all the services so I have kept them common. So my Golang code looks like this:

import othercontroller "github.com/controllerv2"
func Controller() {
	...

	// version V1 gpd
	router.HandleFunc(utils.BasePath+"/entity", c.handleCreate).Methods(http.MethodPost)
	router.HandleFunc(utils.BasePath+"/entity/{id}", c.handleDelete).Methods(http.MethodDelete)

	othercontroller.AttachStatRoutes(utils.BasePath, router)
	router.Serve()
}

APIs in "othercontroller" looks something like this:


func AttachStatRoutes(basepath string, router Router) {
	router.HandleFunc(basepath+"/stats", getStatsHandler).Methods("GET")
}

func AttachHealthRoutes(basepath string, router Router) {
	router.HandleFunc(basepath+"/health", getHealthHandler).Methods("GET")
}

Swagger definition for getStatsHandler API looks like this

// swagger:operation GET /stats  Stat
//
//
// ---
// produces:
// - application/json
// parameters:
// - name: ID
//   in: query
//   description: stats ID
//   required: false
//   type: string
// responses:
//   '200':
//     description: Stats response
//     schema:
//       "$ref": "#/definitions/StatsResponse"
//   '400':
//     description: Bad Request
//   '403':
//     description: Forbidden, you are not authorized
//   '500':
//     description: Error occurred while processing the request

func getStatsHandler(w http.ResponseWriter, r *http.Request) {
    // some logic here
}

"getHealthHandler" API of AttachHealthRoutes() function is also in the same package and has similar swagger UI definition.

Now when I generate the swagger, I am also getting swagger UI for APIs which are in AttachHealthRoutes() function but I am not attaching it to the router.

So what I need is, swagger should only show me UI for APIs which are attached to routers.
What I am assuming is, I am getting the swagger for API of AttachHealthRoutes() becasue getHealthHandler is in the same package as that of getStatHandler.

Note that I can not keep them in different packages.

Can anyone tell me how can I get rid of getHealthHandler API from swagger UI?

答案1

得分: 1

@jmoney感谢你指引我正确的方向。我在其他地方搜索解决方案,但没有搜索到生成Swagger的地方。
确实有排除选项的选项。在这里找到了它们:

https://goswagger.io/generate/spec.html

以下命令对我有效:

//go:generate swagger generate spec -m -o ./swagger.json -x, --exclude-tag=Health
英文:

@jmoney thanks for pointing me in the right direction. I was searching for the solution everywhere else but not where the swagger was getting generated.
There are indeed options to exclude options. Found them here:

https://goswagger.io/generate/spec.html

Following command worked for me:

//go:generate swagger generate spec -m -o ./swagger.json -x, --exclude-tag=Health

huangapple
  • 本文由 发表于 2021年9月17日 17:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/69221236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定