package
0.0.0-20240616025555-8b0bf5e7897e
Repository: https://github.com/xiexianbin/go-echo-demo.git
Documentation: pkg.go.dev

# README

v0 for study

route

e.POST("/users", saveUser)
e.GET("/users/:id", getUser)
e.PUT("/users/:id", updateUser)
e.DELETE("/users/:id", deleteUser)

Content

// https://echo.labstack.com/docs/context
package v0

import "github.com/labstack/echo"

// Define a custom context
type CustomContext struct {
	echo.Context
}

func (c *CustomContext) Foo() {
	println("foo")
}

func (c *CustomContext) Bar() {
	println("bar")
}

// Create a middleware to extend default context
// e := echo.New()
// e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
// 	return func(c echo.Context) error {
// 		cc := &CustomContext{c}
// 		return next(cc)
// 	}
// })

// Use in handler
// e.GET("/", func(c echo.Context) error {
// 	cc := c.(*CustomContext)
// 	cc.Foo()
// 	cc.Bar()
// 	return cc.String(200, "OK")
// })

Customization

Cookies

Error Handling

Start Server

IP Address

Request

Response

Routing

Static Files

Templates

Testing

Middleware

// Root level middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())

// Group level middleware
g := e.Group("/admin")
g.Use(middleware.BasicAuth(func(username, password string, c echo.Context)(error, bool) {
  if username == "u1" && password == "pwd" {
    return nil, true
  }
  rturn nil, false
}))

// Route level middleware
track := func(next echo.HandlerFunc) echo.HandlerFunc {
  return func(c echo.Context) error {
    // ...
    return next(c)
  }
}
e.GET("/users", func(c echo.Context) error {
  return c.String(http.StatusOK, "ok")
}, track)

WebSocket

# Functions

DeleteFoo example @Summary delete foo by path id @Description ..
GetFoo1 example @Summary Read foo by query id @Description https://echo.labstack.com/docs/quick-start#query-parameters @Tags foo @Accept json @Produce json @Param id query int true "Foo Id" @Success 200 {object} string "success" @Failure 400 {object} app.Response "We need ID!!" @Failure 404 {object} app.Response "Can not find ID" @Router /v0/foo [get].
GetFoo2 example @Summary Read foo by url id @Description https://echo.labstack.com/docs/quick-start#path-parameters @Tags foo @Accept json @Produce json @Param id path int true "Foo Id" @Success 200 {object} app.Response{data=string} "success" @Failure 400 {object} app.Response "We need ID!!" @Failure 404 {object} app.Response "Can not find ID" @Router /v0/foo/{id} [get].
Helloworld godoc @Summary helloworld example @Schemes @Description say helloworld @Tags foo @Accept json @Produce json @Success 200 {object} app.Response{data=string} "Helloworld" @Router /v0/helloworld [get].
SaveFoo1 example @Summary save foo by application/form-data @Description https://echo.labstack.com/docs/quick-start#form-applicationx-www-form-urlencoded @Description https://echo.labstack.com/docs/quick-start#form-multipartform-data @Description curl -F "name=xiexianbin" -F "avatar=@/path/to/your/avatar.png" http://localhost:1323/foo @Tags foo @Accept multipart/form-data @Produce json @Param name formData string true "Foo Id" @Param avatar formData file true "Foo Id" @Success 200 {object} app.Response{data=string} "success" @Failure 400 {object} app.Response "We need ID!!" @Failure 404 {object} app.Response "Can not find ID" @Router /v0/foo [post].
SavetFoo2 example @Summary save foo by json @Description ..

# Structs

Foo foo struct for demo ref https://github.com/swaggo/swag/blob/master/README.md#user-defined-structure-with-an-array-type.