go-zero
go-zero copied to clipboard
Does go-zero support setting middleware api by api?
In a .api config file, it has many api endpoints, which is divided into a few groups. Now I want to add middlewares for some specific api endpoints. Is there any solution that I can do it without change the group?
It's supported in api syntax. Check the docs.
like this?
@server (
group: api
prefix: v1
)
service xxx-api {
@handler foo
get /foo returns(Resp)
@handler bar
get /bar returns(Resp)
}
@server (
group: api
prefix: v1
middleware: MyMiddleware
)
service xxx-api {
@handler withMiddleware
get /withMiddleware returns(Resp)
}
Is there a better way? @kevwan
It's supported in api syntax. Check the docs.↳
I'm new to go-zero, so please forgive me if I missed something. I can only find this link that has api file specs: https://go-zero.dev/docs/advance/api-coding/. Unfortunately I cannot find any information about middlewares setting on a specific api endpoint. Also I have tried to add a middleware on a api endpoint, but goctl cannot parse this format.
@handler listNotice
middleware: SomeMiddleware
get /notice/list (NoticeListReq) returns (NoticeListResp)
like this?
@server ( group: api prefix: v1 ) service xxx-api { @handler foo get /foo returns(Resp) @handler bar get /bar returns(Resp) } @server ( group: api prefix: v1 middleware: MyMiddleware ) service xxx-api { @handler withMiddleware get /withMiddleware returns(Resp) }Is there a better way? @kevwan
Yes, you're right.
It's supported in api syntax. Check the docs.↳
I'm new to go-zero, so please forgive me if I missed something. I can only find this link that has api file specs: https://go-zero.dev/docs/advance/api-coding/. Unfortunately I cannot find any information about middlewares setting on a specific api endpoint. Also I have tried to add a middleware on a api endpoint, but goctl cannot parse this format.
@handler listNotice middleware: SomeMiddleware get /notice/list (NoticeListReq) returns (NoticeListResp)
You can use different group for middlewares.
Thanks for the reply. Have we considered supporting api endpoint's level middleware? e.g. we can add any middleware on a http handler. With this feature, we don't have to change the group dividing everytime we want to add a new middleware on a api endpoint
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.