go-zero
go-zero copied to clipboard
Why v1.8.1 remove ServeHTTP method? This method works on serverless services
for example vercel(https://vercel.com/docs/functions/runtimes/go):
func Index(w http.ResponseWriter, r *http.Request) {
server.ServeHTTP(w, r)
}
It's only for test purpose.
The best practices is to create a rest.Server and addRoutes, then start the server.
I want to deploy go-zero rest services using serverless architecture, such as vervcel serverless. If the server has ServeHTTP method, I can directly connect to the vercel function and deploy the service on vercel.
You can look https://vercel.com/docs/functions/runtimes/go
Prior to version 1.8.1, go-zero services could be deployed on vercel, which is the best practice in serverless architecture.
I think this method is not only used for testing, it has a greater purpose. Frameworks like gin, echo, etc. all support ServeHTTP and can be connected to the vercel platform.
see this blog: https://www.minoic.top/%E5%9C%A8-vercel-%E4%B8%AD%E5%BB%BA%E7%AB%8B-go-gin-%E7%9A%84%E7%BD%91%E7%AB%99%E6%9C%8D%E5%8A%A1/
Got it. I'll investigate this and if necessary, I'll add it back.
But for the previous implementation, bindRoutes will be called for each request. It causes memory leak, and it's only used in test purpose.
Thanks for your feedback!
Ok, looking forward to ServeHTTP being added back
hi @jaronnie
Would you please do a code review and check if the PR #4896 meets the requirement? Thanks!
OK
can you create a branch in zeromicro/go-zero. Then I can go get to test in workflows
Would you please use it from https://github.com/kevwan/go-zero/tree/rest/feat-servehttp
I will use replace to solve.
replace (
github.com/zeromicro/go-zero => github.com/kevwan/go-zero v0.0.0-20250525082407-0d5bcc60fb34
)
You can see: https://jzero-admin-deploy-server.vercel.app/api/version
jzero-admin deployed repo: https://github.com/jaronnie/jzero-admin-deploy-server which support vercel and aliyun serverless platform.
jzero admin repo is: https://github.com/jzero-io/jzero-admin
How do you use it in serverless service? Would you please give me an example code?
You can see this file: https://github.com/jaronnie/jzero-admin-deploy-server/blob/main/api/client.go
I think I can write an example code in https://github.com/zeromicro/zero-examples
I've update the PR.
it looks easier now
Does it work for you?
after update, https://jzero-admin-deploy-server.vercel.app/api/version 404
Did you start the server?
before update the PR, it works for me. In serverless mode, just register handler to server then use server.Serve(w, r)
you should bind routes to router
You need to start the server, otherwise, options doesn't apply.
Also, bindRoutes for each request causes memory leak.
Will using the Start() method result in listening to the port? Can NewRestServer add an option to not listen to the port?
I'll start it and see if it works.
Unfortunately it doesn't work properly. I think I still need to add a NewRestServer with option. Only bindRoutes when starting. Or is there any better way?
add two methods:
// Serve is for serverless purpose.
// Don't use it when using the Server in regular HTTP server mode.
// see https://vercel.com/docs/functions/runtimes/go
func (s *Server) Serve(w http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(w, r)
}
// BindRoutes binds the routes to the server.
// It's for serverless purpose.
// You should call it before calling Serve().
func (s *Server) BindRoutes() error {
return s.ngin.bindRoutes(s.router)
}
usage:
func init() {
server = rest.MustNewServer(c.Rest.RestConf, rest.WithCustomCors(func(header http.Header) {
header.Set("Access-Control-Allow-Origin", "*")
header.Add("Access-Control-Allow-Headers", "X-Request-Id")
header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
}, nil, "*"))
logx.Must(server.BindRoutes())
}
func Index(w http.ResponseWriter, r *http.Request) {
server.Serve(w, r)
}
it works ok with jzero-admin service ! Do you think this is okay? If it's ok, I can add this method to your branch