1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
High performance, minimalist Go web framework https://echo.labstack.com/
Go to file
2016-09-01 08:55:27 -07:00
_fixture Left over 2016-01-28 23:56:09 -08:00
.github Update ISSUE_TEMPLATE.md 2016-08-29 13:45:46 -07:00
context Closes #626, #625 2016-08-19 14:07:07 -07:00
engine Use response adapter in engine/standard 2016-08-22 14:47:48 -07:00
log Added JSON logging 2016-06-08 17:16:03 -07:00
middleware Options for redirect middleware 2016-08-31 20:10:14 -07:00
test New redirect middleware 2016-08-27 13:03:40 -07:00
.editorconfig adding editorconfig for happiness 2015-06-23 22:43:07 -07:00
.gitattributes Moved examples to recipes 2015-10-08 20:10:55 -07:00
.gitignore Changes based on comments for #430 2016-04-16 15:56:09 -07:00
.travis.yml Update .travis.yml 2016-08-17 11:08:40 -04:00
binder_test.go Fixed #574 2016-06-23 19:49:01 -07:00
binder.go Closes #610, Closes #611 2016-08-17 11:07:25 -04:00
context_test.go Inline feature (#637) 2016-08-22 14:46:21 -07:00
context.go Inline feature (#637) 2016-08-22 14:46:21 -07:00
cookie_test.go Test coverage for cookie.go and context.go 2016-06-05 22:25:34 -07:00
cookie.go Added cookie.go 2016-05-06 07:39:41 -07:00
echo_test.go Exposed error handlers 2016-07-05 08:43:46 -07:00
echo.go Closes #364 2016-08-18 11:27:37 -07:00
glide.lock Updated deps 2016-08-19 08:19:35 -07:00
glide.yaml Added JWT middleware 2016-04-25 10:58:11 -07:00
group_test.go Fix issue with middleware from one route leaking into the middleware of other routes 2016-06-16 07:29:25 -07:00
group.go Fixed #622 2016-08-11 16:25:03 -07:00
LICENSE Refactored variable names 2016-04-24 10:22:15 -07:00
README.md Update README.md 2016-09-01 08:55:27 -07:00
router_test.go About context #500 2016-06-05 13:47:59 -07:00
router.go Closes #364 2016-08-18 11:27:37 -07:00

Echo GoDoc License Build Status Coverage Status Join the chat at https://gitter.im/labstack/echo Twitter

Fast and unfancy HTTP server framework for Go (Golang). Up to 10x faster than the rest.

Feature Overview

  • 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 -u github.com/labstack/echo

Hello, World!

Create server.go

package main

import (
	"net/http"
	"github.com/labstack/echo"
	"github.com/labstack/echo/engine/standard"
)

func main() {
	e := echo.New()
	e.GET("/", func(c echo.Context) error {
		return c.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

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

Query Parameters

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

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

Form application/x-www-form-urlencoded

POST /save

name value
name Joe Smith
email joe@labstack.com
func save(c echo.Context) error {
	// Get name and email
	name := c.FormValue("name")
	email := c.FormValue("email")
}

Form multipart/form-data

POST /save

name value
name Joe Smith
email joe@labstack.com
avatar avatar
func save(c echo.Context) error {
	// Get name and email
	name := c.FormValue("name")
	email := c.FormValue("email")
	// Get avatar
	avatar, err := c.FormFile("avatar")
	if err != nil {
		return err
	}

	// Source
	src, err := avatar.Open()
	if err != nil {
		return err
	}
	defer src.Close()

	// Destination
	dst, err := os.Create(avatar.Filename)
	if err != nil {
		return err
	}
	defer dst.Close()

	// Copy
	if _, err = io.Copy(dst, src); err != nil {
		return err
	}

	return c.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.
type User struct {
	Name  string `json:"name" xml:"name" form:"name"`
	Email string `json:"email" xml:"email" form:"email"`
}

e.POST("/users", func(c echo.Context) error {
	u := new(User)
	if err := c.Bind(u); err != nil {
		return err
	}
	return c.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")
Learn More

Template Rendering

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) bool {
	if username == "joe" && password == "secret" {
		return true
	}
	return false
}))

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

Built-in Middleware

Middleware Description
BodyLimit Limit request body
Logger Log HTTP requests
Recover Recover from panics
Gzip Send gzip HTTP response
BasicAuth HTTP basic authentication
JWTAuth JWT authentication
Secure Protection against attacks
CORS Cross-Origin Resource Sharing
CSRF Cross-Site Request Forgery
Static Serve static files
HTTPSRedirect Redirect HTTP requests to HTTPS
HTTPSWWWRedirect Redirect HTTP requests to WWW HTTPS
WWWRedirect Redirect non WWW requests to WWW
NonWWWRedirect Redirect WWW requests to non WWW
AddTrailingSlash Add trailing slash to the request URI
RemoveTrailingSlash Remove trailing slash from the request URI
MethodOverride Override request method
Learn More

Third-party Middleware

Middleware Description
echoperm Keeping track of users, login states and permissions.
echopprof Adapt net/http/pprof to labstack/echo.

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