Created
April 25, 2019 17:36
-
-
Save ganlvtech/29919b1f96fefaf33a4786c8fe274165 to your computer and use it in GitHub Desktop.
go-labstack-echo-demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "io" | |
| "net/http" | |
| "os" | |
| "github.com/labstack/echo" | |
| ) | |
| type User struct { | |
| Name string `json:"name" xml:"name" form:"name" query:"name"` | |
| Email string `json:"email" xml:"email" form:"email" query:"email"` | |
| } | |
| func index(c echo.Context) error { | |
| return c.String(http.StatusOK, "Hello, World!") | |
| } | |
| func save(c echo.Context) error { | |
| name := c.FormValue("name") | |
| email := c.FormValue("email") | |
| return c.String(http.StatusOK, "Name:"+name+", Email:"+email) | |
| } | |
| func search(c echo.Context) error { | |
| q := c.QueryParam("q") | |
| return c.String(http.StatusOK, "Searching: "+q) | |
| } | |
| func avatarSave(c echo.Context) error { | |
| name := c.FormValue("name") | |
| avatar, err := c.FormFile("avatar") | |
| if err != nil { | |
| return err | |
| } | |
| src, err := avatar.Open() | |
| if err != nil { | |
| return err | |
| } | |
| defer src.Close() | |
| dst, err := os.Create(avatar.Filename) | |
| if err != nil { | |
| return err | |
| } | |
| defer dst.Close() | |
| if _, err = io.Copy(dst, src); err != nil { | |
| return err | |
| } | |
| return c.HTML(http.StatusOK, "<b>Thank you! "+name+"</b>") | |
| } | |
| func user(c echo.Context) error { | |
| id := c.Param("id") | |
| return c.String(http.StatusOK, id) | |
| } | |
| func userSave(c echo.Context) error { | |
| u := new(User) | |
| if err := c.Bind(u); err != nil { | |
| return err | |
| } | |
| return c.JSON(http.StatusCreated, u) | |
| } | |
| func raisePanic(c echo.Context) error { | |
| panic("Self triggered panic") | |
| return c.String(http.StatusOK, "Panic!!!") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "os" | |
| "github.com/labstack/echo" | |
| "github.com/labstack/echo/middleware" | |
| ) | |
| func main() { | |
| e := echo.New() | |
| e.Use(middleware.Logger()) | |
| e.Use(middleware.Recover()) | |
| e.GET("/", index) | |
| e.POST("/", save) | |
| e.GET("/search", search) | |
| e.POST("/avatar", avatarSave) | |
| e.GET("/user/:id", user) | |
| e.POST("/user", userSave) | |
| e.GET("/panic", raisePanic) | |
| e.Static("/static", "static") | |
| g := e.Group("/admin") | |
| g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) { | |
| if username == "admin" && password == "admin" { | |
| return true, nil | |
| } | |
| return false, nil | |
| })) | |
| e.Logger.Fatal(e.Start(":" + os.Args[1])) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment