Skip to content

Commit c5a3575

Browse files
committed
updated migration guide
Signed-off-by: Vishal Rana <[email protected]>
1 parent 2f70d3e commit c5a3575

17 files changed

+147
-237
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
## Performance
2323

24-
![Performance](http://i.imgur.com/F2V7TfO.png)
24+
![Performance](https://i.imgur.com/F2V7TfO.png)
2525

2626
## Quick Start
2727

context.go

+22-18
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313
"os"
1414
"path/filepath"
1515
"strings"
16-
17-
"golang.org/x/net/websocket"
1816
)
1917

2018
type (
@@ -175,16 +173,15 @@ type (
175173
}
176174

177175
context struct {
178-
request *http.Request
179-
response *Response
180-
webSocket *websocket.Conn
181-
path string
182-
pnames []string
183-
pvalues []string
184-
query url.Values
185-
handler HandlerFunc
186-
store Map
187-
echo *Echo
176+
request *http.Request
177+
response *Response
178+
path string
179+
pnames []string
180+
pvalues []string
181+
query url.Values
182+
handler HandlerFunc
183+
store Map
184+
echo *Echo
188185
}
189186
)
190187

@@ -238,15 +235,22 @@ func (c *context) SetPath(p string) {
238235
c.path = p
239236
}
240237

241-
func (c *context) Param(name string) (value string) {
242-
l := len(c.pnames)
238+
func (c *context) Param(name string) string {
243239
for i, n := range c.pnames {
244-
if n == name && i < l {
245-
value = c.pvalues[i]
246-
break
240+
if i < len(c.pnames) {
241+
if strings.HasPrefix(n, name) {
242+
return c.pvalues[i]
243+
}
244+
245+
// Param name with aliases
246+
for _, p := range strings.Split(n, ",") {
247+
if p == name {
248+
return c.pvalues[i]
249+
}
250+
}
247251
}
248252
}
249-
return
253+
return ""
250254
}
251255

252256
func (c *context) ParamNames() []string {

context_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,18 @@ func TestContextPathParam(t *testing.T) {
250250
assert.Equal(t, "501", c.Param("fid"))
251251
}
252252

253+
func TestContextPathParamNamesAlais(t *testing.T) {
254+
e := New()
255+
req, _ := http.NewRequest(GET, "/", nil)
256+
c := e.NewContext(req, nil)
257+
258+
c.SetParamNames("id,name")
259+
c.SetParamValues("joe")
260+
261+
assert.Equal(t, "joe", c.Param("id"))
262+
assert.Equal(t, "joe", c.Param("name"))
263+
}
264+
253265
func TestContextFormValue(t *testing.T) {
254266
f := make(url.Values)
255267
f.Set("name", "Jon Snow")

router.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package echo
22

3+
import "strings"
4+
35
type (
46
// Router is the registry of all registered routes for an `Echo` instance for
57
// request matching and URL path parameter parsing.
@@ -170,7 +172,12 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
170172
if h != nil {
171173
cn.addHandler(method, h)
172174
cn.ppath = ppath
173-
cn.pnames = pnames
175+
for i, n := range cn.pnames {
176+
// Param name aliases
177+
if !strings.Contains(n, pnames[i]) {
178+
cn.pnames[i] += "," + pnames[i]
179+
}
180+
}
174181
}
175182
}
176183
return

router_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package echo
33
import (
44
"fmt"
55
"net/http"
6+
"strings"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
@@ -823,9 +824,11 @@ func TestRouterAPI(t *testing.T) {
823824
c := e.NewContext(nil, nil).(*context)
824825
for _, route := range gitHubAPI {
825826
r.Find(route.Method, route.Path, c)
826-
for i, n := range c.pnames {
827-
if assert.NotEmpty(t, n) {
828-
assert.Equal(t, n, c.pnames[i])
827+
for _, n := range c.pnames {
828+
for _, p := range strings.Split(n, ",") {
829+
if assert.NotEmpty(t, p) {
830+
assert.Equal(t, c.Param(p), ":"+p)
831+
}
829832
}
830833
}
831834
}

website/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
build:
2-
rm -rf public && hugo
2+
rm -rf public/v3 && hugo

website/config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
2-
"baseurl": "https://echo.labstack.com",
2+
"baseurl": "https://echo.labstack.com/",
33
"languageCode": "en-us",
44
"title": "Echo - Fast and unfancy HTTP server framework for Go (Golang)",
55
"canonifyurls": true,
66
"googleAnalytics": "UA-85059636-2",
7+
"publishdir": "public/v3",
78
"permalinks": {
89
"guide": "/guide/:filename",
910
"middleware": "/middleware/:filename",

website/content/guide/customization.md

+15-64
Original file line numberDiff line numberDiff line change
@@ -11,88 +11,39 @@ description = "Customizing Echo"
1111

1212
### HTTP Error Handler
1313

14-
`Echo#SetHTTPErrorHandler(h HTTPErrorHandler)` registers a custom `Echo#HTTPErrorHandler`.
15-
1614
Default HTTP error handler rules:
1715

1816
- If error is of type `Echo#HTTPError` it sends HTTP response with status code `HTTPError.Code`
1917
and message `HTTPError.Message`.
2018
- Else it sends `500 - Internal Server Error`.
2119
- If debug mode is enabled, it uses `error.Error()` as status message.
2220

23-
### Debug
24-
25-
`Echo#SetDebug(on bool)` enable/disable debug mode.
21+
You can also set a custom HTTP error handler using `Echo#HTTPErrorHandler`.
2622

27-
### Logging
23+
### Debugging
2824

29-
#### Custom Logger
25+
`Echo#Debug` enables/disables debug mode.
3026

31-
`Echo#SetLogger(l log.Logger)`
32-
33-
SetLogger defines a custom logger.
27+
### Logging
3428

3529
#### Log Output
3630

37-
`Echo#SetLogOutput(w io.Writer)` sets the output destination for the logger. Default
38-
value `os.Stdout`
31+
`Echo#Logger.SetOutput(io.Writer)` sets the output destination for the logger.
32+
Default value `os.Stdout`
3933

40-
To completely disable logs use `Echo#SetLogOutput(io.Discard)`
34+
To completely disable logs use `Echo#Logger.SetOutput(io.Discard)` or `Echo#Logger.SetLevel(log.OFF)`
4135

4236
#### Log Level
4337

44-
`Echo#SetLogLevel(l log.Level)`
38+
`Echo#Logger.SetLevel(log.Lvl)`
4539

46-
SetLogLevel sets the log level for the logger. Default value `5` (OFF).
40+
SetLogLevel sets the log level for the logger. Default value `OFF`.
4741
Possible values:
4842

49-
- `0` (DEBUG)
50-
- `1` (INFO)
51-
- `2` (WARN)
52-
- `3` (ERROR)
53-
- `4` (FATAL)
54-
- `5` (OFF)
55-
56-
### HTTP Engine
57-
58-
Echo currently supports standard and [fasthttp](https://github.com/valyala/fasthttp)
59-
server engines. Echo utilizes interfaces to abstract the internal implementation
60-
of these servers so you can seamlessly switch from one engine to another based on
61-
your preference.
62-
63-
#### Running a standard HTTP server
64-
65-
`e.Run(standard.New(":1323"))`
66-
67-
#### Running a fasthttp server
68-
69-
`e.Run(fasthttp.New(":1323"))`
70-
71-
#### Running a server with TLS configuration
72-
73-
`e.Run(<engine>.WithTLS(":1323", "<certFile>", "<keyFile>"))`
74-
75-
#### Running a server with engine configuration
76-
77-
`e.Run(<engine>.WithConfig(<config>))`
78-
79-
##### Configuration
80-
81-
```go
82-
Config struct {
83-
Address string // TCP address to listen on.
84-
Listener net.Listener // Custom `net.Listener`. If set, server accepts connections on it.
85-
TLSCertFile string // TLS certificate file path.
86-
TLSKeyFile string // TLS key file path.
87-
ReadTimeout time.Duration // Maximum duration before timing out read of the request.
88-
WriteTimeout time.Duration // Maximum duration before timing out write of the response.
89-
}
90-
```
91-
92-
#### Access internal server instance and configure its properties
43+
- `DEBUG`
44+
- `INFO`
45+
- `WARN`
46+
- `ERROR`
47+
- `OFF`
9348

94-
```go
95-
s := standard.New(":1323")
96-
s.MaxHeaderBytes = 1 << 20
97-
e.Run(s)
98-
```
49+
You can also set a custom logger using `Echo#Logger`.

website/content/guide/faq.md

+13-48
Original file line numberDiff line numberDiff line change
@@ -9,82 +9,47 @@ description = "Frequently asked questions in Echo"
99

1010
## FAQ
1111

12-
Q: **How to retrieve `*http.Request` and `http.ResponseWriter` from `echo.Context`?**
12+
Q: How to retrieve `*http.Request` and `http.ResponseWriter` from `echo.Context`?
1313

14-
- `http.Request` > `c.Request().(*standard.Request).Request`
14+
- `http.Request` > `c.Request()`
1515
- `http.ResponseWriter` > `c.Response()`
1616

17-
> Standard engine only
18-
19-
Q: **How to use standard handler `func(http.ResponseWriter, *http.Request)` with Echo?**
17+
Q: How to use standard handler `func(http.ResponseWriter, *http.Request)` with Echo?
2018

2119
```go
2220
func handler(w http.ResponseWriter, r *http.Request) {
23-
io.WriteString(w, "Handler!")
24-
}
25-
26-
func main() {
27-
e := echo.New()
28-
e.GET("/", standard.WrapHandler(http.HandlerFunc(handler)))
29-
e.Run(standard.New(":1323"))
30-
}
31-
```
32-
33-
Q: **How to use fasthttp handler `func(fasthttp.RequestCtx)` with Echo?**
34-
35-
```go
36-
func handler(c *fh.RequestCtx) {
37-
io.WriteString(c, "Handler!")
21+
io.WriteString(w, "Echo!")
3822
}
3923

4024
func main() {
4125
e := echo.New()
42-
e.GET("/", fasthttp.WrapHandler(handler))
43-
e.Run(fasthttp.New(":1323"))
26+
e.GET("/", echo.WrapHandler(http.HandlerFunc(handler)))
27+
e.Start(":1323")
4428
}
4529
```
4630

47-
Q: **How to use standard middleware `func(http.Handler) http.Handler` with Echo?**
31+
Q: How to use standard middleware `func(http.Handler) http.Handler` with Echo?
4832

4933
```go
5034
func middleware(h http.Handler) http.Handler {
5135
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52-
println("Middleware!")
36+
println("middleware")
5337
h.ServeHTTP(w, r)
5438
})
5539
}
5640

5741
func main() {
5842
e := echo.New()
59-
e.Use(standard.WrapMiddleware(middleware))
43+
e.Use(echo.WrapMiddleware(middleware))
6044
e.GET("/", func(c echo.Context) error {
61-
return c.String(http.StatusOK, "OK")
45+
return c.String(http.StatusOK, "Echo!")
6246
})
63-
e.Run(standard.New(":1323"))
47+
e.Start(":1323")
6448
}
6549
```
6650

67-
Q: **How to use fasthttp middleware `func(http.Handler) http.Handler` with Echo?**
51+
Q: How to run Echo on a specific IP address?
6852

6953
```go
70-
func middleware(h fh.RequestHandler) fh.RequestHandler {
71-
return func(ctx *fh.RequestCtx) {
72-
println("Middleware!")
73-
h(ctx)
74-
}
75-
}
76-
77-
func main() {
78-
e := echo.New()
79-
e.Use(fasthttp.WrapMiddleware(middleware))
80-
e.GET("/", func(c echo.Context) error {
81-
return c.String(http.StatusOK, "OK")
82-
})
83-
e.Run(fasthttp.New(":1323"))
84-
}
54+
e.Start("<ip>:<port>")
8555
```
86-
87-
<!-- ### Q: How to run Echo on specific IP and port?
88-
89-
```go
90-
``` -->

website/content/guide/installation.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ Echo is developed and tested using Go `1.6.x` and `1.7.x`
1515
$ go get -u github.com/labstack/echo
1616
```
1717

18-
> Ideally, you should rely on a [package manager](https://github.com/avelino/awesome-go#package-management) like glide or govendor to use a specific [version](https://github.com/labstack/echo/releases) of Echo.
18+
> Ideally you should rely on a [package manager](https://github.com/avelino/awesome-go#package-management) like glide or govendor to use a specific [version](https://github.com/labstack/echo/releases) of Echo.
1919
20-
### [Migrating from v1](/guide/migrating)
20+
### [Migrating Guide](/guide/migration)
2121

2222
Echo follows [semantic versioning](http://semver.org) managed through GitHub releases.
2323
Specific version of Echo can be installed using a [package manager](https://github.com/avelino/awesome-go#package-management).

0 commit comments

Comments
 (0)