From c674ebf505e72decece0f0762ed55640b9945f93 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 17 Mar 2016 15:15:42 +0100 Subject: [PATCH] Support custom net.Listener #414 --- engine/engine.go | 5 ++++- engine/fasthttp/server.go | 32 ++++++++++++++++++++++++++++---- engine/standard/server.go | 28 ++++++++++++++++++++++++---- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/engine/engine.go b/engine/engine.go index 37e1c542..9fe33951 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -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() } diff --git a/engine/fasthttp/server.go b/engine/fasthttp/server.go index a03fbe40..a768f48b 100644 --- a/engine/fasthttp/server.go +++ b/engine/fasthttp/server.go @@ -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 { diff --git a/engine/standard/server.go b/engine/standard/server.go index 4b8d98fc..06945cf0 100644 --- a/engine/standard/server.go +++ b/engine/standard/server.go @@ -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