mirror of
https://github.com/labstack/echo.git
synced 2025-06-04 23:37:45 +02:00
Mention about custom listener
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
49321f5829
commit
069e80b9e0
29
echo.go
29
echo.go
@ -562,27 +562,30 @@ func (e *Echo) startTLS(address string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StartServer starts a custom http server.
|
// StartServer starts a custom http server.
|
||||||
func (e *Echo) StartServer(s *http.Server) error {
|
func (e *Echo) StartServer(s *http.Server) (err error) {
|
||||||
// Setup
|
// Setup
|
||||||
e.colorer.SetOutput(e.Logger.Output())
|
e.colorer.SetOutput(e.Logger.Output())
|
||||||
s.Handler = e
|
s.Handler = e
|
||||||
s.ErrorLog = e.stdLogger
|
s.ErrorLog = e.stdLogger
|
||||||
|
|
||||||
l, err := net.Listen("tcp", s.Addr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if s.TLSConfig == nil {
|
if s.TLSConfig == nil {
|
||||||
if e.Listener == nil {
|
if e.Listener == nil {
|
||||||
e.Listener = tcpKeepAliveListener{l.(*net.TCPListener)}
|
e.Listener, err = newListener(s.Addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
e.colorer.Printf("⇛ http server started on %s\n", e.colorer.Green(s.Addr))
|
e.colorer.Printf("⇛ http server started on %s\n", e.colorer.Green(e.Listener.Addr()))
|
||||||
return s.Serve(e.Listener)
|
return s.Serve(e.Listener)
|
||||||
}
|
}
|
||||||
if e.TLSListener == nil {
|
if e.TLSListener == nil {
|
||||||
e.TLSListener = tls.NewListener(tcpKeepAliveListener{l.(*net.TCPListener)}, s.TLSConfig)
|
l, err := newListener(s.Addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e.TLSListener = tls.NewListener(l, s.TLSConfig)
|
||||||
}
|
}
|
||||||
e.colorer.Printf("⇛ https server started on %s\n", e.colorer.Green(s.Addr))
|
e.colorer.Printf("⇛ https server started on %s\n", e.colorer.Green(e.TLSListener.Addr()))
|
||||||
return s.Serve(e.TLSListener)
|
return s.Serve(e.TLSListener)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -646,3 +649,11 @@ func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
|
|||||||
tc.SetKeepAlivePeriod(3 * time.Minute)
|
tc.SetKeepAlivePeriod(3 * time.Minute)
|
||||||
return tc, nil
|
return tc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newListener(address string) (*tcpKeepAliveListener, error) {
|
||||||
|
l, err := net.Listen("tcp", address)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &tcpKeepAliveListener{l.(*net.TCPListener)}, nil
|
||||||
|
}
|
||||||
|
@ -11,6 +11,8 @@ import (
|
|||||||
|
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -391,6 +393,7 @@ func TestEchoStart(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
assert.NoError(t, e.Start(":0"))
|
assert.NoError(t, e.Start(":0"))
|
||||||
}()
|
}()
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEchoStartTLS(t *testing.T) {
|
func TestEchoStartTLS(t *testing.T) {
|
||||||
@ -398,6 +401,7 @@ func TestEchoStartTLS(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
assert.NoError(t, e.StartTLS(":0", "_fixture/certs/cert.pem", "_fixture/certs/key.pem"))
|
assert.NoError(t, e.StartTLS(":0", "_fixture/certs/cert.pem", "_fixture/certs/key.pem"))
|
||||||
}()
|
}()
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMethod(t *testing.T, method, path string, e *Echo) {
|
func testMethod(t *testing.T, method, path string, e *Echo) {
|
||||||
|
@ -52,6 +52,21 @@ s := &http.Server{
|
|||||||
e.Logger.Fatal(e.StartServer(s))
|
e.Logger.Fatal(e.StartServer(s))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Custom Listener
|
||||||
|
|
||||||
|
`Echo#*Listener` can be used to run a custom listener.
|
||||||
|
|
||||||
|
*Example*
|
||||||
|
|
||||||
|
```go
|
||||||
|
l, err := net.Listen("tcp", ":1323")
|
||||||
|
if err != nil {
|
||||||
|
e.Logger.Fatal(l)
|
||||||
|
}
|
||||||
|
e.Listener = l
|
||||||
|
e.Logger.Fatal(e.Start(""))
|
||||||
|
```
|
||||||
|
|
||||||
## Disable HTTP/2
|
## Disable HTTP/2
|
||||||
|
|
||||||
`Echo#DisableHTTP2` can be used disable HTTP/2 protocol.
|
`Echo#DisableHTTP2` can be used disable HTTP/2 protocol.
|
||||||
|
@ -51,3 +51,9 @@ Q: How to run Echo on a specific IP address?
|
|||||||
```go
|
```go
|
||||||
e.Start("<ip>:<port>")
|
e.Start("<ip>:<port>")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Q: How to run Echo on a random port?
|
||||||
|
|
||||||
|
```go
|
||||||
|
e.Start(":0")
|
||||||
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user