Giter Site home page Giter Site logo

mewben / echo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from labstack/echo

0.0 2.0 0.0 2.35 MB

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

Home Page: https://labstack.com/echo

License: MIT License

HTML 0.16% Go 99.84%

echo's Introduction

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.
  • 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 [email protected]
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 [email protected]
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 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")

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
CORS Cross-Origin Resource Sharing
Static Serve static files
AddTrailingSlash Add trailing slash to the request URI
RemoveTrailingSlash Remove trailing slash from the request URI
MethodOverride Override request method

Third-party Middleware

Middleware Description
echoperm Keeping track of users, login states and permissions.

Next

Need help?

Support Us

  • โญ the project
  • Donate
  • ๐ŸŒŽ spread the word
  • Contribute to the project

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

echo's People

Contributors

01walid avatar andreas-bergstrom avatar axdg avatar caarlos0 avatar captaincodeman avatar chrisseto avatar dlsniper avatar gavv avatar gitter-badger avatar hasty avatar hownowbrowncow avatar hvnsweeting avatar jbub avatar markbates avatar matrika avatar mertenvg avatar mre avatar mtojek avatar nptenable avatar o1egl avatar obivan avatar rojtjo avatar roth1002 avatar sekimura avatar swenson avatar syntaqx avatar tanema avatar tossp avatar tylerb avatar vishr avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.