编程

echo: 高性能、极简的Go语言 Web 框架

713 2023-04-22 09:34:00

特性

  • 优化的HTTP路由器,可智能地对路由进行优先级排序
  • 创建健壮及可伸缩的 RESTful API
  • API 分组
  • 可扩展的中间件框架
  • 在root、group或路由级别中定义中间件
  • JSON, XML 及表单的数据绑定
  • 方便的函数,用以发送各种HTTP响应
  • 中心化 HTTP 错误处理
  • 可使用任何模板引擎渲染模板
  • 可自定义 logger 格式
  • 高度定制化
  • 通过 Let’s Encrypt 自动化TLS
  • HTTP/2 支持

安装

// go get github.com/labstack/echo/{version}
go get github.com/labstack/echo/v4

示例

package main

import (
  "github.com/labstack/echo/v4"
  "github.com/labstack/echo/v4/middleware"
  "net/http"
)

func main() {
  // Echo instance
  e := echo.New()

  // Middleware
  e.Use(middleware.Logger())
  e.Use(middleware.Recover())

  // Routes
  e.GET("/", hello)

  // Start server
  e.Logger.Fatal(e.Start(":1323"))
}

// Handler
func hello(c echo.Context) error {
  return c.String(http.StatusOK, "Hello, World!")
}

官方中间件仓库

以下是 Echo 团队维护的中间件。

RepositoryDescription
github.com/labstack/echo-jwtJWT middleware
github.com/labstack/echo-contribcasbin, gorilla/sessions, jaegertracing, prometheus, pprof, zipkin middlewares

第三方中间件仓库

添加第三方中间件时请慎重。Echo 团队没有时间及人力保证下列中间件的安全性和质量。

RepositoryDescription
deepmap/oapi-codegenAutomatically generate RESTful API documentation with OpenAPI Client and Server Code Generator
github.com/swaggo/echo-swaggerAutomatically generate RESTful API documentation with Swagger 2.0.
github.com/ziflex/lechoZerolog logging library wrapper for Echo logger interface.
github.com/brpaz/echozapUber´s Zap logging library wrapper for Echo logger interface.
github.com/darkweak/souin/plugins/echoHTTP cache system based on Souin to automatically get your endpoints cached. It supports some distributed and non-distributed storage systems depending your needs.
github.com/mikestefanello/pagodaRapid, easy full-stack web development starter kit built with Echo.
github.com/go-woo/protoc-gen-echoProtoBuf generate Echo server side code