Skip to content

Commit 13a733f

Browse files
committed
Changes from master (from 70acd57 to 5b36ce3)
1 parent 0d62f00 commit 13a733f

19 files changed

+647
-176
lines changed

.github/workflows/checks.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ permissions:
1414

1515
env:
1616
# run static analysis only with the latest Go version
17-
LATEST_GO_VERSION: 1.19
17+
LATEST_GO_VERSION: "1.20"
1818

1919
jobs:
2020
check:

.github/workflows/echo.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ permissions:
1414

1515
env:
1616
# run coverage and benchmarks only with the latest Go version
17-
LATEST_GO_VERSION: 1.19
17+
LATEST_GO_VERSION: "1.20"
1818

1919
jobs:
2020
test:
@@ -25,7 +25,7 @@ jobs:
2525
# Echo tests with last four major releases (unless there are pressing vulnerabilities)
2626
# As we depend on `golang.org/x/` libraries which only support last 2 Go releases we could have situations when
2727
# we derive from last four major releases promise.
28-
go: [1.18, 1.19]
28+
go: ["1.19", "1.20"]
2929
name: ${{ matrix.os }} @ Go ${{ matrix.go }}
3030
runs-on: ${{ matrix.os }}
3131
steps:

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ benchmark: ## Run benchmarks
3131
help: ## Display this help screen
3232
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
3333

34-
goversion ?= "1.17"
35-
test_version: ## Run tests inside Docker with given version (defaults to 1.17 oldest supported). Example: make test_version goversion=1.17
34+
goversion ?= "1.20"
35+
test_version: ## Run tests inside Docker with given version (defaults to 1.20 oldest supported). Example: make test_version goversion=1.20
3636
@docker run --rm -it -v $(shell pwd):/project golang:$(goversion) /bin/sh -c "cd /project && make init check"

README.md

+23-12
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,29 @@ func hello(c echo.Context) error {
8181
}
8282
```
8383

84-
# Third-party middlewares
85-
86-
| Repository | Description |
87-
|------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
88-
| [github.com/labstack/echo-contrib](https://github.com/labstack/echo-contrib) | (by Echo team) [casbin](https://github.com/casbin/casbin), [gorilla/sessions](https://github.com/gorilla/sessions), [jaegertracing](github.com/uber/jaeger-client-go), [prometheus](https://github.com/prometheus/client_golang/), [pprof](https://pkg.go.dev/net/http/pprof), [zipkin](https://github.com/openzipkin/zipkin-go) middlewares |
89-
| [deepmap/oapi-codegen](https://github.com/deepmap/oapi-codegen) | Automatically generate RESTful API documentation with [OpenAPI](https://swagger.io/specification/) Client and Server Code Generator |
90-
| [github.com/swaggo/echo-swagger](https://github.com/swaggo/echo-swagger) | Automatically generate RESTful API documentation with [Swagger](https://swagger.io/) 2.0. |
91-
| [github.com/ziflex/lecho](https://github.com/ziflex/lecho) | [Zerolog](https://github.com/rs/zerolog) logging library wrapper for Echo logger interface. |
92-
| [github.com/brpaz/echozap](https://github.com/brpaz/echozap) | Uber´s [Zap](https://github.com/uber-go/zap) logging library wrapper for Echo logger interface. |
93-
| [github.com/darkweak/souin/plugins/echo](https://github.com/darkweak/souin/tree/master/plugins/echo) | HTTP cache system based on [Souin](https://github.com/darkweak/souin) to automatically get your endpoints cached. It supports some distributed and non-distributed storage systems depending your needs. |
94-
| [github.com/mikestefanello/pagoda](https://github.com/mikestefanello/pagoda) | Rapid, easy full-stack web development starter kit built with Echo. |
95-
| [github.com/go-woo/protoc-gen-echo](https://github.com/go-woo/protoc-gen-echo) | ProtoBuf generate Echo server side code |
84+
# Official middleware repositories
85+
86+
Following list of middleware is maintained by Echo team.
87+
88+
| Repository | Description |
89+
|------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
90+
| [github.com/labstack/echo-jwt](https://github.com/labstack/echo-jwt) | [JWT](https://github.com/golang-jwt/jwt) middleware |
91+
| [github.com/labstack/echo-contrib](https://github.com/labstack/echo-contrib) | [casbin](https://github.com/casbin/casbin), [gorilla/sessions](https://github.com/gorilla/sessions), [jaegertracing](https://github.com/uber/jaeger-client-go), [prometheus](https://github.com/prometheus/client_golang/), [pprof](https://pkg.go.dev/net/http/pprof), [zipkin](https://github.com/openzipkin/zipkin-go) middlewares |
92+
93+
# Third-party middleware repositories
94+
95+
Be careful when adding 3rd party middleware. Echo teams does not have time or manpower to guarantee safety and quality
96+
of middlewares in this list.
97+
98+
| Repository | Description |
99+
|------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
100+
| [deepmap/oapi-codegen](https://github.com/deepmap/oapi-codegen) | Automatically generate RESTful API documentation with [OpenAPI](https://swagger.io/specification/) Client and Server Code Generator |
101+
| [github.com/swaggo/echo-swagger](https://github.com/swaggo/echo-swagger) | Automatically generate RESTful API documentation with [Swagger](https://swagger.io/) 2.0. |
102+
| [github.com/ziflex/lecho](https://github.com/ziflex/lecho) | [Zerolog](https://github.com/rs/zerolog) logging library wrapper for Echo logger interface. |
103+
| [github.com/brpaz/echozap](https://github.com/brpaz/echozap) | Uber´s [Zap](https://github.com/uber-go/zap) logging library wrapper for Echo logger interface. |
104+
| [github.com/darkweak/souin/plugins/echo](https://github.com/darkweak/souin/tree/master/plugins/echo) | HTTP cache system based on [Souin](https://github.com/darkweak/souin) to automatically get your endpoints cached. It supports some distributed and non-distributed storage systems depending your needs. |
105+
| [github.com/mikestefanello/pagoda](https://github.com/mikestefanello/pagoda) | Rapid, easy full-stack web development starter kit built with Echo. |
106+
| [github.com/go-woo/protoc-gen-echo](https://github.com/go-woo/protoc-gen-echo) | ProtoBuf generate Echo server side code |
96107

97108
Please send a PR to add your own library here.
98109

go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ module github.com/labstack/echo/v5
33
go 1.18
44

55
require (
6-
github.com/stretchr/testify v1.8.1
6+
github.com/stretchr/testify v1.8.2
77
github.com/valyala/fasttemplate v1.2.2
8-
golang.org/x/net v0.4.0
8+
golang.org/x/net v0.7.0
99
golang.org/x/time v0.3.0
1010
)
1111

1212
require (
1313
github.com/davecgh/go-spew v1.1.1 // indirect
1414
github.com/pmezard/go-difflib v1.0.0 // indirect
1515
github.com/valyala/bytebufferpool v1.0.0 // indirect
16-
golang.org/x/text v0.5.0 // indirect
16+
golang.org/x/text v0.7.0 // indirect
1717
gopkg.in/yaml.v3 v3.0.1 // indirect
1818
)

go.sum

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
88
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
99
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1010
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11-
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
12-
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
11+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
12+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
1313
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
1414
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
1515
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
1616
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
17-
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
18-
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
19-
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
20-
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
17+
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
18+
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
19+
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
20+
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
2121
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
2222
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
2323
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

middleware/context_timeout.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package middleware
2+
3+
import (
4+
"context"
5+
"errors"
6+
"github.com/labstack/echo/v5"
7+
"time"
8+
)
9+
10+
// ContextTimeoutConfig defines the config for ContextTimeout middleware.
11+
type ContextTimeoutConfig struct {
12+
// Skipper defines a function to skip middleware.
13+
Skipper Skipper
14+
15+
// ErrorHandler is a function when error aries in middeware execution.
16+
ErrorHandler func(c echo.Context, err error) error
17+
18+
// Timeout configures a timeout for the middleware
19+
Timeout time.Duration
20+
}
21+
22+
// ContextTimeout returns a middleware which returns error (503 Service Unavailable error) to client
23+
// when underlying method returns context.DeadlineExceeded error.
24+
func ContextTimeout(timeout time.Duration) echo.MiddlewareFunc {
25+
return ContextTimeoutWithConfig(ContextTimeoutConfig{Timeout: timeout})
26+
}
27+
28+
// ContextTimeoutWithConfig returns a Timeout middleware with config.
29+
func ContextTimeoutWithConfig(config ContextTimeoutConfig) echo.MiddlewareFunc {
30+
return toMiddlewareOrPanic(config)
31+
}
32+
33+
// ToMiddleware converts Config to middleware.
34+
func (config ContextTimeoutConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
35+
if config.Timeout == 0 {
36+
return nil, errors.New("timeout must be set")
37+
}
38+
if config.Skipper == nil {
39+
config.Skipper = DefaultSkipper
40+
}
41+
if config.ErrorHandler == nil {
42+
config.ErrorHandler = func(c echo.Context, err error) error {
43+
if err != nil && errors.Is(err, context.DeadlineExceeded) {
44+
return echo.ErrServiceUnavailable.WithInternal(err)
45+
}
46+
return err
47+
}
48+
}
49+
50+
return func(next echo.HandlerFunc) echo.HandlerFunc {
51+
return func(c echo.Context) error {
52+
if config.Skipper(c) {
53+
return next(c)
54+
}
55+
56+
timeoutContext, cancel := context.WithTimeout(c.Request().Context(), config.Timeout)
57+
defer cancel()
58+
59+
c.SetRequest(c.Request().WithContext(timeoutContext))
60+
61+
if err := next(c); err != nil {
62+
return config.ErrorHandler(c, err)
63+
}
64+
return nil
65+
}
66+
}, nil
67+
}

0 commit comments

Comments
 (0)