zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.32k stars 3.96k forks source link

Have considered adding @middleware at the handler level in goctl? #3428

Open smithyj opened 1 year ago

smithyj commented 1 year ago

Have you considered adding @middleware at the handler level in goctl?

smithyj commented 1 year ago

@kevwan

chenquan commented 1 year ago

In what scenarios do you need to use this?

kevwan commented 1 year ago

You can add middlewares in your AddRoute statements.

But would you please give the concrete scenarios that you want this feature?

smithyj commented 1 year ago

According to the current goctl pattern, I have to repeat the definition of the same group syntax block as shown in the example below, but the goctl idea plugin shows an error.

I encountered an issue while trying to repeat the definition of the group syntax block as indicated in the example, and the goctl idea plugin flagged it as an error.

@server (
    prefix: /captcha/v1
    group: generate
)
service captcha {
    @doc "生成图片验证码"
    @handler GenerateImage
    get /generate/image returns (GenerateImageResponse)
}

@server (
    prefix: /captcha/v1
    group: generate
    middleware: VerifyMiddleware
)
service captcha {
    @doc "生成短信验证码"
    @handler GenerateSms
    post /generate/sms (GenerateSmsRequest) returns (GenerateSmsResponse)

    @doc "生成邮箱验证码"
    @handler GenerateEmail
    post /generate/email (GenerateEmailRequest) returns (GenerateEmailResponse)

    @doc "生成滑动验证码"
    @handler GenerateSlide
    post /generate/slide (GenerateSlideRequest) returns (GenerateSlideResponse)
}

I believe that adding middleware configuration at the handler level would make the business definition more flexible, reduce code redundancy, and also meet the requirements of the idea plugin.

@server (
    prefix: /captcha/v1
    group: generate
)
service captcha {
    @doc "生成图片验证码"
    @handler GenerateImage
    get /generate/image returns (GenerateImageResponse)

    @doc "生成短信验证码"
    @handler GenerateSms
    @middleware: VerifyMiddleware
    post /generate/sms (GenerateSmsRequest) returns (GenerateSmsResponse)

    @doc "生成邮箱验证码"
    @handler GenerateEmail
    @middleware: VerifyMiddleware
    post /generate/email (GenerateEmailRequest) returns (GenerateEmailResponse)

    @doc "生成滑动验证码"
    @handler GenerateSlide
    @middleware: VerifyMiddleware
    post /generate/slide (GenerateSlideRequest) returns (GenerateSlideResponse)
}
smithyj commented 1 year ago

In fact, mainstream web frameworks in any language currently support global-level middleware, group-level middleware, and handler-level middleware. I hope this requirement can be included in the development plan as it will greatly benefit developers.

smithyj commented 1 year ago

@kevwan

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@kevwan

Mikaelemmmm commented 1 year ago

I think this approach is sufficient, if the idea plugin prompts an error, then it should be possible to fix it for the idea plugin

@server ( prefix: /captcha/v1 group: generate ) service captcha { @doc "生成图片验证码" @handler GenerateImage get /generate/image returns (GenerateImageResponse) }

@server ( prefix: /captcha/v1 group: generate middleware: VerifyMiddleware ) service captcha { @doc "生成短信验证码" @handler GenerateSms post /generate/sms (GenerateSmsRequest) returns (GenerateSmsResponse)

@doc "生成邮箱验证码"
@handler GenerateEmail
post /generate/email (GenerateEmailRequest) returns (GenerateEmailResponse)

@doc "生成滑动验证码"
@handler GenerateSlide
post /generate/slide (GenerateSlideRequest) returns (GenerateSlideResponse)

}

smithyj commented 1 year ago

@kevwan @Mikaelemmmm 如下所示,这种需求下,就很有必要推出 handler 级别 middleware

@server (
    prefix: /account/v1
    group: account
    middleware: AuthMiddleware
)
service account {
    @doc "获取用户信息"
    @handler GetInfo
    get /account/info returns (AccountInfoResponse)

    @doc "修改密码"
    @handler ModifyPassword
    @middleware: VerifyCatpchaMiddleware
    patch /account/password (ModifyPasswordRequest) returns (ModifyPasswordResponse)
}
Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@kevwan @Mikaelemmmm As shown below, under this requirement, it is necessary to introduce handler level middleware

@server (
    prefix: /account/v1
    group: account
    middleware: Auth Middleware
)
service account {
    @doc "Get user information"
    @handler GetInfo
    get /account/info returns (AccountInfoResponse)

    @doc "Change Password"
    @handler ModifyPassword
    @middleware: VerifyCatpchaMiddleware
    patch /account/password (ModifyPasswordRequest) returns (ModifyPasswordResponse)
}
smithyj commented 1 year ago

@kevwan @Mikaelemmmm

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@kevwan @Mikaelemmmm

github-actions[bot] commented 2 months ago

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