1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00
High performance, minimalist Go web framework https://echo.labstack.com/
Go to file
Vishal Rana c5a3575d4c updated migration guide
Signed-off-by: Vishal Rana <vr@labstack.com>
2016-11-14 22:54:09 -08:00
_fixture Test cased for Echo#Start/Shutdown 2016-09-24 17:50:38 -07:00
.github Update ISSUE_TEMPLATE.md 2016-08-29 13:45:46 -07:00
middleware fixed #712 2016-11-12 20:34:52 -08:00
recipe recipes: static middleware changes 2016-10-25 16:30:44 -07:00
website updated migration guide 2016-11-14 22:54:09 -08: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 cors: not checking for origin header 2016-11-12 14:05:41 -08:00
.travis.yml First commit to v3, #665 2016-09-22 22:56:00 -07:00
binder_test.go First commit to v3, #665 2016-09-22 22:56:00 -07:00
binder.go First commit to v3, #665 2016-09-22 22:56:00 -07:00
context_test.go updated migration guide 2016-11-14 22:54:09 -08:00
context.go updated migration guide 2016-11-14 22:54:09 -08:00
echo_test.go updated recipes 2016-10-20 16:57:31 -07:00
echo.go fixed #719 2016-11-12 11:35:46 -08:00
glide.lock - Removed Context#ServeContent 2016-10-25 14:53:47 -07:00
glide.yaml updated recipes 2016-10-20 16:57:31 -07:00
group_test.go removed deprecated functions group.go 2016-10-13 14:04:08 -07:00
group.go fixed #701 2016-10-30 15:03:18 -07:00
LICENSE Refactored variable names 2016-04-24 10:22:15 -07:00
logger.go logger new api, updated recipes 2016-10-23 13:37:04 -07:00
README.md updated migration guide 2016-11-14 22:54:09 -08:00
response.go First commit to v3, #665 2016-09-22 22:56:00 -07:00
router_test.go updated migration guide 2016-11-14 22:54:09 -08:00
router.go updated migration guide 2016-11-14 22:54:09 -08:00

[Echo v3] (https://echo.labstack.com) 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).

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
  • Automatic TLS via Let’s Encrypt
  • Built-in graceful shutdown

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"
)

func main() {
	e := echo.New()
	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "Hello, World!")
	})

	if err := e.Start(":1323"); err != nil {
		e.Logger.Fatal(err.Error())
	}
}

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

// e.GET("/users/:id", getUser)
func getUser(c echo.Context) error {
  	// User ID from path `users/:id`
  	id := c.Param("id")
	return c.String(http.StatusOK, id)
}

Browse to http://localhost:1323/users/Joe and you should see 'Joe' on the page.

Query Parameters

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

//e.GET("/show", show)
func show(c echo.Context) error {
	// Get team and member from the query string
	team := c.QueryParam("team")
	member := c.QueryParam("member")
	return c.String(http.StatusOK, "team:" + team + ", member:" + member)
}

Browse to http://localhost:1323/show?team=x-men&member=wolverine and you should see 'team:x-men, member:wolverine' on the page.

Form application/x-www-form-urlencoded

POST /save

name value
name Joe Smith
email joe@labstack.com
// e.POST("/save", save)
func save(c echo.Context) error {
	// Get name and email
	name := c.FormValue("name")
	email := c.FormValue("email")
	return c.String(http.StatusOK, "name:" + name + ", email:" + email)
}

Run the following command:

$ curl -F "name=Joe Smith" -F "email=joe@labstack.com" http://localhost:1323/save
// => name:Joe Smith, email:joe@labstack.com

Form multipart/form-data

POST /save

name value
name Joe Smith
avatar avatar
func save(c echo.Context) error {
	// Get name
	name := c.FormValue("name")
	// 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! " + name + "</b>")
}

Run the following command.

$ curl -F "name=Joe Smith" -F "avatar=@/path/to/your/avatar.png" http://localhost:1323/save
// => <b>Thank you! Joe Smith</b>

For checking uploaded image, run the following command.

cd <project directory>
ls avatar.png
// => avatar.png

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
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