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

How to share middleware among multiple services?

Open ofey404 opened this issue 2 years ago • 3 comments
trafficstars

Hello, I wonder how to share middleware like JwtBlockList among multiple services.

Given an API file like this, goctl would generate middleware under internal/ directory.

@server(
	jwt: Auth
	group: user
	middleware: JwtBlockList
)

Are there any API syntax could refer to a public reused middleware, like in pkg/middleware/jwtblocklist/

ofey404 avatar Dec 20 '22 09:12 ofey404

Add the middleware literally in your code.

kevwan avatar Dec 24 '22 13:12 kevwan

It is possible to achieve this with template modification. Goctl template command is extremely helpful goctl template init --home yourprojectpath/alternate.

You can overwrite yourprojectpath/alternate/api/main.tpl and inject your global middleware with server.Use(remote.Middleware). Don't forget to provide import statement and run go mod tidy after generation if required.

xcono avatar Apr 06 '23 17:04 xcono

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

github-actions[bot] avatar Apr 11 '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 13 '24 01:07 github-actions[bot]