From 2cb612ff57cbcc5dcf11981bc317b17a811f168f Mon Sep 17 00:00:00 2001 From: Antonio Pagano Date: Tue, 1 Nov 2016 10:33:56 -0500 Subject: [PATCH] changes README's hello-world example for v3 (#698) * [doc] adding graceful documentation and example. * adding myself to the maintainers list and minor comment formatting change * [doc] updating code on the guides/context.md and guides/cookies.md to use v3 code. * [doc] updating error-handling and request to v3 codebase * [doc] updating templates documentation * [doc] cleaning hello-world documentation for v3 * [content] adding website index content based on #667 --- README.md | 7 +++- website/content/index.md | 82 +++++++++++++++++++++++++++------------- 2 files changed, 60 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 3d5e00c9..d53c3ee6 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,8 @@ package main import ( "net/http" + "github.com/labstack/echo" - "github.com/labstack/echo/engine/standard" ) func main() { @@ -54,7 +54,10 @@ func main() { e.GET("/", func(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!") }) - e.Run(standard.New(":1323")) + + if err := e.Start(":1323"); err != nil { + e.Logger.Fatal(err.Error()) + } } ``` diff --git a/website/content/index.md b/website/content/index.md index 3c911537..4f776035 100644 --- a/website/content/index.md +++ b/website/content/index.md @@ -57,7 +57,10 @@ func main() { e.GET("/", func(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!") }) - e.Run(standard.New(":1323")) + + if err := e.Start(":1323"); err != nil { + e.Logger.Fatal(err.Error()) + } } ``` @@ -85,8 +88,10 @@ e.DELETE("/users/:id", deleteUser) func getUser(c echo.Context) error { // User ID from path `users/:id` id := c.Param("id") + return c.String(http.StatusOK, id) } ``` +Browse to http://localhost:1323/users/Joe and you should see 'Joe' on the page. ### Query Parameters @@ -97,9 +102,13 @@ func show(c echo.Context) error { // Get team and member from the query string team := c.QueryParam("team") member := c.QueryParam("member") + + return c.String(http.StatusOK, "team:" + team + ", member:" + member) } ``` +Browse to http://localhost:1323/show?team=x-men&member=wolverine and you should see 'team:x-men, member:wolverine' on the page. + ### Form `application/x-www-form-urlencoded` `POST` `/save` @@ -114,9 +123,17 @@ func save(c echo.Context) error { // Get name and email name := c.FormValue("name") email := c.FormValue("email") + + return c.String(http.StatusOK, "name:" + name + ", email:" + email) } ``` +Run the following command. +```sh +$ curl -F "name=Joe Smith" -F "email=joe@labstack.com" http://localhost:1323/save +// => name:Joe Smith, email:joe@labstack.com +``` + ### Form `multipart/form-data` `POST` `/save` @@ -124,43 +141,54 @@ func save(c echo.Context) error { name | value :--- | :--- name | Joe Smith -email | joe@labstack.com avatar | avatar ```go func save(c echo.Context) error { - // Get name and email + // Get name name := c.FormValue("name") - email := c.FormValue("email") // Get avatar avatar, err := c.FormFile("avatar") - if err != nil { - return err - } + if err != nil { + return err + } + + // Source + src, err := avatar.Open() + if err != nil { + return err + } + defer src.Close() + + // Destination + dst, err := os.Create(avatar.Filename) + if err != nil { + return err + } + defer dst.Close() + + // Copy + if _, err = io.Copy(dst, src); err != nil { + return err + } - // Source - src, err := avatar.Open() - if err != nil { - return err - } - defer src.Close() - - // Destination - dst, err := os.Create(avatar.Filename) - if err != nil { - return err - } - defer dst.Close() - - // Copy - if _, err = io.Copy(dst, src); err != nil { - return err - } - - return c.HTML(http.StatusOK, "Thank you!") + return c.HTML(http.StatusOK, "Thank you! " + name + "") } ``` +Run the following command. +```sh +$ curl -F "name=Joe Smith" -F "avatar=@/path/to/your/avatar.png" http://localhost:1323/save +// => Thank you! Joe Smith +``` + +To check the uploaded image, run the following command. +```sh +cd +ls avatar.png +// => avatar.png +``` + ### Handling Request - Bind `JSON` or `XML` or `form` payload into Go struct based on `Content-Type` request header.