快速的HTTP router+微型 Web 框架:Echo

dwd4 9年前

Echo 是一个用 Go 语言开发的快速 HTTP 路由器(零内存分配)和微型 Web 框架。

特性:

  • Zippy router.

  • Extensible middleware/handler, supports:

    • func(*echo.Context)

    • http.Handler

    • http.HandlerFunc

    • func(http.ResponseWriter, *http.Request)

    • func(*echo.Context)

    • func(echo.HandlerFunc) echo.HandlerFunc

    • func(http.Handler) http.Handler

    • http.Handler

    • http.HandlerFunc

    • func(http.ResponseWriter, *http.Request)

    • Middleware

    • Handler

  • Handy encoding/decoding functions.

  • 支持静态文件处理

示例代码:

package main    import (      "net/http"        "github.com/labstack/echo"      mw "github.com/labstack/echo/middleware"      "github.com/rs/cors"      "github.com/thoas/stats"  )    type user struct {      ID   string `json:"id"`      Name string `json:"name"`  }    var users map[string]user    func init() {      users = map[string]user{          "1": user{              ID:   "1",              Name: "Wreck-It Ralph",          },      }  }    func createUser(c *echo.Context) {      u := new(user)      if c.Bind(u) {          users[u.ID] = *u          c.JSON(http.StatusCreated, u)      }  }    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() {      e := echo.New()        //*************************//      //   Built-in middleware   //      //*************************//      e.Use(mw.Logger)        //****************************//      //   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) {          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)        // Start server      e.Run(":8080")  }

项目主页:http://www.open-open.com/lib/view/home/1427853032459