1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-15 01:34:53 +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() { func main() {
e := echo.New() e := echo.New()
e.Use(mw.Log()) e.Use(mw.Logger())
e.Get("/", func(c echo.Context) error { e.Use(mw.Recover())
e.Get("/", echo.HandlerFunc(func(c echo.Context) error {
return c.String(200, "Hello, World!") 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") return c.String(200, "Echo v2")
}) }))
// FastHTTP // FastHTTP
// e.Run(fasthttp.New(":4444")) // e.Run(fasthttp.New(":1323"))
// Standard // 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 - Suggest new features/recipes
- Improve/fix documentation - Improve/fix documentation
## Support
- [Chat](https://gitter.im/labstack/echo)
## Credits ## Credits
- [Vishal Rana](https://github.com/vishr) - Author - [Vishal Rana](https://github.com/vishr) - Author
- [Nitin Rana](https://github.com/nr17) - Consultant - [Nitin Rana](https://github.com/nr17) - Consultant

View File

@ -32,6 +32,7 @@ type (
Path() string Path() string
P(int) string P(int) string
Param(string) string Param(string) string
ParamNames() []string
Query(string) string Query(string) string
Form(string) string Form(string) string
Set(string, interface{}) Set(string, interface{})
@ -148,6 +149,11 @@ func (c *context) Param(name string) (value string) {
return return
} }
// ParamNames returns path parameter names.
func (c *context) ParamNames() []string {
return c.pnames
}
// Query returns query parameter by name. // Query returns query parameter by name.
func (c *context) Query(name string) string { func (c *context) Query(name string) string {
return c.request.URL().QueryValue(name) 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 // Bind binds the request body into specified type `i`. The default binder does
// it based on Content-Type header. // it based on Content-Type header.
func (c *context) Bind(i interface{}) error { 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 // 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 return ErrRendererNotRegistered
} }
buf := new(bytes.Buffer) 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 return
} }
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8) 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) return t.templates.ExecuteTemplate(w, name, data)
} }
@ -46,6 +46,10 @@ func TestContext(t *testing.T) {
// Socket // Socket
assert.Nil(t, c.Socket()) assert.Nil(t, c.Socket())
// ParamNames
c.Object().pnames = []string{"uid", "fid"}
assert.EqualValues(t, []string{"uid", "fid"}, c.ParamNames())
// Param by id // Param by id
c.Object().pnames = []string{"id"} c.Object().pnames = []string{"id"}
c.Object().pvalues = []string{"1"} 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 is the interface that wraps the Bind method.
Binder interface { Binder interface {
Bind(engine.Request, interface{}) error Bind(interface{}, Context) error
} }
binder struct { binder struct {
@ -76,7 +76,7 @@ type (
// Renderer is the interface that wraps the Render method. // Renderer is the interface that wraps the Render method.
Renderer interface { 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 return e.message
} }
func (binder) Bind(r engine.Request, i interface{}) (err error) { func (binder) Bind(i interface{}, c Context) (err error) {
ct := r.Header().Get(ContentType) req := c.Request()
ct := req.Header().Get(ContentType)
err = ErrUnsupportedMediaType err = ErrUnsupportedMediaType
if strings.HasPrefix(ct, ApplicationJSON) { 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()) err = NewHTTPError(http.StatusBadRequest, err.Error())
} }
} else if strings.HasPrefix(ct, ApplicationXML) { } 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()) 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", "languageCode": "en-us",
"title": "Echo", "title": "Echo",
"canonifyurls": true, "canonifyurls": true,
@ -21,6 +21,7 @@
}, },
"params": { "params": {
"description": "Golang micro web framework, High performance, Minimalistic and Fast.",
"googleAnayticsId": "UA-51208124-3" "googleAnayticsId": "UA-51208124-3"
} }
} }

View File

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

View File

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

View File

@ -6,19 +6,21 @@ menu:
weight: 1 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 ```sh
$ go get github.com/labstack/echo $ go get github.com/labstack/echo
``` ```
To upgrade #### Update
```sh ```sh
$ go get -u github.com/labstack/echo $ go get -u github.com/labstack/echo
``` ```
#### [Migrating from v1](/guide/migrating)
Echo follows [semantic versioning](http://semver.org) managed through GitHub releases. 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). 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: menu:
side: side:
parent: guide parent: guide
weight: 4 weight: 5
--- ---
Middleware is a function which is chained in the HTTP request-response cycle. Middleware Middleware is a function which is chained in the HTTP request-response cycle. Middleware

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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