mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
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:
27
rest/serverless.go
Normal file
27
rest/serverless.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user