mirror of
https://github.com/labstack/echo.git
synced 2024-11-24 08:22:21 +02:00
Support custom net.Listener #414
This commit is contained in:
parent
6f0a7372fb
commit
c674ebf505
@ -6,7 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/labstack/gommon/log"
|
||||
)
|
||||
"net")
|
||||
|
||||
type (
|
||||
// Engine defines the interface for HTTP server.
|
||||
@ -17,6 +17,9 @@ type (
|
||||
// SetLogger sets the logger for the HTTP server.
|
||||
SetLogger(*log.Logger)
|
||||
|
||||
// SetListener sets custom listener for the HTTP server.
|
||||
SetListener(net.Listener)
|
||||
|
||||
// Start starts the HTTP server.
|
||||
Start()
|
||||
}
|
||||
|
@ -9,15 +9,17 @@ import (
|
||||
"github.com/labstack/echo/engine"
|
||||
"github.com/labstack/gommon/log"
|
||||
"github.com/valyala/fasthttp"
|
||||
"net"
|
||||
)
|
||||
|
||||
type (
|
||||
// Server implements `engine.Engine`.
|
||||
Server struct {
|
||||
config engine.Config
|
||||
handler engine.Handler
|
||||
logger *log.Logger
|
||||
pool *pool
|
||||
config engine.Config
|
||||
handler engine.Handler
|
||||
listener net.Listener
|
||||
logger *log.Logger
|
||||
pool *pool
|
||||
}
|
||||
|
||||
pool struct {
|
||||
@ -89,6 +91,11 @@ func (s *Server) SetHandler(h engine.Handler) {
|
||||
s.handler = h
|
||||
}
|
||||
|
||||
// SetHandler implements `engine.Engine#SetListener` method.
|
||||
func (s *Server) SetListener(ln net.Listener) {
|
||||
s.listener = ln
|
||||
}
|
||||
|
||||
// SetLogger implements `engine.Engine#SetLogger` method.
|
||||
func (s *Server) SetLogger(l *log.Logger) {
|
||||
s.logger = l
|
||||
@ -123,6 +130,15 @@ func (s *Server) Start() {
|
||||
addr := s.config.Address
|
||||
certfile := s.config.TLSCertfile
|
||||
keyfile := s.config.TLSKeyfile
|
||||
|
||||
if nil == s.listener {
|
||||
s.startDefaultListener(addr, certfile, keyfile, handler)
|
||||
} else {
|
||||
s.startCustomListener(certfile, keyfile, handler)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) startDefaultListener(addr, certfile, keyfile string, handler func(c *fasthttp.RequestCtx)) {
|
||||
if certfile != "" && keyfile != "" {
|
||||
s.logger.Fatal(fasthttp.ListenAndServeTLS(addr, certfile, keyfile, handler))
|
||||
} else {
|
||||
@ -130,6 +146,14 @@ func (s *Server) Start() {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) startCustomListener(certfile, keyfile string, handler func(c *fasthttp.RequestCtx)) {
|
||||
if certfile != "" && keyfile != "" {
|
||||
s.logger.Fatal(fasthttp.ServeTLS(s.listener, certfile, keyfile, handler))
|
||||
} else {
|
||||
s.logger.Fatal(fasthttp.Serve(s.listener, handler))
|
||||
}
|
||||
}
|
||||
|
||||
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.HandlerFunc`.
|
||||
func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
|
@ -7,16 +7,18 @@ import (
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/engine"
|
||||
"github.com/labstack/gommon/log"
|
||||
"net"
|
||||
)
|
||||
|
||||
type (
|
||||
// Server implements `engine.Engine`.
|
||||
Server struct {
|
||||
*http.Server
|
||||
config engine.Config
|
||||
handler engine.Handler
|
||||
logger *log.Logger
|
||||
pool *pool
|
||||
config engine.Config
|
||||
handler engine.Handler
|
||||
listener net.Listener
|
||||
logger *log.Logger
|
||||
pool *pool
|
||||
}
|
||||
|
||||
pool struct {
|
||||
@ -85,6 +87,11 @@ func (s *Server) SetHandler(h engine.Handler) {
|
||||
s.handler = h
|
||||
}
|
||||
|
||||
// SetHandler implements `engine.Engine#SetListener` method.
|
||||
func (s *Server) SetListener(ln net.Listener) {
|
||||
s.listener = ln
|
||||
}
|
||||
|
||||
// SetLogger implements `engine.Engine#SetLogger` method.
|
||||
func (s *Server) SetLogger(l *log.Logger) {
|
||||
s.logger = l
|
||||
@ -94,6 +101,15 @@ func (s *Server) SetLogger(l *log.Logger) {
|
||||
func (s *Server) Start() {
|
||||
certfile := s.config.TLSCertfile
|
||||
keyfile := s.config.TLSKeyfile
|
||||
|
||||
if nil == s.listener {
|
||||
s.startDefaultListener(certfile, keyfile)
|
||||
} else {
|
||||
s.startCustomListener()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) startDefaultListener(certfile, keyfile string) {
|
||||
if certfile != "" && keyfile != "" {
|
||||
s.logger.Fatal(s.ListenAndServeTLS(certfile, keyfile))
|
||||
} else {
|
||||
@ -101,6 +117,10 @@ func (s *Server) Start() {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) startCustomListener() {
|
||||
s.logger.Fatal(s.Serve(s.listener))
|
||||
}
|
||||
|
||||
// ServeHTTP implements `http.Handler` interface.
|
||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// Request
|
||||
|
Loading…
Reference in New Issue
Block a user