1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00

Startup banner

This commit is contained in:
Vishal Rana 2017-01-14 12:20:01 -08:00
parent db8b2ce62c
commit 736d153c29
3 changed files with 53 additions and 14 deletions

42
echo.go
View File

@ -51,6 +51,7 @@ import (
"sync"
"time"
"github.com/labstack/gommon/color"
"github.com/labstack/gommon/log"
"golang.org/x/crypto/acme/autocert"
)
@ -62,6 +63,7 @@ type (
TLSServer *http.Server
Listener net.Listener
TLSListener net.Listener
HideBanner bool
DisableHTTP2 bool
Debug bool
HTTPErrorHandler HTTPErrorHandler
@ -71,6 +73,7 @@ type (
AutoTLSManager autocert.Manager
Logger Logger
stdLogger *slog.Logger
colorer *color.Color
premiddleware []MiddlewareFunc
middleware []MiddlewareFunc
maxParam *int
@ -120,6 +123,30 @@ type (
}
)
// Banner
const (
// http://patorjk.com/software/taag/#p=display&f=Small%20Slant&t=Echo
banner = `
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/
%s %s
High performance, minimalist Go web framework
______________________________________O/_____
O\
%s server started on %s
`
)
const (
website = "https://echo.labstack.com"
version = "3.1.0.master"
)
// HTTP methods
const (
CONNECT = "CONNECT"
@ -244,6 +271,7 @@ func New() (e *Echo) {
Prompt: autocert.AcceptTOS,
},
Logger: log.New("echo"),
colorer: color.New(),
maxParam: new(int),
}
e.Server.Handler = e
@ -560,8 +588,20 @@ func (e *Echo) startTLS(address string) error {
// StartServer starts a custom http server.
func (e *Echo) StartServer(s *http.Server) error {
// Setup
e.colorer.SetOutput(e.Logger.Output())
s.Handler = e
s.ErrorLog = e.stdLogger
args := []interface{}{e.colorer.Blue(website), e.colorer.Red("v" + version), "http", e.colorer.Green(s.Addr)}
if s.TLSConfig != nil {
args[2] = "https"
}
// Banner
if !e.HideBanner {
e.colorer.Printf(banner, args...)
}
l, err := net.Listen("tcp", s.Addr)
if err != nil {
return err
@ -570,13 +610,11 @@ func (e *Echo) StartServer(s *http.Server) error {
if e.Listener == nil {
e.Listener = tcpKeepAliveListener{l.(*net.TCPListener)}
}
e.Logger.Printf("http server started on %s", s.Addr)
return s.Serve(e.Listener)
}
if e.TLSListener == nil {
e.TLSListener = tls.NewListener(tcpKeepAliveListener{l.(*net.TCPListener)}, s.TLSConfig)
}
e.Logger.Printf(" ⇛ https server started on %s", s.Addr)
return s.Serve(e.TLSListener)
}

View File

@ -11,7 +11,6 @@ import (
"github.com/labstack/echo"
"github.com/labstack/gommon/color"
isatty "github.com/mattn/go-isatty"
"github.com/valyala/fasttemplate"
)
@ -54,7 +53,7 @@ type (
Output io.Writer
template *fasttemplate.Template
color *color.Color
colorer *color.Color
pool sync.Pool
}
)
@ -67,8 +66,8 @@ var (
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
`"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
`"bytes_out":${bytes_out}}` + "\n",
Output: os.Stdout,
color: color.New(),
Output: os.Stdout,
colorer: color.New(),
}
)
@ -92,10 +91,8 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
}
config.template = fasttemplate.New(config.Format, "${", "}")
config.color = color.New()
if w, ok := config.Output.(*os.File); !ok || !isatty.IsTerminal(w.Fd()) {
config.color.Disable()
}
config.colorer = color.New()
config.colorer.SetOutput(config.Output)
config.pool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 256))
@ -149,14 +146,14 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
return buf.WriteString(req.UserAgent())
case "status":
n := res.Status
s := config.color.Green(n)
s := config.colorer.Green(n)
switch {
case n >= 500:
s = config.color.Red(n)
s = config.colorer.Red(n)
case n >= 400:
s = config.color.Yellow(n)
s = config.colorer.Yellow(n)
case n >= 300:
s = config.color.Cyan(n)
s = config.colorer.Cyan(n)
}
return buf.WriteString(s)
case "latency":

View File

@ -37,6 +37,10 @@ Default value is `OFF`. Possible values:
Logging is implemented using `echo.Logger` interface which allows you to register
a custom logger using `Echo#Logger`.
### Hide Banner
`Echo#HideBanner` can be used to hide the startup banner.
## Custom Server
### Using `Echo#StartServer()`