mirror of
https://github.com/labstack/echo.git
synced 2025-07-17 01:43:02 +02:00
6
echo.go
6
echo.go
@ -230,7 +230,7 @@ func (e *Echo) SetLogOutput(w io.Writer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetLogLevel sets the log level for the logger. Default value is `log.FATAL`.
|
// SetLogLevel sets the log level for the logger. Default value is `log.FATAL`.
|
||||||
func (e *Echo) SetLogLevel(l log.Level) {
|
func (e *Echo) SetLogLevel(l uint8) {
|
||||||
e.logger.SetLevel(l)
|
e.logger.SetLevel(l)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -459,10 +459,10 @@ func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run starts the HTTP server.
|
// Run starts the HTTP server.
|
||||||
func (e *Echo) Run(s engine.Server) {
|
func (e *Echo) Run(s engine.Server) error {
|
||||||
s.SetHandler(e)
|
s.SetHandler(e)
|
||||||
s.SetLogger(e.logger)
|
s.SetLogger(e.logger)
|
||||||
s.Start()
|
return s.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPError(code int, msg ...string) *HTTPError {
|
func NewHTTPError(code int, msg ...string) *HTTPError {
|
||||||
|
12
glide.lock
generated
12
glide.lock
generated
@ -1,5 +1,5 @@
|
|||||||
hash: f220137e9ccd9aaf3051be33c3c23ea8abd2c40df6cf96175c3994d482b5e007
|
hash: f220137e9ccd9aaf3051be33c3c23ea8abd2c40df6cf96175c3994d482b5e007
|
||||||
updated: 2016-03-14T18:03:44.054071074-07:00
|
updated: 2016-03-18T16:35:06.621226469-07:00
|
||||||
imports:
|
imports:
|
||||||
- name: github.com/klauspost/compress
|
- name: github.com/klauspost/compress
|
||||||
version: 006acde2c5d283d2f8b8aa03d8f0cd2891c680cf
|
version: 006acde2c5d283d2f8b8aa03d8f0cd2891c680cf
|
||||||
@ -12,7 +12,7 @@ imports:
|
|||||||
- name: github.com/klauspost/crc32
|
- name: github.com/klauspost/crc32
|
||||||
version: 19b0b332c9e4516a6370a0456e6182c3b5036720
|
version: 19b0b332c9e4516a6370a0456e6182c3b5036720
|
||||||
- name: github.com/labstack/gommon
|
- name: github.com/labstack/gommon
|
||||||
version: c7a42f4800da9d39225ce15411f48288d622e517
|
version: 81aef43cee03d40e9190983caeb70343a68d7f76
|
||||||
subpackages:
|
subpackages:
|
||||||
- color
|
- color
|
||||||
- log
|
- log
|
||||||
@ -21,14 +21,16 @@ imports:
|
|||||||
- name: github.com/mattn/go-isatty
|
- name: github.com/mattn/go-isatty
|
||||||
version: 56b76bdf51f7708750eac80fa38b952bb9f32639
|
version: 56b76bdf51f7708750eac80fa38b952bb9f32639
|
||||||
- name: github.com/valyala/fasthttp
|
- name: github.com/valyala/fasthttp
|
||||||
version: ca2c5535a325ab129d46891de0bbe84261822b3c
|
version: 2b172da53920a126cfc2532eced9400864bdacd9
|
||||||
|
- name: github.com/valyala/fasttemplate
|
||||||
|
version: 3b874956e03f1636d171bda64b130f9135f42cff
|
||||||
- name: golang.org/x/net
|
- name: golang.org/x/net
|
||||||
version: e7da8edaa52631091740908acaf2c2d4c9b3ce90
|
version: 35b06af0720201bc2f326773a80767387544f8c4
|
||||||
subpackages:
|
subpackages:
|
||||||
- context
|
- context
|
||||||
- websocket
|
- websocket
|
||||||
- name: golang.org/x/sys
|
- name: golang.org/x/sys
|
||||||
version: 7a56174f0086b32866ebd746a794417edbc678a1
|
version: 9d4e42a20653790449273b3c85e67d6d8bae6e2e
|
||||||
subpackages:
|
subpackages:
|
||||||
- unix
|
- unix
|
||||||
devImports: []
|
devImports: []
|
||||||
|
@ -2,10 +2,11 @@ package: github.com/labstack/echo
|
|||||||
import:
|
import:
|
||||||
- package: github.com/labstack/gommon
|
- package: github.com/labstack/gommon
|
||||||
subpackages:
|
subpackages:
|
||||||
- /color
|
- color
|
||||||
- log
|
- log
|
||||||
- package: github.com/valyala/fasthttp
|
- package: github.com/valyala/fasthttp
|
||||||
- package: golang.org/x/net
|
- package: golang.org/x/net
|
||||||
subpackages:
|
subpackages:
|
||||||
- /context
|
- context
|
||||||
- websocket
|
- websocket
|
||||||
|
- package: github.com/valyala/fasttemplate
|
||||||
|
@ -26,9 +26,9 @@ var (
|
|||||||
//
|
//
|
||||||
// For valid credentials it calls the next handler.
|
// For valid credentials it calls the next handler.
|
||||||
// For invalid credentials, it sends "401 - Unauthorized" response.
|
// For invalid credentials, it sends "401 - Unauthorized" response.
|
||||||
func BasicAuth(fn BasicAuthFunc) echo.MiddlewareFunc {
|
func BasicAuth(f BasicAuthFunc) echo.MiddlewareFunc {
|
||||||
c := DefaultBasicAuthConfig
|
c := DefaultBasicAuthConfig
|
||||||
c.AuthFunc = fn
|
c.AuthFunc = f
|
||||||
return BasicAuthFromConfig(c)
|
return BasicAuthFromConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,13 +15,13 @@ func TestBasicAuth(t *testing.T) {
|
|||||||
req := test.NewRequest(echo.GET, "/", nil)
|
req := test.NewRequest(echo.GET, "/", nil)
|
||||||
res := test.NewResponseRecorder()
|
res := test.NewResponseRecorder()
|
||||||
c := echo.NewContext(req, res, e)
|
c := echo.NewContext(req, res, e)
|
||||||
fn := func(u, p string) bool {
|
f := func(u, p string) bool {
|
||||||
if u == "joe" && p == "secret" {
|
if u == "joe" && p == "secret" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
h := BasicAuth(fn)(echo.HandlerFunc(func(c echo.Context) error {
|
h := BasicAuth(f)(echo.HandlerFunc(func(c echo.Context) error {
|
||||||
return c.String(http.StatusOK, "test")
|
return c.String(http.StatusOK, "test")
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
@ -1,20 +1,28 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"github.com/labstack/gommon/color"
|
"github.com/labstack/gommon/color"
|
||||||
|
"github.com/valyala/fasttemplate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
LoggerConfig struct {
|
LoggerConfig struct {
|
||||||
|
Format string
|
||||||
|
template *fasttemplate.Template
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DefaultLoggerConfig = LoggerConfig{}
|
DefaultLoggerConfig = LoggerConfig{
|
||||||
|
Format: "[${time}] ${remote_ip} ${method} ${path} ${status} ${response_time} ${size}\n",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func Logger() echo.MiddlewareFunc {
|
func Logger() echo.MiddlewareFunc {
|
||||||
@ -22,13 +30,15 @@ func Logger() echo.MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
|
func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
|
||||||
|
config.template = fasttemplate.New(config.Format, "${", "}")
|
||||||
|
|
||||||
return func(next echo.Handler) echo.Handler {
|
return func(next echo.Handler) echo.Handler {
|
||||||
return echo.HandlerFunc(func(c echo.Context) error {
|
return echo.HandlerFunc(func(c echo.Context) (err error) {
|
||||||
req := c.Request()
|
req := c.Request()
|
||||||
res := c.Response()
|
res := c.Response()
|
||||||
logger := c.Logger()
|
|
||||||
|
|
||||||
remoteAddr := req.RemoteAddress()
|
remoteAddr := req.RemoteAddress()
|
||||||
|
output := c.Logger().Output()
|
||||||
|
|
||||||
if ip := req.Header().Get(echo.XRealIP); ip != "" {
|
if ip := req.Header().Get(echo.XRealIP); ip != "" {
|
||||||
remoteAddr = ip
|
remoteAddr = ip
|
||||||
} else if ip = req.Header().Get(echo.XForwardedFor); ip != "" {
|
} else if ip = req.Header().Get(echo.XForwardedFor); ip != "" {
|
||||||
@ -42,26 +52,46 @@ func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
stop := time.Now()
|
stop := time.Now()
|
||||||
method := req.Method()
|
method := []byte(req.Method())
|
||||||
path := req.URL().Path()
|
path := req.URL().Path()
|
||||||
if path == "" {
|
if path == "" {
|
||||||
path = "/"
|
path = "/"
|
||||||
}
|
}
|
||||||
size := res.Size()
|
took := stop.Sub(start)
|
||||||
|
size := strconv.FormatInt(res.Size(), 10)
|
||||||
|
|
||||||
n := res.Status()
|
n := res.Status()
|
||||||
code := color.Green(n)
|
status := color.Green(n)
|
||||||
switch {
|
switch {
|
||||||
case n >= 500:
|
case n >= 500:
|
||||||
code = color.Red(n)
|
status = color.Red(n)
|
||||||
case n >= 400:
|
case n >= 400:
|
||||||
code = color.Yellow(n)
|
status = color.Yellow(n)
|
||||||
case n >= 300:
|
case n >= 300:
|
||||||
code = color.Cyan(n)
|
status = color.Cyan(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Printf("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
|
_, err = config.template.ExecuteFunc(output, func(w io.Writer, tag string) (int, error) {
|
||||||
return nil
|
switch tag {
|
||||||
|
case "time":
|
||||||
|
return w.Write([]byte(time.Now().Format(time.Stamp)))
|
||||||
|
case "remote_ip":
|
||||||
|
return w.Write([]byte(remoteAddr))
|
||||||
|
case "method":
|
||||||
|
return w.Write(method)
|
||||||
|
case "path":
|
||||||
|
return w.Write([]byte(path))
|
||||||
|
case "status":
|
||||||
|
return w.Write([]byte(status))
|
||||||
|
case "response_time":
|
||||||
|
return w.Write([]byte(took.String()))
|
||||||
|
case "size":
|
||||||
|
return w.Write([]byte(size))
|
||||||
|
default:
|
||||||
|
return w.Write([]byte(fmt.Sprintf("[unknown tag %s]", tag)))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user