Documentation ¶
Overview ¶
Package echo implements a fast and unfancy micro web framework for Go.
Example:
package main import ( "net/http" "github.com/labstack/echo" "github.com/labstack/echo/engine/standard" "github.com/labstack/echo/middleware" ) // Handler func hello() echo.HandlerFunc { return func(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!\n") } } func main() { // Echo instance e := echo.New() // Middleware e.Use(middleware.Logger()) e.Use(middleware.Recover()) // Routes e.Get("/", hello()) // Start server e.Run(standard.New(":1323")) }
Learn more at https://labstack.com/echo
Index ¶
- Constants
- Variables
- func ServeContent(req engine.Request, res engine.Response, f http.File, fi os.FileInfo) error
- type Binder
- type Context
- type Echo
- func (e *Echo) Any(path string, handler Handler, middleware ...Middleware)
- func (e *Echo) Connect(path string, h Handler, m ...Middleware)
- func (e *Echo) Debug() bool
- func (e *Echo) DefaultHTTPErrorHandler(err error, c Context)
- func (e *Echo) Delete(path string, h Handler, m ...Middleware)
- func (e *Echo) File(path, file string)
- func (e *Echo) Get(path string, h Handler, m ...Middleware)
- func (e *Echo) GetContext() Context
- func (e *Echo) Group(prefix string, m ...Middleware) (g *Group)
- func (e *Echo) Head(path string, h Handler, m ...Middleware)
- func (e *Echo) Logger() *log.Logger
- func (e *Echo) Match(methods []string, path string, handler Handler, middleware ...Middleware)
- func (e *Echo) Options(path string, h Handler, m ...Middleware)
- func (e *Echo) Patch(path string, h Handler, m ...Middleware)
- func (e *Echo) Post(path string, h Handler, m ...Middleware)
- func (e *Echo) Pre(middleware ...Middleware)
- func (e *Echo) Put(path string, h Handler, m ...Middleware)
- func (e *Echo) PutContext(c Context)
- func (e *Echo) Router() *Router
- func (e *Echo) Routes() []Route
- func (e *Echo) Run(s engine.Server)
- func (e *Echo) ServeHTTP(req engine.Request, res engine.Response)
- func (e *Echo) SetBinder(b Binder)
- func (e *Echo) SetDebug(on bool)
- func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler)
- func (e *Echo) SetLogLevel(l uint8)
- func (e *Echo) SetLogOutput(w io.Writer)
- func (e *Echo) SetLogPrefix(prefix string)
- func (e *Echo) SetRenderer(r Renderer)
- func (e *Echo) Static(prefix, root string)
- func (e *Echo) Trace(path string, h Handler, m ...Middleware)
- func (e *Echo) URI(handler Handler, params ...interface{}) string
- func (e *Echo) URL(h Handler, params ...interface{}) string
- func (e *Echo) Use(middleware ...Middleware)
- type Group
- func (g *Group) Any(path string, handler Handler, middleware ...Middleware)
- func (g *Group) Connect(path string, h Handler, m ...Middleware)
- func (g *Group) Delete(path string, h Handler, m ...Middleware)
- func (g *Group) Get(path string, h Handler, m ...Middleware)
- func (g *Group) Group(prefix string, m ...Middleware) *Group
- func (g *Group) Head(path string, h Handler, m ...Middleware)
- func (g *Group) Match(methods []string, path string, handler Handler, middleware ...Middleware)
- func (g *Group) Options(path string, h Handler, m ...Middleware)
- func (g *Group) Patch(path string, h Handler, m ...Middleware)
- func (g *Group) Post(path string, h Handler, m ...Middleware)
- func (g *Group) Put(path string, h Handler, m ...Middleware)
- func (g *Group) Trace(path string, h Handler, m ...Middleware)
- func (g *Group) Use(m ...Middleware)
- type HTTPError
- type HTTPErrorHandler
- type Handler
- type HandlerFunc
- type Middleware
- type MiddlewareFunc
- type Renderer
- type Route
- type Router
- type Validator
Constants ¶
const ( CONNECT = "CONNECT" DELETE = "DELETE" GET = "GET" HEAD = "HEAD" OPTIONS = "OPTIONS" PATCH = "PATCH" POST = "POST" PUT = "PUT" TRACE = "TRACE" )
HTTP methods
const ( ApplicationJSON = "application/json" ApplicationJSONCharsetUTF8 = ApplicationJSON + "; " + CharsetUTF8 ApplicationJavaScript = "application/javascript" ApplicationJavaScriptCharsetUTF8 = ApplicationJavaScript + "; " + CharsetUTF8 ApplicationXML = "application/xml" ApplicationXMLCharsetUTF8 = ApplicationXML + "; " + CharsetUTF8 ApplicationForm = "application/x-www-form-urlencoded" ApplicationProtobuf = "application/protobuf" ApplicationMsgpack = "application/msgpack" TextHTML = "text/html" TextHTMLCharsetUTF8 = TextHTML + "; " + CharsetUTF8 TextPlain = "text/plain" TextPlainCharsetUTF8 = TextPlain + "; " + CharsetUTF8 MultipartForm = "multipart/form-data" OctetStream = "application/octet-stream" )
Media types
const ( AcceptEncoding = "Accept-Encoding" Authorization = "Authorization" ContentDisposition = "Content-Disposition" ContentEncoding = "Content-Encoding" ContentLength = "Content-Length" ContentType = "Content-Type" LastModified = "Last-Modified" Location = "Location" Upgrade = "Upgrade" Vary = "Vary" WWWAuthenticate = "WWW-Authenticate" XForwardedFor = "X-Forwarded-For" XRealIP = "X-Real-IP" )
Headers
const (
CharsetUTF8 = "charset=utf-8"
)
Charset
Variables ¶
var ( ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType) ErrNotFound = NewHTTPError(http.StatusNotFound) ErrMethodNotAllowed = NewHTTPError(http.StatusMethodNotAllowed) ErrRendererNotRegistered = errors.New("renderer not registered") ErrInvalidRedirectCode = errors.New("invalid redirect status code") )
Errors
Functions ¶
Types ¶
type Context ¶
type Context interface { netContext.Context // NetContext returns `http://blog.golang.org/context.Context` interface. NetContext() netContext.Context // SetNetContext sets `http://blog.golang.org/context.Context` interface. SetNetContext(netContext.Context) // Request returns `engine.Request` interface. Request() engine.Request // Request returns `engine.Response` interface. Response() engine.Response // Path returns the registered path for the handler. Path() string // P returns path parameter by index. P(int) string // Param returns path parameter by name. Param(string) string // ParamNames returns path parameter names. ParamNames() []string // QueryParam returns the query param for the provided name. It is an alias // for `engine.URL#QueryParam()`. QueryParam(string) string // FormValue returns the form field value for the provided name. It is an // alias for `engine.Request#FormValue()`. FormValue(string) string // FormFile returns the multipart form file for the provided name. It is an // alias for `engine.Request#FormFile()`. FormFile(string) (*multipart.FileHeader, error) // MultipartForm returns the multipart form. It is an alias for `engine.Request#MultipartForm()`. MultipartForm() (*multipart.Form, error) // Get retrieves data from the context. Get(string) interface{} // Set saves data in the context. Set(string, interface{}) // Bind binds the request body into provided type `i`. The default binder does // it based on Content-Type header. Bind(interface{}) error // Render renders a template with data and sends a text/html response with status // code. Templates can be registered using `Echo.SetRenderer()`. Render(int, string, interface{}) error // HTML sends an HTTP response with status code. HTML(int, string) error // String sends a string response with status code. String(int, string) error // JSON sends a JSON response with status code. JSON(int, interface{}) error // JSONBlob sends a JSON blob response with status code. JSONBlob(int, []byte) error // JSONP sends a JSONP response with status code. It uses `callback` to construct // the JSONP payload. JSONP(int, string, interface{}) error // XML sends an XML response with status code. XML(int, interface{}) error // XMLBlob sends a XML blob response with status code. XMLBlob(int, []byte) error // File sends a response with the content of the file. File(string) error // Attachment sends a response from `io.Reader` as attachment, prompting client // to save the file. Attachment(io.Reader, string) error // NoContent sends a response with no body and a status code. NoContent(int) error // Redirect redirects the request with status code. Redirect(int, string) error // Error invokes the registered HTTP error handler. Generally used by middleware. Error(err error) // Handler implements `Handler` interface. Handle(Context) error // Logger returns the `Logger` instance. Logger() *log.Logger // Echo returns the `Echo` instance. Echo() *Echo // Object returns the `context` instance. Object() *context // Reset resets the context after request completes. It must be called along // with `Echo#GetContext()` and `Echo#PutContext()`. See `Echo#ServeHTTP()` Reset(engine.Request, engine.Response) }
Context represents the context of the current HTTP request. It holds request and response objects, path, path parameters, data and registered handler.
type Echo ¶
type Echo struct {
// contains filtered or unexported fields
}
Echo is the top-level framework instance.
func (*Echo) Any ¶ added in v1.2.0
func (e *Echo) Any(path string, handler Handler, middleware ...Middleware)
Any registers a new route for all HTTP methods and path with matching handler in the router with optional route-level middleware.
func (*Echo) Connect ¶
func (e *Echo) Connect(path string, h Handler, m ...Middleware)
Connect registers a new CONNECT route for a path with matching handler in the router with optional route-level middleware.
func (*Echo) DefaultHTTPErrorHandler ¶ added in v1.0.0
DefaultHTTPErrorHandler invokes the default HTTP error handler.
func (*Echo) Delete ¶
func (e *Echo) Delete(path string, h Handler, m ...Middleware)
Delete registers a new DELETE route for a path with matching handler in the router with optional route-level middleware.
func (*Echo) Get ¶
func (e *Echo) Get(path string, h Handler, m ...Middleware)
Get registers a new GET route for a path with matching handler in the router with optional route-level middleware.
func (*Echo) GetContext ¶
GetContext returns `Context` from the sync.Pool. You must return the context by calling `PutContext()`.
func (*Echo) Group ¶ added in v0.0.4
func (e *Echo) Group(prefix string, m ...Middleware) (g *Group)
Group creates a new router group with prefix and optional group-level middleware.
func (*Echo) Head ¶
func (e *Echo) Head(path string, h Handler, m ...Middleware)
Head registers a new HEAD route for a path with matching handler in the router with optional route-level middleware.
func (*Echo) Match ¶ added in v1.2.0
func (e *Echo) Match(methods []string, path string, handler Handler, middleware ...Middleware)
Match registers a new route for multiple HTTP methods and path with matching handler in the router with optional route-level middleware.
func (*Echo) Options ¶
func (e *Echo) Options(path string, h Handler, m ...Middleware)
Options registers a new OPTIONS route for a path with matching handler in the router with optional route-level middleware.
func (*Echo) Patch ¶
func (e *Echo) Patch(path string, h Handler, m ...Middleware)
Patch registers a new PATCH route for a path with matching handler in the router with optional route-level middleware.
func (*Echo) Post ¶
func (e *Echo) Post(path string, h Handler, m ...Middleware)
Post registers a new POST route for a path with matching handler in the router with optional route-level middleware.
func (*Echo) Pre ¶
func (e *Echo) Pre(middleware ...Middleware)
Pre adds middleware to the chain which is run before router.
func (*Echo) Put ¶
func (e *Echo) Put(path string, h Handler, m ...Middleware)
Put registers a new PUT route for a path with matching handler in the router with optional route-level middleware.
func (*Echo) PutContext ¶
PutContext returns `Context` instance back to the sync.Pool. You must call it after `GetContext()`.
func (*Echo) SetBinder ¶ added in v0.0.14
SetBinder registers a custom binder. It's invoked by `Context#Bind()`.
func (*Echo) SetHTTPErrorHandler ¶ added in v0.0.14
func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler)
SetHTTPErrorHandler registers a custom Echo.HTTPErrorHandler.
func (*Echo) SetLogLevel ¶ added in v1.4.1
SetLogLevel sets the log level for the logger. Default value is `log.ERROR`.
func (*Echo) SetLogOutput ¶ added in v1.4.1
SetLogOutput sets the output destination for the logger. Default value is `os.Std*`
func (*Echo) SetLogPrefix ¶ added in v1.4.1
SetLogPrefix sets the prefix for the logger. Default value is `echo`.
func (*Echo) SetRenderer ¶ added in v0.0.14
SetRenderer registers an HTML template renderer. It's invoked by `Context#Render()`.
func (*Echo) Static ¶
Static serves files from provided `root` directory for `/<prefix>*` HTTP path.
func (*Echo) Trace ¶
func (e *Echo) Trace(path string, h Handler, m ...Middleware)
Trace registers a new TRACE route for a path with matching handler in the router with optional route-level middleware.
func (*Echo) Use ¶
func (e *Echo) Use(middleware ...Middleware)
Use adds middleware to the chain which is run after router.
type Group ¶ added in v0.0.16
type Group struct {
// contains filtered or unexported fields
}
Group is a set of sub-routes for a specified route. It can be used for inner routes that share a common middlware or functionality that should be separate from the parent echo instance while still inheriting from it.
func (*Group) Any ¶ added in v1.4.1
func (g *Group) Any(path string, handler Handler, middleware ...Middleware)
Any implements `Echo#Any()` for sub-routes within the Group.
func (*Group) Connect ¶ added in v0.0.16
func (g *Group) Connect(path string, h Handler, m ...Middleware)
Connect implements `Echo#Connect()` for sub-routes within the Group.
func (*Group) Delete ¶ added in v0.0.16
func (g *Group) Delete(path string, h Handler, m ...Middleware)
Delete implements `Echo#Delete()` for sub-routes within the Group.
func (*Group) Get ¶ added in v0.0.16
func (g *Group) Get(path string, h Handler, m ...Middleware)
Get implements `Echo#Get()` for sub-routes within the Group.
func (*Group) Group ¶ added in v0.0.16
func (g *Group) Group(prefix string, m ...Middleware) *Group
Group creates a new sub-group with prefix and optional sub-group-level middleware.
func (*Group) Head ¶ added in v0.0.16
func (g *Group) Head(path string, h Handler, m ...Middleware)
Head implements `Echo#Head()` for sub-routes within the Group.
func (*Group) Match ¶ added in v1.4.1
func (g *Group) Match(methods []string, path string, handler Handler, middleware ...Middleware)
Match implements `Echo#Match()` for sub-routes within the Group.
func (*Group) Options ¶ added in v0.0.16
func (g *Group) Options(path string, h Handler, m ...Middleware)
Options implements `Echo#Options()` for sub-routes within the Group.
func (*Group) Patch ¶ added in v0.0.16
func (g *Group) Patch(path string, h Handler, m ...Middleware)
Patch implements `Echo#Patch()` for sub-routes within the Group.
func (*Group) Post ¶ added in v0.0.16
func (g *Group) Post(path string, h Handler, m ...Middleware)
Post implements `Echo#Post()` for sub-routes within the Group.
func (*Group) Put ¶ added in v0.0.16
func (g *Group) Put(path string, h Handler, m ...Middleware)
Put implements `Echo#Put()` for sub-routes within the Group.
func (*Group) Trace ¶ added in v0.0.16
func (g *Group) Trace(path string, h Handler, m ...Middleware)
Trace implements `Echo#Trace()` for sub-routes within the Group.
func (*Group) Use ¶ added in v0.0.16
func (g *Group) Use(m ...Middleware)
Use implements `Echo#Use()` for sub-routes within the Group.
type HTTPError ¶ added in v0.0.12
HTTPError represents an error that occured while handling a request.
func NewHTTPError ¶ added in v0.0.14
NewHTTPError creates a new HTTPError instance.
type HTTPErrorHandler ¶ added in v0.0.10
HTTPErrorHandler is a centralized HTTP error handler.
type HandlerFunc ¶
HandlerFunc is an adapter to allow the use of `func(Context)` as an HTTP handler.
func (HandlerFunc) Handle ¶
func (f HandlerFunc) Handle(c Context) error
Handle serves HTTP request.
type Middleware ¶
Middleware defines an interface for middleware via `Handle(Handler) Handler` function.
type MiddlewareFunc ¶
MiddlewareFunc is an adapter to allow the use of `func(Handler) Handler` as middleware.
func WrapMiddleware ¶
func WrapMiddleware(h Handler) MiddlewareFunc
WrapMiddleware wrap `echo.Handler` into `echo.MiddlewareFunc`.
func (MiddlewareFunc) Handle ¶
func (f MiddlewareFunc) Handle(h Handler) Handler
Handle chains middleware.
type Route ¶ added in v1.0.0
Route contains a handler and information for matching against requests.
type Router ¶ added in v0.0.16
type Router struct {
// contains filtered or unexported fields
}
Router is the registry of all registered routes for an `Echo` instance for request matching and URL path parameter parsing.
func (*Router) Add ¶ added in v0.0.16
Add registers a new route for method and path with matching handler.
func (*Router) Find ¶ added in v0.0.16
Find lookup a handler registed for method and path. It also parses URL for path parameters and load them into context.
For performance:
- Get context from `Echo#GetContext()` - Reset it `Context#Reset()` - Return it `Echo#PutContext()`.