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 e37b54e73f Removed a print statement
Signed-off-by: Vishal Rana <vr@labstack.com>
2016-04-06 11:07:56 -07:00
_fixture Left over 2016-01-28 23:56:09 -08:00
engine Updated README.md 2016-04-04 20:10:19 -07:00
middleware Removed a print statement 2016-04-06 11:07:56 -07:00
test Fixed #435 2016-03-28 06:57:31 -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 Logger as not an interface 2016-03-06 09:52:32 -08:00
.travis.yml Fixed group middleware 2016-02-20 08:11:02 -08:00
context_test.go Proper header and MIME constants 2016-04-06 07:29:47 -07:00
context.go Proper header and MIME constants 2016-04-06 07:29:47 -07:00
echo_test.go Fixed #441, #294 2016-04-02 14:24:51 -07:00
echo.go Proper header and MIME constants 2016-04-06 07:29:47 -07:00
glide.lock Fixed #441, #294 2016-04-02 14:24:51 -07:00
glide.yaml Fixed #437 2016-04-01 08:51:18 -07:00
group_test.go Fixed #441, #294 2016-04-02 14:24:51 -07:00
group.go Fixed #441, #294 2016-04-02 14:24:51 -07:00
LICENSE Update LICENSE 2015-04-13 17:32:22 -07:00
README.md Fixed #455 2016-04-06 08:21:31 -07:00
router_test.go Fixed #441, #294 2016-04-02 14:24:51 -07:00
router.go Fixed #441, #294 2016-04-02 14:24:51 -07:00

NOTICE

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

Echo is a fast and unfancy web 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.
  • 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"
)

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.FormParam("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.FormParam("email")

	//------------
	// Get avatar
	//------------

	avatar, err := c.FormFile("avatar")
	if err != nil {
		return err
	}
	src, err := file.Open()
	if err != nil {
		return err
	}
	defer src.Close()

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

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

Handling Request

  • Bind JSON or XML 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"`
	Email string `json:"email" xml:"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")

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)

More...

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