Closed hmajid2301 closed 3 years ago
Routes are added to groups, so I'm not sure I understand what you mean by:
define an API Group like /api/v1/ or /v1/ to all paths
Aren't you refering to OpenAPI tags instead ?
Sorry I meant in an openapi spec (v2) I can specify something like
basePath: "/api/v1"
And I cannot remember exactly how you can do that in V3.
But I was wondering is there any easy way with add say prepend /api/v1
to all endpoints ?
The Swagger v2 documentation regarding that subject is here: https://swagger.io/docs/specification/2-0/api-host-and-base-path/, and the equivalent for OpenAPI 3.x is https://swagger.io/docs/specification/api-host-and-base-path/.
OpenAPI v3 introduced the Server
object. With fizz, you can see that the root object defines a Servers
field.
To set a list of servers, see the following snippet:
f := fizz.NewFromEngine(r)
if url != "" {
f.Generator().SetServers([]*openapi.Server{
&openapi.Server{
URL: url,
Description: description,
},
})
}
The Server
type is defined here.
Thanks but if I wanted the route in the application itself to change, will this work ? or do I need to do it a the group level like so
routes(fizz.Group("/api/v1/market", "market", "Your daily dose of freshness"))
Your questions are confusing. You started talking about router groups, and then switched to mention thebasePath
property of Swagger.
To clarify, router groups are managed by the underlying router, Gin. The name of the groups is used to construct a list of tags in the OpenAPI spec. And finally, the basePath
/URL server is just a static prefix.
So I think what your're looking for is using router groups. See the following example adapted from the README:
f := fizz.New()
api := f.Group("/api", "API", "API group")
v1 := api.Group("/v1", "v1 routes", "Version 1")
// /api/v1/{id}
bar.GET("/:id", nil, tonic.Handler(MyHandler, 200))
Does it answer your question ?
Yeh sorry for the confusion, I was basically asking how can we replicate the basepath via gin using Fizz. I didn't phrase it properly.
Is there an easy way to define an API Group like
/api/v1/
or/v1/
to all paths ? Or do we have to simply add to every group in the "routes" filethanks