go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

Does go-zero support setting middleware api by api?

Open kentliuqiao opened this issue 2 years ago • 7 comments
trafficstars

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?

kentliuqiao avatar Apr 20 '23 17:04 kentliuqiao

It's supported in api syntax. Check the docs.

kevwan avatar Apr 21 '23 00:04 kevwan

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

hjxisking avatar Apr 21 '23 07:04 hjxisking

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)

kentliuqiao avatar Apr 21 '23 08:04 kentliuqiao

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.

kevwan avatar Apr 21 '23 13:04 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)

You can use different group for middlewares.

kevwan avatar Apr 21 '23 13:04 kevwan

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

kentliuqiao avatar Apr 22 '23 06:04 kentliuqiao

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Apr 28 '24 01:04 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jul 28 '24 01:07 github-actions[bot]