Skip to content

Commit

Permalink
net.Listener moved into config
Browse files Browse the repository at this point in the history
  • Loading branch information
mtojek committed Mar 17, 2016
1 parent c674ebf commit 5ed4d06
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
7 changes: 3 additions & 4 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"time"

"github.com/labstack/gommon/log"
"net")
"net"
)

type (
// Engine defines the interface for HTTP server.
Expand All @@ -17,9 +18,6 @@ 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()
}
Expand Down Expand Up @@ -140,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.
Expand Down
21 changes: 7 additions & 14 deletions engine/fasthttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ 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
listener net.Listener
logger *log.Logger
pool *pool
config engine.Config
handler engine.Handler
logger *log.Logger
pool *pool
}

pool struct {
Expand Down Expand Up @@ -91,11 +89,6 @@ 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
Expand Down Expand Up @@ -131,7 +124,7 @@ func (s *Server) Start() {
certfile := s.config.TLSCertfile
keyfile := s.config.TLSKeyfile

if nil == s.listener {
if nil == s.config.Listener {
s.startDefaultListener(addr, certfile, keyfile, handler)
} else {
s.startCustomListener(certfile, keyfile, handler)
Expand All @@ -148,9 +141,9 @@ func (s *Server) startDefaultListener(addr, certfile, keyfile string, handler fu

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))
s.logger.Fatal(fasthttp.ServeTLS(s.config.Listener, certfile, keyfile, handler))
} else {
s.logger.Fatal(fasthttp.Serve(s.listener, handler))
s.logger.Fatal(fasthttp.Serve(s.config.Listener, handler))
}
}

Expand Down
19 changes: 6 additions & 13 deletions engine/standard/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ 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
listener net.Listener
logger *log.Logger
pool *pool
config engine.Config
handler engine.Handler
logger *log.Logger
pool *pool
}

pool struct {
Expand Down Expand Up @@ -87,11 +85,6 @@ 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
Expand All @@ -102,7 +95,7 @@ func (s *Server) Start() {
certfile := s.config.TLSCertfile
keyfile := s.config.TLSKeyfile

if nil == s.listener {
if nil == s.config.Listener {
s.startDefaultListener(certfile, keyfile)
} else {
s.startCustomListener()
Expand All @@ -118,7 +111,7 @@ func (s *Server) startDefaultListener(certfile, keyfile string) {
}

func (s *Server) startCustomListener() {
s.logger.Fatal(s.Serve(s.listener))
s.logger.Fatal(s.Serve(s.config.Listener))
}

// ServeHTTP implements `http.Handler` interface.
Expand Down

0 comments on commit 5ed4d06

Please sign in to comment.