1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-05 22:53:57 +02:00
echo/website/docs/index.md
Vishal Rana b951910c4a Docs updated
Signed-off-by: Vishal Rana <vr@labstack.com>
2015-04-20 13:38:30 -07:00

3.3 KiB

Echo

Simple and performant HTTP systems!


Overview

Echo is a fast HTTP router (zero memory allocation) and micro web framework in Go.

Features

  • Fast HTTP router which smartly resolves conflicting routes.
  • Extensible middleware/handler, supports:
    • Middleware
      • func(*echo.Context)
      • func(*echo.Context) error
      • func(echo.HandlerFunc) echo.HandlerFunc
      • http.Handler
      • http.HandlerFunc
      • func(http.ResponseWriter, *http.Request)
      • func(http.ResponseWriter, *http.Request) error
    • Handler
      • func(*echo.Context)
      • func(*echo.Context) error
      • http.Handler
      • http.HandlerFunc
      • func(http.ResponseWriter, *http.Request)
      • func(http.ResponseWriter, *http.Request) error
  • Sub routing with groups.
  • Handy encoding/decoding functions.
  • Serve static files, including index.
  • Centralized HTTP error handling.
  • Use a customized function to bind request body to a Go type.
  • Register a view render so you can use any HTML templating engine.

Installation

  • Go > 1.4.x
  • go get github.com/labstack/echo

##Examples

Hello, World!

Create server.go with the following content:

package main

import (
	"net/http"

	"github.com/labstack/echo"
)

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

func main() {
	e := echo.New()

	// Middleware
	e.Use(echo.Logger)

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

	// Start server
	e.Run(":4444")
}

go run server.go & browse to http://localhost:8080

CRUD - Create, read, update and delete.

  • Create user curl -X POST -H "Content-Type: application/json" -d '{"name":"Joe"}' http://localhost:4444/users
  • Get user curl http://localhost:4444/users/1
  • Update user: Change user's name to Sid curl -X PATCH -H "Content-Type: application/json" -d '{"name":"Sid"}' http://localhost:4444/users/1
  • Delete user curl -X DELETE http://localhost:4444/users/1
package main

import (
	"net/http"
	"strconv"

	"github.com/labstack/echo"
)

type (
	user struct {
		ID   int
		Name string
	}
)

var (
	users = map[int]*user{}
	seq   = 1
)

//----------
// Handlers
//----------

func createUser(c *echo.Context) error {
	u := &user{
		ID: seq,
	}
	if err := c.Bind(u); err != nil {
		return err
	}
	users[u.ID] = u
	seq++
	return c.JSON(http.StatusCreated, u)
}

func getUser(c *echo.Context) error {
	id, _ := strconv.Atoi(c.Param("id"))
	return c.JSON(http.StatusOK, users[id])
}

func updateUser(c *echo.Context) error {
	u := new(user)
	if err := c.Bind(u); err != nil {
		return err
	}
	id, _ := strconv.Atoi(c.Param("id"))
	users[id].Name = u.Name
	return c.JSON(http.StatusOK, users[id])
}

func deleteUser(c *echo.Context) error {
	id, _ := strconv.Atoi(c.Param("id"))
	delete(users, id)
	return c.NoContent(http.StatusNoContent)
}

func main() {
	e := echo.New()

	// Middleware
	e.Use(echo.Logger)

	// Routes
	e.Post("/users", createUser)
	e.Get("/users/:id", getUser)
	e.Patch("/users/:id", updateUser)
	e.Delete("/users/:id", deleteUser)

	// Start server
	e.Run(":4444")
}

Contribute

  • Report issues
  • Suggest new features
  • Participate in discussion
  • Improve documentation

License

MIT