feat: support serverless in rest (#5001)

Signed-off-by: kevin <wanjunfeng@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Kevin Wan
2025-07-13 00:00:52 +08:00
committed by GitHub
parent a71e56de52
commit c9ff6a10d3
4 changed files with 106 additions and 2 deletions

27
rest/serverless.go Normal file
View File

@@ -0,0 +1,27 @@
package rest
import "net/http"
// Serverless is a wrapper around Server that allows it to be used in serverless environments.
type Serverless struct {
server *Server
}
// NewServerless creates a new Serverless instance from the provided Server.
func NewServerless(server *Server) (*Serverless, error) {
// Ensure the server is built before using it in a serverless context.
// Why not call server.build() when serving requests,
// is because we need to ensure fail fast behavior.
if err := server.build(); err != nil {
return nil, err
}
return &Serverless{
server: server,
}, nil
}
// Serve handles HTTP requests by delegating them to the underlying Server instance.
func (s *Serverless) Serve(w http.ResponseWriter, r *http.Request) {
s.server.serve(w, r)
}