diff --git a/README.md b/README.md index 3e9a020f..8f45b297 100644 --- a/README.md +++ b/README.md @@ -71,331 +71,10 @@ $ go get github.com/labstack/echo ##[Examples](https://github.com/labstack/echo/tree/master/examples) -###[Hello, World!](https://github.com/labstack/echo/tree/master/examples/hello) - -```go -package main - -import ( - "net/http" - - "github.com/labstack/echo" - mw "github.com/labstack/echo/middleware" -) - -// Handler -func hello(c *echo.Context) error { - return c.String(http.StatusOK, "Hello, World!\n") -} - -func main() { - // Echo instance - e := echo.New() - - // Middleware - e.Use(mw.Logger()) - e.Use(mw.Recover()) - - // Routes - e.Get("/", hello) - - // Start server - e.Run(":1323") -} -``` - -###[CRUD](https://github.com/labstack/echo/tree/master/examples/crud) - -```go -package main - -import ( - "net/http" - "strconv" - - "github.com/labstack/echo" - mw "github.com/labstack/echo/middleware" -) - -type ( - user struct { - ID int - Name string - } -) - -var ( - users = map[int]*user{} - seq = 1 -) - -//---------- -// Handlers -//---------- - -func createUser(c *echo.Context) error { - u := &user{ - ID: seq, - } - if err := c.Bind(u); err != nil { - return err - } - users[u.ID] = u - seq++ - return c.JSON(http.StatusCreated, u) -} - -func getUser(c *echo.Context) error { - id, _ := strconv.Atoi(c.Param("id")) - return c.JSON(http.StatusOK, users[id]) -} - -func updateUser(c *echo.Context) error { - u := new(user) - if err := c.Bind(u); err != nil { - return err - } - id, _ := strconv.Atoi(c.Param("id")) - users[id].Name = u.Name - return c.JSON(http.StatusOK, users[id]) -} - -func deleteUser(c *echo.Context) error { - id, _ := strconv.Atoi(c.Param("id")) - delete(users, id) - return c.NoContent(http.StatusNoContent) -} - -func main() { - e := echo.New() - - // Middleware - e.Use(mw.Logger()) - e.Use(mw.Recover()) - - // Routes - e.Post("/users", createUser) - e.Get("/users/:id", getUser) - e.Patch("/users/:id", updateUser) - e.Delete("/users/:id", deleteUser) - - // Start server - e.Run(":1323") -} -``` - -###[Website](https://github.com/labstack/echo/tree/master/examples/website) - -```go -package main - -import ( - "io" - "net/http" - - "html/template" - - "github.com/labstack/echo" - mw "github.com/labstack/echo/middleware" - "github.com/rs/cors" - "github.com/thoas/stats" -) - -type ( - // Template provides HTML template rendering - Template struct { - 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) -} - -//---------- -// Handlers -//---------- - -func welcome(c *echo.Context) error { - return c.Render(http.StatusOK, "welcome", "Joe") -} - -func createUser(c *echo.Context) error { - u := new(user) - if err := c.Bind(u); err != nil { - return err - } - users[u.ID] = *u - return c.JSON(http.StatusCreated, u) -} - -func getUsers(c *echo.Context) error { - return c.JSON(http.StatusOK, users) -} - -func getUser(c *echo.Context) error { - return c.JSON(http.StatusOK, users[c.P(0)]) -} - -func main() { - e := echo.New() - - // Middleware - e.Use(mw.Logger()) - e.Use(mw.Recover()) - - //------------------------ - // Third-party middleware - //------------------------ - - // 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) error { - return c.JSON(http.StatusOK, s.Data()) - }) - - // Serve index file - e.Index("public/index.html") - - // Serve favicon - e.Favicon("public/favicon.ico") - - // Serve static files - e.Static("/scripts/", "public/scripts") - - //-------- - // 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/views/welcome.html")), - } - e.SetRenderer(t) - e.Get("/welcome", welcome) - - //------- - // Group - //------- - - // Group with parent middleware - a := e.Group("/admin") - a.Use(func(c *echo.Context) error { - // Security middleware - return nil - }) - a.Get("", func(c *echo.Context) error { - return c.String(http.StatusOK, "Welcome admin!") - }) - - // Group with no parent middleware - g := e.Group("/files", func(c *echo.Context) error { - // Security middleware - return nil - }) - g.Get("", func(c *echo.Context) error { - return c.String(http.StatusOK, "Your files!") - }) - - // Start server - e.Run(":1323") -} - -func init() { - users = map[string]user{ - "1": user{ - ID: "1", - Name: "Wreck-It Ralph", - }, - } -} -``` - -###[Middleware](https://github.com/labstack/echo/tree/master/examples/middleware) - -```go -package main - -import ( - "net/http" - - "github.com/labstack/echo" - mw "github.com/labstack/echo/middleware" -) - -// Handler -func hello(c *echo.Context) error { - return c.String(http.StatusOK, "Hello, World!\n") -} - -func main() { - // Echo instance - e := echo.New() - - // Debug mode - e.SetDebug(true) - - //------------ - // Middleware - //------------ - - // Logger - e.Use(mw.Logger()) - - // Recover - e.Use(mw.Recover()) - - // Basic auth - e.Use(mw.BasicAuth(func(u, p string) bool { - if u == "joe" && p == "secret" { - return true - } - return false - })) - - //------- - // Slash - //------- - - e.Use(mw.StripTrailingSlash()) - - // or - - // e.Use(mw.RedirectToSlash()) - - // Gzip - e.Use(mw.Gzip()) - - // Routes - e.Get("/", hello) - - // Start server - e.Run(":1323") -} -``` +- [Hello, World!](https://github.com/labstack/echo/tree/master/examples/hello) +- [CRUD](https://github.com/labstack/echo/tree/master/examples/crud) +- [Website](https://github.com/labstack/echo/tree/master/examples/website) +- [Middleware](https://github.com/labstack/echo/tree/master/examples/middleware) ##[Guide](http://labstack.github.io/echo/guide) diff --git a/echo.go b/echo.go index bda5a45b..4af0f4c9 100644 --- a/echo.go +++ b/echo.go @@ -121,9 +121,9 @@ var ( ) func NewHTTPError(code int, msgs ...string) *HTTPError { - he := &HTTPError{Code: code} - if len(msgs) == 0 { - he.Message = http.StatusText(code) + he := &HTTPError{Code: code, Message: http.StatusText(code)} + for _, m := range msgs { + he.Message = m } return he } diff --git a/examples/website/server.go b/examples/website/server.go index 286340c2..40d95d42 100644 --- a/examples/website/server.go +++ b/examples/website/server.go @@ -87,7 +87,7 @@ func main() { e.Favicon("public/favicon.ico") // Serve static files - e.Static("/scripts/", "public/scripts") + e.Static("/scripts", "public/scripts") //-------- // Routes