1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00

Fixed example and updated readme.

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-04-18 17:02:02 -07:00
parent 381fbae1ff
commit a9c41b367c
2 changed files with 16 additions and 122 deletions

136
README.md
View File

@ -3,21 +3,25 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G
## Features ## Features
- Fast :rocket: router which smartly resolves conflicting routes - Fast :rocket: router which smartly resolves conflicting routes.
- Extensible middleware/handler, supports: - Extensible middleware/handler, supports:
- Middleware - Middleware
- `func(*echo.Context)` - `func(*echo.Context)`
- `func(echo.HandlerFunc) echo.HandlerFunc` - `func(*echo.Context) error`
- `func(echo.HandlerFunc) echo.HandlerFunc error`
- `func(http.Handler) http.Handler` - `func(http.Handler) http.Handler`
- `http.Handler` - `http.Handler`
- `http.HandlerFunc` - `http.HandlerFunc`
- `func(http.ResponseWriter, *http.Request)` - `func(http.ResponseWriter, *http.Request)`
- `func(http.ResponseWriter, *http.Request) error`
- Handler - Handler
- `func(*echo.Context)` - `func(*echo.Context)`
- `func(*echo.Context) error`
- `http.Handler` - `http.Handler`
- `http.HandlerFunc` - `http.HandlerFunc`
- `func(http.ResponseWriter, *http.Request)` - `func(http.ResponseWriter, *http.Request)`
- Group routing - `func(http.ResponseWriter, *http.Request) error`
- Sub routing with groups.
- Handy encoding/decoding functions. - Handy encoding/decoding functions.
- Serve static files, including index. - Serve static files, including index.
@ -61,147 +65,37 @@ BenchmarkZeus_GithubAll 2000 752907 ns/op 300688 B/op 2648 all
```go get github.com/labstack/echo``` ```go get github.com/labstack/echo```
## Example ## Examples
[labstack/echo/example](https://github.com/labstack/echo/tree/master/example) [labstack/echo/example](https://github.com/labstack/echo/tree/master/examples)
```go ```go
package main package main
import ( import (
"io"
"net/http" "net/http"
"html/template"
"github.com/labstack/echo" "github.com/labstack/echo"
mw "github.com/labstack/echo/middleware" mw "github.com/labstack/echo/middleware"
"github.com/rs/cors"
"github.com/thoas/stats"
) )
type ( // Handler
// Template provides HTML template rendering func hello(c *echo.Context) {
Template struct { c.String(http.StatusOK, "Hello, World!\n")
templates *template.Template
}
user struct {
ID string `json:"id"`
Name string `json:"name"`
}
)
var (
users map[string]user
)
// Render HTML
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func welcome(c *echo.Context) {
c.Render("welcome", "Joe")
}
func createUser(c *echo.Context) {
u := new(user)
if err := c.Bind(u); err == nil {
users[u.ID] = *u
if err := c.JSON(http.StatusCreated, u); err == nil {
// Do something!
}
return
}
http.Error(c.Response, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
func getUsers(c *echo.Context) {
c.JSON(http.StatusOK, users)
}
func getUser(c *echo.Context) {
c.JSON(http.StatusOK, users[c.P(0)])
} }
func main() { func main() {
e := echo.New() e := echo.New()
//*************************// // Middleware
// Built-in middleware //
//*************************//
e.Use(mw.Logger) e.Use(mw.Logger)
//****************************// // Routes
// Third-party middleware // e.Get("/", hello)
//****************************//
// https://github.com/rs/cors
e.Use(cors.Default().Handler)
// https://github.com/thoas/stats
s := stats.New()
e.Use(s.Handler)
// Route
e.Get("/stats", func(c *echo.Context) {
c.JSON(200, s.Data())
})
// Serve index file
e.Index("public/index.html")
// Serve static files
e.Static("/js", "public/js")
//************//
// Routes //
//************//
e.Post("/users", createUser)
e.Get("/users", getUsers)
e.Get("/users/:id", getUser)
//***************//
// Templates //
//***************//
t := &Template{
// Cached templates
templates: template.Must(template.ParseFiles("public/tpl/welcome.tpl")),
}
e.Renderer(t)
e.Get("/welcome", welcome)
//***********//
// Group //
//***********//
// Group with parent middleware
a := e.Group("/admin")
a.Use(func(c *echo.Context) {
// Security middleware
})
a.Get("", func(c *echo.Context) {
c.String(http.StatusOK, "Welcome admin!")
})
// Group with no parent middleware
g := e.Group("/files", func(c *echo.Context) {
// Security middleware
})
g.Get("", func(c *echo.Context) {
c.String(http.StatusOK, "Your files!")
})
// Start server // Start server
e.Run(":8080") e.Run(":8080")
} }
func init() {
users = map[string]user{
"1": user{
ID: "1",
Name: "Wreck-It Ralph",
},
}
}
``` ```
## License ## License

View File

@ -34,7 +34,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}) error {
} }
func welcome(c *echo.Context) { func welcome(c *echo.Context) {
c.Render("welcome", "Joe") c.Render(http.StatusOK, "welcome", "Joe")
} }
func createUser(c *echo.Context) { func createUser(c *echo.Context) {