Skip to content

Why v1.8.1 remove ServeHTTP method? This method works on serverless services #4892

@jaronnie

Description

@jaronnie
Contributor
Image

for example vercel(https://vercel.com/docs/functions/runtimes/go):

func Index(w http.ResponseWriter, r *http.Request) {
	server.ServeHTTP(w, r)
}

Activity

kevwan

kevwan commented on May 24, 2025

@kevwan
Contributor

It's only for test purpose.

The best practices is to create a rest.Server and addRoutes, then start the server.

jaronnie

jaronnie commented on May 24, 2025

@jaronnie
ContributorAuthor

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

jaronnie

jaronnie commented on May 24, 2025

@jaronnie
ContributorAuthor

Prior to version 1.8.1, go-zero services could be deployed on vercel, which is the best practice in serverless architecture.

jaronnie

jaronnie commented on May 24, 2025

@jaronnie
ContributorAuthor

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.

kevwan

kevwan commented on May 24, 2025

@kevwan
Contributor

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!

self-assigned this
on May 24, 2025
added
featureMarks an issue or PR as related to a new feature.
on May 24, 2025
jaronnie

jaronnie commented on May 25, 2025

@jaronnie
ContributorAuthor

Ok, looking forward to ServeHTTP being added back

kevwan

kevwan commented on May 25, 2025

@kevwan
Contributor

hi @jaronnie

Would you please do a code review and check if the PR #4896 meets the requirement? Thanks!

jaronnie

jaronnie commented on May 25, 2025

@jaronnie
ContributorAuthor

OK

jaronnie

jaronnie commented on May 25, 2025

@jaronnie
ContributorAuthor

can you create a branch in zeromicro/go-zero. Then I can go get to test in workflows

kevwan

kevwan commented on May 25, 2025

@kevwan
Contributor
jaronnie

jaronnie commented on May 25, 2025

@jaronnie
ContributorAuthor

I will use replace to solve.

replace (
	github.com/zeromicro/go-zero => github.com/kevwan/go-zero v0.0.0-20250525082407-0d5bcc60fb34
)

13 remaining items

kevwan

kevwan commented on May 26, 2025

@kevwan
Contributor

You need to start the server, otherwise, options doesn't apply.

Also, bindRoutes for each request causes memory leak.

jaronnie

jaronnie commented on May 26, 2025

@jaronnie
ContributorAuthor

Will using the Start() method result in listening to the port? Can NewRestServer add an option to not listen to the port?

jaronnie

jaronnie commented on May 26, 2025

@jaronnie
ContributorAuthor

I'll start it and see if it works.

jaronnie

jaronnie commented on May 26, 2025

@jaronnie
ContributorAuthor

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?

jaronnie

jaronnie commented on May 26, 2025

@jaronnie
ContributorAuthor

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

changed the title [-]Why v1.8.1 remove ServeHTTP method? This method works on cloud computing platforms[/-] [+]Why v1.8.1 remove ServeHTTP method? This method works on serverless services[/+] on May 26, 2025
kevwan

kevwan commented on May 26, 2025

@kevwan
Contributor

Thanks for your feedback!

But I think it's better not to expose BindRoutes. Let me think it over and get back to you.

jaronnie

jaronnie commented on May 26, 2025

@jaronnie
ContributorAuthor

OK. Looking forward to your solution.

linked a pull request that will close this issuefeat: support serverless in rest #5001on Jul 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

featureMarks an issue or PR as related to a new feature.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @kevwan@jaronnie

    Issue actions

      Why v1.8.1 remove ServeHTTP method? This method works on serverless services · Issue #4892 · zeromicro/go-zero