1
0
mirror of https://github.com/labstack/echo.git synced 2025-03-23 21:29:26 +02:00

Merge branch 'fix_missing_net_listener' of https://github.com/mtojek/echo into mtojek-fix_missing_net_listener

This commit is contained in:
Vishal Rana 2016-03-18 08:36:08 -07:00
commit bf239af966
3 changed files with 32 additions and 0 deletions
engine

@ -6,6 +6,7 @@ import (
"time"
"github.com/labstack/gommon/log"
"net"
)
type (
@ -137,6 +138,7 @@ type (
// Config defines engine configuration.
Config struct {
Address string // TCP address to listen on.
Listener net.Listener // Custom net.Listener for the HTTP server.
TLSCertfile string // TLS certificate file path.
TLSKeyfile string // TLS key file path.
ReadTimeout time.Duration // Maximum duration before timing out read of the request.

@ -123,6 +123,15 @@ func (s *Server) Start() {
addr := s.config.Address
certfile := s.config.TLSCertfile
keyfile := s.config.TLSKeyfile
if nil == s.config.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 +139,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.config.Listener, certfile, keyfile, handler))
} else {
s.logger.Fatal(fasthttp.Serve(s.config.Listener, handler))
}
}
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.HandlerFunc`.
func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
return func(c echo.Context) error {

@ -94,6 +94,15 @@ func (s *Server) SetLogger(l *log.Logger) {
func (s *Server) Start() {
certfile := s.config.TLSCertfile
keyfile := s.config.TLSKeyfile
if nil == s.config.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 +110,10 @@ func (s *Server) Start() {
}
}
func (s *Server) startCustomListener() {
s.logger.Fatal(s.Serve(s.config.Listener))
}
// ServeHTTP implements `http.Handler` interface.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Request