1
0
mirror of https://github.com/labstack/echo.git synced 2025-03-03 14:52:47 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-05 20:03:11 -08:00
parent 8916d5558c
commit a9c88cad63
16 changed files with 55 additions and 36 deletions

View File

@ -17,19 +17,20 @@ import (
func main() {
e := echo.New()
e.Use(mw.Log())
e.Get("/", func(c echo.Context) error {
e.Use(mw.Logger())
e.Use(mw.Recover())
e.Get("/", echo.HandlerFunc(func(c echo.Context) error {
return c.String(200, "Hello, World!")
})
e.Get("/v2", func(c echo.Context) error {
}))
e.Get("/v2", echo.HandlerFunc(func(c echo.Context) error {
return c.String(200, "Echo v2")
})
}))
// FastHTTP
// e.Run(fasthttp.New(":4444"))
// e.Run(fasthttp.New(":1323"))
// Standard
e.Run(standard.New(":4444"))
e.Run(standard.New(":1323"))
}
```
@ -82,6 +83,10 @@ BenchmarkZeus_GithubAll 2000 748827 ns/op 30068
- Suggest new features/recipes
- Improve/fix documentation
## Support
- [Chat](https://gitter.im/labstack/echo)
## Credits
- [Vishal Rana](https://github.com/vishr) - Author
- [Nitin Rana](https://github.com/nr17) - Consultant

View File

@ -32,6 +32,7 @@ type (
Path() string
P(int) string
Param(string) string
ParamNames() []string
Query(string) string
Form(string) string
Set(string, interface{})
@ -148,6 +149,11 @@ func (c *context) Param(name string) (value string) {
return
}
// ParamNames returns path parameter names.
func (c *context) ParamNames() []string {
return c.pnames
}
// Query returns query parameter by name.
func (c *context) Query(name string) string {
return c.request.URL().QueryValue(name)
@ -174,7 +180,7 @@ func (c *context) Set(key string, val interface{}) {
// Bind binds the request body into specified type `i`. The default binder does
// it based on Content-Type header.
func (c *context) Bind(i interface{}) error {
return c.echo.binder.Bind(c.request, i)
return c.echo.binder.Bind(i, c)
}
// Render renders a template with data and sends a text/html response with status
@ -184,7 +190,7 @@ func (c *context) Render(code int, name string, data interface{}) (err error) {
return ErrRendererNotRegistered
}
buf := new(bytes.Buffer)
if err = c.echo.renderer.Render(buf, name, data); err != nil {
if err = c.echo.renderer.Render(buf, name, data, c); err != nil {
return
}
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)

View File

@ -23,7 +23,7 @@ type (
}
)
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
func (t *Template) Render(w io.Writer, name string, data interface{}, c Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
@ -46,6 +46,10 @@ func TestContext(t *testing.T) {
// Socket
assert.Nil(t, c.Socket())
// ParamNames
c.Object().pnames = []string{"uid", "fid"}
assert.EqualValues(t, []string{"uid", "fid"}, c.ParamNames())
// Param by id
c.Object().pnames = []string{"id"}
c.Object().pvalues = []string{"1"}

13
echo.go
View File

@ -63,7 +63,7 @@ type (
// Binder is the interface that wraps the Bind method.
Binder interface {
Bind(engine.Request, interface{}) error
Bind(interface{}, Context) error
}
binder struct {
@ -76,7 +76,7 @@ type (
// Renderer is the interface that wraps the Render method.
Renderer interface {
Render(w io.Writer, name string, data interface{}) error
Render(io.Writer, string, interface{}, Context) error
}
)
@ -444,15 +444,16 @@ func (e *HTTPError) Error() string {
return e.message
}
func (binder) Bind(r engine.Request, i interface{}) (err error) {
ct := r.Header().Get(ContentType)
func (binder) Bind(i interface{}, c Context) (err error) {
req := c.Request()
ct := req.Header().Get(ContentType)
err = ErrUnsupportedMediaType
if strings.HasPrefix(ct, ApplicationJSON) {
if err = json.NewDecoder(r.Body()).Decode(i); err != nil {
if err = json.NewDecoder(req.Body()).Decode(i); err != nil {
err = NewHTTPError(http.StatusBadRequest, err.Error())
}
} else if strings.HasPrefix(ct, ApplicationXML) {
if err = xml.NewDecoder(r.Body()).Decode(i); err != nil {
if err = xml.NewDecoder(req.Body()).Decode(i); err != nil {
err = NewHTTPError(http.StatusBadRequest, err.Error())
}
}

View File

@ -1,5 +1,5 @@
{
"baseurl": "http://labstack.com/echo",
"baseurl": "https://labstack.com/echo",
"languageCode": "en-us",
"title": "Echo",
"canonifyurls": true,
@ -21,6 +21,7 @@
},
"params": {
"description": "Golang micro web framework, High performance, Minimalistic and Fast.",
"googleAnayticsId": "UA-51208124-3"
}
}

View File

@ -3,7 +3,7 @@ title: Customization
menu:
side:
parent: guide
weight: 2
weight: 3
---
### HTTP error handler

View File

@ -3,7 +3,7 @@ title: Error Handling
menu:
side:
parent: guide
weight: 7
weight: 8
---
Echo advocates centralized HTTP error handling by returning `error` from middleware

View File

@ -6,19 +6,21 @@ menu:
weight: 1
---
Echo has been developed and tested using Go `1.4.x`
Echo is developed and tested using Go `1.5.x` and `1.6.x`
Install the latest version of Echo via `go get`
#### Install the latest stable version of Echo via `go get`
```sh
$ go get github.com/labstack/echo
```
To upgrade
#### Update
```sh
$ go get -u github.com/labstack/echo
```
#### [Migrating from v1](/guide/migrating)
Echo follows [semantic versioning](http://semver.org) managed through GitHub releases.
Specific version of Echo can be installed using a [package manager](https://github.com/avelino/awesome-go#package-management).

View File

@ -3,7 +3,7 @@ title: Middleware
menu:
side:
parent: guide
weight: 4
weight: 5
---
Middleware is a function which is chained in the HTTP request-response cycle. Middleware

View File

@ -3,7 +3,7 @@ title: Request
menu:
side:
parent: guide
weight: 5
weight: 6
---
### Handler path

View File

@ -3,7 +3,7 @@ title: Response
menu:
side:
parent: guide
weight: 6
weight: 7
---
### Template

View File

@ -3,7 +3,7 @@ title: Routing
menu:
side:
parent: guide
weight: 3
weight: 4
---
Echo's router is [fast, optimized]({{< relref "index.md#performance">}}) and

View File

@ -2,7 +2,7 @@
title: Index
---
# Echo
# ![Echo](/images/echo.svg) Echo
A fast and unfancy micro web framework for Go.

View File

@ -5,9 +5,9 @@
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>
{{ if ne .URL "/" }}{{ .Title }} | {{ end }}{{ .Site.Title }}
{{ .Site.Title }} - {{ .Site.Params.description }}{{ if ne .URL "/" }} - {{ .Title }} {{ end }}
</title>
<meta name="description" content="">
<meta name="description" content="{{ .Site.Params.description }}">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- <link rel="apple-touch-icon" href="apple-touch-icon.png"> -->

View File

@ -1,12 +1,12 @@
<header class="mdl-layout__header">
<div class="mdl-layout__header-row">
<a href="/" class="mdl-navigation__link mdl-layout-title">
{{ .Site.Title }}
<a href="https://labstack.com" class="mdl-navigation__link mdl-layout-title">
<img src="/images/logo.svg" alt="LabStack">
</a>
<div class="mdl-layout-spacer"></div>
<nav class="mdl-navigation mdl-layout--large-screen-only">
<a href="https://github.com/labstack/echo" class="mdl-navigation__link">
<i class="fa fa-github fa-lg"></i> GitHub
<i class="fa fa-github fa-2x"></i>
</a>
</nav>
</div>

View File

@ -1,6 +1,6 @@
footer {
background-color: inherit !important;
border-top: 2px solid #E0E0E0;
border-top: 2px solid #e0e0e0;
}
.facebook a:hover {
color: #3B5998;
@ -15,12 +15,12 @@ footer {
color: #333;
}
:not(pre) > code {
padding: 2px 4px;
background: #EEE;
padding: 0 4px;
background: #eee;
color: #424242;
font-size: .95em;
font-family: Source Code Pro, Monaco, Menlo, Consolas, monospace;
border: 1px solid #E0E0E0;
border: 1px solid #e0e0e0;
border-radius: 2px;
}
a:link {