Skip to content

Commit 9e9f5f4

Browse files
lrobotkevwan
authored andcommitted
feat: reset engine can call rest.WithExternalListener to use external listener for start engine
1 parent 9c47862 commit 9e9f5f4

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

rest/engine.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/tls"
55
"errors"
66
"fmt"
7+
"net"
78
"net/http"
89
"sort"
910
"time"
@@ -37,6 +38,7 @@ type engine struct {
3738
shedder load.Shedder
3839
priorityShedder load.Shedder
3940
tlsConfig *tls.Config
41+
extListner net.Listener
4042
}
4143

4244
func newEngine(c RestConf) *engine {
@@ -283,6 +285,10 @@ func (ng *engine) setUnsignedCallback(callback handler.UnsignedCallback) {
283285
ng.unsignedCallback = callback
284286
}
285287

288+
func (ng *engine) setExternalListener(l net.Listener) {
289+
ng.extListner = l
290+
}
291+
286292
func (ng *engine) signatureVerifier(signature signatureSetting) (func(chain.Chain) chain.Chain, error) {
287293
if !signature.enabled {
288294
return func(chn chain.Chain) chain.Chain {
@@ -328,6 +334,10 @@ func (ng *engine) start(router httpx.Router, opts ...StartOption) error {
328334
return err
329335
}
330336

337+
if ng.extListner != nil {
338+
return internal.StartHttpWithListner(ng.extListner, router, ng.withTimeout())
339+
}
340+
331341
// make sure user defined options overwrite default options
332342
opts = append([]StartOption{ng.withTimeout()}, opts...)
333343

rest/internal/starter.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"net"
78
"net/http"
89

910
"github.com/zeromicro/go-zero/core/logx"
@@ -16,6 +17,12 @@ const probeNamePrefix = "rest"
1617
// StartOption defines the method to customize http.Server.
1718
type StartOption func(svr *http.Server)
1819

20+
func StartHttpWithListner(l net.Listener, handler http.Handler, opts ...StartOption) error {
21+
return start("", 0, handler, func(svr *http.Server) error {
22+
return svr.Serve(l)
23+
}, opts...)
24+
}
25+
1926
// StartHttp starts a http server.
2027
func StartHttp(host string, port int, handler http.Handler, opts ...StartOption) error {
2128
return start(host, port, handler, func(svr *http.Server) error {
@@ -35,9 +42,11 @@ func StartHttps(host string, port int, certFile, keyFile string, handler http.Ha
3542
func start(host string, port int, handler http.Handler, run func(svr *http.Server) error,
3643
opts ...StartOption) (err error) {
3744
server := &http.Server{
38-
Addr: fmt.Sprintf("%s:%d", host, port),
3945
Handler: handler,
4046
}
47+
if len(host) > 0 {
48+
server.Addr = fmt.Sprintf("%s:%d", host, port)
49+
}
4150
for _, opt := range opts {
4251
opt(server)
4352
}

rest/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rest
33
import (
44
"crypto/tls"
55
"errors"
6+
"net"
67
"net/http"
78
"path"
89
"time"
@@ -315,6 +316,13 @@ func WithUnsignedCallback(callback handler.UnsignedCallback) RunOption {
315316
}
316317
}
317318

319+
// WithUnsignedCallback returns a RunOption that with given unsigned callback set.
320+
func WithExternalListener(listner net.Listener) RunOption {
321+
return func(svr *Server) {
322+
svr.ngin.setExternalListener(listner)
323+
}
324+
}
325+
318326
func handleError(err error) {
319327
// ErrServerClosed means the server is closed manually
320328
if err == nil || errors.Is(err, http.ErrServerClosed) {

0 commit comments

Comments
 (0)