Skip to content

Repository files navigation

NOTICE

Echo is a fast and unfancy HTTP server framework for Go (Golang). Up to 10x faster than the rest.

Features

  • Optimized HTTP router which smartly prioritize routes.
  • Build robust and scalable RESTful APIs.
  • Run with standard HTTP server or FastHTTP server.
  • Group APIs.
  • Extensible middleware framework.
  • Define middleware at root, group or route level.
  • Data binding for JSON, XML and form payload.
  • Handy functions to send variety of HTTP responses.
  • Centralized HTTP error handling.
  • Template rendering with any template engine.
  • Define your format for the logger.
  • Highly customizable.

Performance

Performance

Quick Start

Installation

$ go get github.com/labstack/echo/...

Hello, World!

Create server.go

package main
import (
"net/http""github.com/labstack/echo""github.com/labstack/echo/engine/standard"
)
funcmain() {
e:=echo.New()
e.GET("/", func(c echo.Context) error {
returnc.String(http.StatusOK, "Hello, World!")
})
e.Run(standard.New(":1323"))
}

Start server

$ go run server.go

Browse to http://localhost:1323 and you should see Hello, World! on the page.

Routing

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

Path Parameters

funcgetUser(c echo.Context) error {
// User ID from path `users/:id`id:=c.Param("id")
}

Query Parameters

/show?team=x-men&member=wolverine

funcshow(c echo.Context) error {
// Get team and member from the query stringteam:=c.QueryParam("team")
member:=c.QueryParam("member")
}

Form application/x-www-form-urlencoded

POST/save

namevalue
nameJoe Smith
emailjoe@labstack.com
funcsave(c echo.Context) error {
// Get name and emailname:=c.FormValue("name")
email:=c.FormValue("email")
}

Form multipart/form-data

POST/save

namevalue
nameJoe Smith
emailjoe@labstack.com
avataravatar
funcsave(c echo.Context) error {
// Get name and emailname:=c.FormValue("name")
email:=c.FormValue("email")
// Get avataravatar, err:=c.FormFile("avatar")
iferr!=nil {
returnerr
}
// Sourcesrc, err:=avatar.Open()
iferr!=nil {
returnerr
}
defersrc.Close()
// Destinationdst, err:=os.Create(avatar.Filename)
iferr!=nil {
returnerr
}
deferdst.Close()
// Copyif_, err=io.Copy(dst, src); err!=nil {
returnerr
}
returnc.HTML(http.StatusOK, "<b>Thank you!</b>")
}

Handling Request

  • Bind JSON or XML or form payload into Go struct based on Content-Type request header.
  • Render response as JSON or XML with status code.
typeUserstruct {
Namestring`json:"name" xml:"name" form:"name"`Emailstring`json:"email" xml:"email" form:"email"`
}
e.POST("/users", func(c echo.Context) error {
u:=new(User)
iferr:=c.Bind(u); err!=nil {
returnerr
}
returnc.JSON(http.StatusCreated, u)
// or// return c.XML(http.StatusCreated, u)
})

Static Content

Server any file from static directory for path /static/*.

e.Static("/static", "static")

Middleware

// Root level middlewaree.Use(middleware.Logger())
e.Use(middleware.Recover())
// Group level middlewareg:=e.Group("/admin")
g.Use(middleware.BasicAuth(func(username, passwordstring) bool {
ifusername=="joe"&&password=="secret" {
returntrue
}
returnfalse
}))
// Route level middlewaretrack:=func(next echo.HandlerFunc) echo.HandlerFunc {
returnfunc(c echo.Context) error {
println("request to /users")
returnnext(c)
}
}
e.GET("/users", func(c echo.Context) error {
returnc.String(http.StatusOK, "/users")
}, track)

Built-in Middleware

MiddlewareDescription
BodyLimitLimit request body
LoggerLog HTTP requests
RecoverRecover from panics
GzipSend gzip HTTP response
BasicAuthHTTP basic authentication
JWTAuthJWT authentication
SecureProtection against attacks
CORSCross-Origin Resource Sharing
StaticServe static files
AddTrailingSlashAdd trailing slash to the request URI
RemoveTrailingSlashRemove trailing slash from the request URI
MethodOverrideOverride request method

Third-party Middleware

MiddlewareDescription
echopermKeeping track of users, login states and permissions.

Next

Need help?

Support Us

Contribute

Use issues for everything

  • Report issues
  • Discuss on chat before sending a pull request
  • Suggest new features or enhancements
  • Improve/fix documentation

Credits

License

MIT

About

Echo is a fast and unfancy HTTP server framework for Go (Golang). Up to 10x faster than the rest.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages