defines the method to customize http server (#2171)

master
sniperwzq 2 years ago committed by GitHub
parent 211b9498ef
commit 3e093bf34e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -301,21 +301,25 @@ func (ng *engine) signatureVerifier(signature signatureSetting) (func(chain.Chai
}, nil }, nil
} }
func (ng *engine) start(router httpx.Router) error { func (ng *engine) start(router httpx.Router, opts ...internal.StartOption) error {
if err := ng.bindRoutes(router); err != nil { if err := ng.bindRoutes(router); err != nil {
return err return err
} }
opts = append(opts, ng.withTimeout())
if len(ng.conf.CertFile) == 0 && len(ng.conf.KeyFile) == 0 { if len(ng.conf.CertFile) == 0 && len(ng.conf.KeyFile) == 0 {
return internal.StartHttp(ng.conf.Host, ng.conf.Port, router, ng.withTimeout()) return internal.StartHttp(ng.conf.Host, ng.conf.Port, router, opts...)
} }
return internal.StartHttps(ng.conf.Host, ng.conf.Port, ng.conf.CertFile, opts = append(opts, func(svr *http.Server) {
ng.conf.KeyFile, router, func(svr *http.Server) {
if ng.tlsConfig != nil { if ng.tlsConfig != nil {
svr.TLSConfig = ng.tlsConfig svr.TLSConfig = ng.tlsConfig
} }
}, ng.withTimeout()) })
return internal.StartHttps(ng.conf.Host, ng.conf.Port, ng.conf.CertFile,
ng.conf.KeyFile, router, opts...)
} }
func (ng *engine) use(middleware Middleware) { func (ng *engine) use(middleware Middleware) {

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

Loading…
Cancel
Save