Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions rest/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"sort"
"time"
Expand Down Expand Up @@ -37,6 +38,7 @@ type engine struct {
shedder load.Shedder
priorityShedder load.Shedder
tlsConfig *tls.Config
extListner net.Listener
}

func newEngine(c RestConf) *engine {
Expand Down Expand Up @@ -283,6 +285,10 @@ func (ng *engine) setUnsignedCallback(callback handler.UnsignedCallback) {
ng.unsignedCallback = callback
}

func (ng *engine) setExternalListener(l net.Listener) {
ng.extListner = l
}

func (ng *engine) signatureVerifier(signature signatureSetting) (func(chain.Chain) chain.Chain, error) {
if !signature.enabled {
return func(chn chain.Chain) chain.Chain {
Expand Down Expand Up @@ -328,6 +334,10 @@ func (ng *engine) start(router httpx.Router, opts ...StartOption) error {
return err
}

if ng.extListner != nil {
return internal.StartHttpWithListner(ng.extListner, router, ng.withTimeout())
}

// make sure user defined options overwrite default options
opts = append([]StartOption{ng.withTimeout()}, opts...)

Expand Down
11 changes: 10 additions & 1 deletion rest/internal/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"net/http"

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

func StartHttpWithListner(l net.Listener, handler http.Handler, opts ...StartOption) error {
return start("", 0, handler, func(svr *http.Server) error {
return svr.Serve(l)
}, opts...)
}

// StartHttp starts a http server.
func StartHttp(host string, port int, handler http.Handler, opts ...StartOption) error {
return start(host, port, handler, func(svr *http.Server) error {
Expand All @@ -35,9 +42,11 @@ func StartHttps(host string, port int, certFile, keyFile string, handler http.Ha
func start(host string, port int, handler http.Handler, run func(svr *http.Server) error,
opts ...StartOption) (err error) {
server := &http.Server{
Addr: fmt.Sprintf("%s:%d", host, port),
Handler: handler,
}
if len(host) > 0 {
server.Addr = fmt.Sprintf("%s:%d", host, port)
}
for _, opt := range opts {
opt(server)
}
Expand Down
8 changes: 8 additions & 0 deletions rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rest
import (
"crypto/tls"
"errors"
"net"
"net/http"
"path"
"time"
Expand Down Expand Up @@ -315,6 +316,13 @@ func WithUnsignedCallback(callback handler.UnsignedCallback) RunOption {
}
}

// WithUnsignedCallback returns a RunOption that with given unsigned callback set.
func WithExternalListener(listner net.Listener) RunOption {
return func(svr *Server) {
svr.ngin.setExternalListener(listner)
}
}

func handleError(err error) {
// ErrServerClosed means the server is closed manually
if err == nil || errors.Is(err, http.ErrServerClosed) {
Expand Down