Ask a Question

Ignoring swagger endpoints which are not in Golang router

Vallabh
Occasional Visitor

Ignoring swagger endpoints which are not in Golang router

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?

Just for reference, the question is also posted on this forum: https://stackoverflow.com/posts/69221236

cancel
Showing results for 
Search instead for 
Did you mean: