defines the method to customize http server (#2171)

This commit is contained in:
sniperwzq
2023-03-11 23:15:00 +08:00
committed by GitHub
parent 211b9498ef
commit 3e093bf34e
2 changed files with 21 additions and 9 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/zeromicro/go-zero/rest/chain"
"github.com/zeromicro/go-zero/rest/handler"
"github.com/zeromicro/go-zero/rest/httpx"
"github.com/zeromicro/go-zero/rest/internal"
"github.com/zeromicro/go-zero/rest/internal/cors"
"github.com/zeromicro/go-zero/rest/router"
)
@@ -19,6 +20,9 @@ type (
// RunOption defines the method to customize a Server.
RunOption func(*Server)
// StartOption defines the method to customize http server.
StartOption func(svr *http.Server)
// A Server is a http server.
Server struct {
ngin *engine
@@ -112,8 +116,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Start starts the Server.
// Graceful shutdown is enabled by default.
// Use proc.SetTimeToForceQuit to customize the graceful shutdown period.
func (s *Server) Start() {
handleError(s.ngin.start(s.router))
func (s *Server) Start(opts ...StartOption) {
var startOption []internal.StartOption
for _, opt := range opts {
startOption = append(startOption, internal.StartOption(opt))
}
handleError(s.ngin.start(s.router, startOption...))
}
// Stop stops the Server.