package echo
import "github.com/labstack/echo"
Package echo implements a fast and unfancy micro web framework for Go.
Example:
package main import ( "net/http" echo "gopkg.in/labstack/echo.v1" mw "gopkg.in/labstack/echo.v1/middleware" ) func hello(c *echo.Context) error { return c.String(http.StatusOK, "Hello, World!\n") } func main() { e := echo.New() e.Use(mw.Logger()) e.Use(mw.Recover()) e.Get("/", hello) e.Run(":1323") }
Learn more at https://labstack.com/echo
Index ¶
- Constants
- Variables
- type Binder
- type Context
- func NewContext(req *http.Request, res *Response, e *Echo) *Context
- func (c *Context) Bind(i interface{}) error
- func (c *Context) Echo() *Echo
- func (c *Context) Error(err error)
- func (c *Context) File(path, name string, attachment bool) (err error)
- func (c *Context) Form(name string) string
- func (c *Context) Get(key string) interface{}
- func (c *Context) HTML(code int, html string) (err error)
- func (c *Context) JSON(code int, i interface{}) (err error)
- func (c *Context) JSONBlob(code int, b []byte) (err error)
- func (c *Context) JSONIndent(code int, i interface{}, prefix string, indent string) (err error)
- func (c *Context) JSONP(code int, callback string, i interface{}) (err error)
- func (c *Context) NoContent(code int) error
- func (c *Context) P(i int) (value string)
- func (c *Context) Param(name string) (value string)
- func (c *Context) ParamNames() []string
- func (c *Context) Path() string
- func (c *Context) Query(name string) string
- func (c *Context) Redirect(code int, url string) error
- func (c *Context) Render(code int, name string, data interface{}) (err error)
- func (c *Context) Request() *http.Request
- func (c *Context) Response() *Response
- func (c *Context) Set(key string, val interface{})
- func (c *Context) Socket() *websocket.Conn
- func (c *Context) String(code int, s string) (err error)
- func (c *Context) XML(code int, i interface{}) (err error)
- func (c *Context) XMLIndent(code int, i interface{}, prefix string, indent string) (err error)
- type Echo
- func New() (e *Echo)
- func (e *Echo) Any(path string, h Handler)
- func (e *Echo) AutoIndex(on bool)
- func (e *Echo) Connect(path string, h Handler)
- func (e *Echo) Debug() bool
- func (e *Echo) DefaultHTTPErrorHandler(err error, c *Context)
- func (e *Echo) Delete(path string, h Handler)
- func (e *Echo) Favicon(file string)
- func (e *Echo) Get(path string, h Handler)
- func (e *Echo) Group(prefix string, m ...Middleware) *Group
- func (e *Echo) Head(path string, h Handler)
- func (e *Echo) Hook(h http.HandlerFunc)
- func (e *Echo) Index(file string)
- func (e *Echo) Logger() *log.Logger
- func (e *Echo) Match(methods []string, path string, h Handler)
- func (e *Echo) Options(path string, h Handler)
- func (e *Echo) Patch(path string, h Handler)
- func (e *Echo) Post(path string, h Handler)
- func (e *Echo) Put(path string, h Handler)
- func (e *Echo) Router() *Router
- func (e *Echo) Routes() []Route
- func (e *Echo) Run(addr string)
- func (e *Echo) RunServer(s *http.Server)
- func (e *Echo) RunTLS(addr, certfile, keyfile string)
- func (e *Echo) RunTLSServer(s *http.Server, crtFile, keyFile string)
- func (e *Echo) ServeDir(path, dir string)
- func (e *Echo) ServeFile(path, file string)
- func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (e *Echo) Server(addr string) *http.Server
- func (e *Echo) SetBinder(b Binder)
- func (e *Echo) SetDebug(on bool)
- func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler)
- func (e *Echo) SetLogLevel(l log.Lvl)
- func (e *Echo) SetLogOutput(w io.Writer)
- func (e *Echo) SetLogPrefix(prefix string)
- func (e *Echo) SetRenderer(r Renderer)
- func (e *Echo) Static(path, dir string)
- func (e *Echo) Trace(path string, h Handler)
- func (e *Echo) URI(h Handler, params ...interface{}) string
- func (e *Echo) URL(h Handler, params ...interface{}) string
- func (e *Echo) Use(m ...Middleware)
- func (e *Echo) WebSocket(path string, h HandlerFunc)
- type Group
- func (g *Group) Any(path string, h Handler)
- func (g *Group) Connect(path string, h Handler)
- func (g *Group) Delete(path string, h Handler)
- func (g *Group) Get(path string, h Handler)
- func (g *Group) Group(prefix string, m ...Middleware) *Group
- func (g *Group) Head(path string, h Handler)
- func (g *Group) Match(methods []string, path string, h Handler)
- func (g *Group) Options(path string, h Handler)
- func (g *Group) Patch(path string, h Handler)
- func (g *Group) Post(path string, h Handler)
- func (g *Group) Put(path string, h Handler)
- func (g *Group) ServeDir(path, root string)
- func (g *Group) ServeFile(path, file string)
- func (g *Group) Static(path, root string)
- func (g *Group) Trace(path string, h Handler)
- func (g *Group) Use(m ...Middleware)
- func (g *Group) WebSocket(path string, h HandlerFunc)
- type HTTPError
- func NewHTTPError(code int, msg ...string) *HTTPError
- func (e *HTTPError) Code() int
- func (e *HTTPError) Error() string
- func (e *HTTPError) SetCode(code int)
- type HTTPErrorHandler
- type Handler
- type HandlerFunc
- type Middleware
- type MiddlewareFunc
- type Renderer
- type Response
- func NewResponse(w http.ResponseWriter, e *Echo) *Response
- func (r *Response) CloseNotify() <-chan bool
- func (r *Response) Committed() bool
- func (r *Response) Flush()
- func (r *Response) Header() http.Header
- func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (r *Response) SetWriter(w http.ResponseWriter)
- func (r *Response) Size() int64
- func (r *Response) Status() int
- func (r *Response) Write(b []byte) (n int, err error)
- func (r *Response) WriteHeader(code int)
- func (r *Response) Writer() http.ResponseWriter
- type Route
- type Router
- func NewRouter(e *Echo) *Router
- func (r *Router) Add(method, path string, h HandlerFunc, e *Echo)
- func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- type Validator
Constants ¶
const ( // CONNECT HTTP method CONNECT = "CONNECT" // DELETE HTTP method DELETE = "DELETE" // GET HTTP method GET = "GET" // HEAD HTTP method HEAD = "HEAD" // OPTIONS HTTP method OPTIONS = "OPTIONS" // PATCH HTTP method PATCH = "PATCH" // POST HTTP method POST = "POST" // PUT HTTP method PUT = "PUT" // TRACE HTTP method TRACE = "TRACE" 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" CharsetUTF8 = "charset=utf-8" AcceptEncoding = "Accept-Encoding" Authorization = "Authorization" ContentDisposition = "Content-Disposition" ContentEncoding = "Content-Encoding" ContentLength = "Content-Length" ContentType = "Content-Type" Location = "Location" Upgrade = "Upgrade" Vary = "Vary" WWWAuthenticate = "WWW-Authenticate" XForwardedFor = "X-Forwarded-For" XRealIP = "X-Real-IP" WebSocket = "websocket" )
Variables ¶
var ( ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType) ErrRendererNotRegistered = errors.New("renderer not registered") ErrInvalidRedirectCode = errors.New("invalid redirect status code") )
Types ¶
type Binder ¶
Binder is the interface that wraps the Bind method.
type Context ¶
Context represents context for the current request. It holds request and response objects, path parameters, data and registered handler.
func NewContext ¶
NewContext creates a Context object.
func (*Context) Bind ¶
Bind binds the request body into specified type `i`. The default binder does it based on Content-Type header.
func (*Context) Echo ¶
Echo returns the `Echo` instance.
func (*Context) Error ¶
Error invokes the registered HTTP error handler. Generally used by middleware.
func (*Context) File ¶
File sends a response with the content of the file. If `attachment` is set to true, the client is prompted to save the file with provided `name`, name can be empty, in that case name of the file is used.
func (*Context) Form ¶
Form returns form parameter by name.
func (*Context) Get ¶
Get retrieves data from the context.
func (*Context) HTML ¶
HTML sends an HTTP response with status code.
func (*Context) JSON ¶
JSON sends a JSON response with status code.
func (*Context) JSONBlob ¶
JSONBlob sends a JSON blob response with status code.
func (*Context) JSONIndent ¶
JSONIndent sends a JSON response with status code, but it applies prefix and indent to format the output.
func (*Context) JSONP ¶
JSONP sends a JSONP response with status code. It uses `callback` to construct the JSONP payload.
func (*Context) NoContent ¶
NoContent sends a response with no body and a status code.
func (*Context) P ¶
P returns path parameter by index.
func (*Context) Param ¶
Param returns path parameter by name.
func (*Context) ParamNames ¶
ParamNames returns path parameter names.
func (*Context) Path ¶
Path returns the registered path for the handler.
func (*Context) Query ¶
Query returns query parameter by name.
func (*Context) Redirect ¶
Redirect redirects the request using http.Redirect with status code.
func (*Context) Render ¶
Render renders a template with data and sends a text/html response with status code. Templates can be registered using `Echo.SetRenderer()`.
func (*Context) Request ¶
Request returns *http.Request.
func (*Context) Response ¶
Response returns *Response.
func (*Context) Set ¶
Set saves data in the context.
func (*Context) Socket ¶
Socket returns *websocket.Conn.
func (*Context) String ¶
String sends a string response with status code.
func (*Context) XML ¶
XML sends an XML response with status code.
func (*Context) XMLIndent ¶
XMLIndent sends an XML response with status code, but it applies prefix and indent to format the output.
type Echo ¶
type Echo struct {
// contains filtered or unexported fields
}
Echo is the top-level framework instance.
func New ¶
func New() (e *Echo)
New creates an instance of Echo.
func (*Echo) Any ¶
Any adds a route > handler to the router for all HTTP methods.
func (*Echo) AutoIndex ¶
AutoIndex enable/disable automatically creating an index page for the directory.
func (*Echo) Connect ¶
Connect adds a CONNECT route > handler to the router.
func (*Echo) Debug ¶
Debug returns debug mode (enabled or disabled).
func (*Echo) DefaultHTTPErrorHandler ¶
DefaultHTTPErrorHandler invokes the default HTTP error handler.
func (*Echo) Delete ¶
Delete adds a DELETE route > handler to the router.
func (*Echo) Favicon ¶
Favicon serves the default favicon - GET /favicon.ico.
func (*Echo) Get ¶
Get adds a GET route > handler to the router.
func (*Echo) Group ¶
func (e *Echo) Group(prefix string, m ...Middleware) *Group
Group creates a new sub router with prefix. It inherits all properties from the parent. Passing middleware overrides parent middleware.
func (*Echo) Head ¶
Head adds a HEAD route > handler to the router.
func (*Echo) Hook ¶
func (e *Echo) Hook(h http.HandlerFunc)
Hook registers a callback which is invoked from `Echo#ServerHTTP` as the first statement. Hook is useful if you want to modify response/response objects even before it hits the router or any middleware.
func (*Echo) Index ¶
Index serves index file.
func (*Echo) Logger ¶
Logger returns the logger instance.
func (*Echo) Match ¶
Match adds a route > handler to the router for multiple HTTP methods provided.
func (*Echo) Options ¶
Options adds an OPTIONS route > handler to the router.
func (*Echo) Patch ¶
Patch adds a PATCH route > handler to the router.
func (*Echo) Post ¶
Post adds a POST route > handler to the router.
func (*Echo) Put ¶
Put adds a PUT route > handler to the router.
func (*Echo) Router ¶
Router returns router.
func (*Echo) Routes ¶
Routes returns the registered routes.
func (*Echo) Run ¶
Run runs a server.
func (*Echo) RunServer ¶
RunServer runs a custom server.
func (*Echo) RunTLS ¶
RunTLS runs a server with TLS configuration.
func (*Echo) RunTLSServer ¶
RunTLSServer runs a custom server with TLS configuration.
func (*Echo) ServeDir ¶
ServeDir serves files from a directory.
func (*Echo) ServeFile ¶
ServeFile serves a file.
func (*Echo) ServeHTTP ¶
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
func (*Echo) Server ¶
Server returns the internal *http.Server.
func (*Echo) SetBinder ¶
SetBinder registers a custom binder. It's invoked by Context.Bind().
func (*Echo) SetDebug ¶
SetDebug enable/disable debug mode.
func (*Echo) SetHTTPErrorHandler ¶
func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler)
SetHTTPErrorHandler registers a custom Echo.HTTPErrorHandler.
func (*Echo) SetLogLevel ¶
SetLogLevel sets the log level for the logger. Default value FATAL.
func (*Echo) SetLogOutput ¶
SetLogOutput sets the output destination for the logger. Default value is `os.Std*`
func (*Echo) SetLogPrefix ¶
SetLogPrefix sets the prefix for the logger. Default value is `echo`.
func (*Echo) SetRenderer ¶
SetRenderer registers an HTML template renderer. It's invoked by Context.Render().
func (*Echo) Static ¶
Static serves static files from a directory. It's an alias for `Echo.ServeDir`
func (*Echo) Trace ¶
Trace adds a TRACE route > handler to the router.
func (*Echo) URI ¶
URI generates a URI from handler.
func (*Echo) URL ¶
URL is an alias for `URI` function.
func (*Echo) Use ¶
func (e *Echo) Use(m ...Middleware)
Use adds handler to the middleware chain.
func (*Echo) WebSocket ¶
func (e *Echo) WebSocket(path string, h HandlerFunc)
WebSocket adds a WebSocket route > handler to the router.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group is a set of subroutes 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 ¶
Any implements the echo.Any interface for subroutes within the Group.
func (*Group) Connect ¶
Connect implements the echo.Connect interface for subroutes within the Group.
func (*Group) Delete ¶
Delete implements the echo.Delete interface for subroutes within the Group.
func (*Group) Get ¶
Get implements the echo.Get interface for subroutes within the Group.
func (*Group) Group ¶
func (g *Group) Group(prefix string, m ...Middleware) *Group
Group implements the echo.Group interface for subroutes within the Group.
func (*Group) Head ¶
Head implements the echo.Head interface for subroutes within the Group.
func (*Group) Match ¶
Match implements the echo.Match interface for subroutes within the Group.
func (*Group) Options ¶
Options implements the echo.Options interface for subroutes within the Group.
func (*Group) Patch ¶
Patch implements the echo.Patch interface for subroutes within the Group.
func (*Group) Post ¶
Post implements the echo.Post interface for subroutes within the Group.
func (*Group) Put ¶
Put implements the echo.Put interface for subroutes within the Group.
func (*Group) ServeDir ¶
ServeDir implements the echo.ServeDir interface for subroutes within the Group.
func (*Group) ServeFile ¶
ServeFile implements the echo.ServeFile interface for subroutes within the Group.
func (*Group) Static ¶
Static implements the echo.Static interface for subroutes within the Group.
func (*Group) Trace ¶
Trace implements the echo.Trace interface for subroutes within the Group.
func (*Group) Use ¶
func (g *Group) Use(m ...Middleware)
Use implements the echo.Use interface for subroutes within the Group.
func (*Group) WebSocket ¶
func (g *Group) WebSocket(path string, h HandlerFunc)
WebSocket implements the echo.WebSocket interface for subroutes within the Group.
type HTTPError ¶
type HTTPError struct {
// contains filtered or unexported fields
}
HTTPError represents an error that occured while handling a request.
func NewHTTPError ¶
NewHTTPError creates a new HTTPError instance.
func (*HTTPError) Code ¶
Code returns code.
func (*HTTPError) Error ¶
Error returns message.
func (*HTTPError) SetCode ¶
SetCode sets code.
type HTTPErrorHandler ¶
HTTPErrorHandler is a centralized HTTP error handler.
type Handler ¶
type Handler interface{}
Handler ...
type HandlerFunc ¶
HandlerFunc ...
func Use ¶
func Use(handler Handler, middleware ...Middleware) (h HandlerFunc)
Use chains all middleware with handler in the end and returns head of the chain. The head can be used as handler in any route.
type Middleware ¶
type Middleware interface{}
Middleware ...
type MiddlewareFunc ¶
type MiddlewareFunc func(HandlerFunc) HandlerFunc
MiddlewareFunc ...
type Renderer ¶
Renderer is the interface that wraps the Render method.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response wraps an http.ResponseWriter and implements its interface to be used by an HTTP handler to construct an HTTP response. See http.ResponseWriter(https://golang.org/pkg/net/http/#ResponseWriter)
func NewResponse ¶
func NewResponse(w http.ResponseWriter, e *Echo) *Response
NewResponse creates a new instance of Response.
func (*Response) CloseNotify ¶
CloseNotify implements the http.CloseNotifier interface to allow detecting when the underlying connection has gone away. This mechanism can be used to cancel long operations on the server if the client has disconnected before the response is ready. See http.CloseNotifier(https://golang.org/pkg/net/http/#CloseNotifier)
func (*Response) Committed ¶
Committed asserts whether or not the response has been committed to.
func (*Response) Flush ¶
func (r *Response) Flush()
Flush implements the http.Flusher interface to allow an HTTP handler to flush buffered data to the client. See http.Flusher(https://golang.org/pkg/net/http/#Flusher)
func (*Response) Header ¶
Header returns the header map for the writer that will be sent by WriteHeader. Changing the header after a call to WriteHeader (or Write) has no effect unless the modified headers were declared as trailers by setting the "Trailer" header before the call to WriteHeader (see example) To suppress implicit response headers, set their value to nil. Example [ResponseWriter.Trailers](https://golang.org/pkg/net/http/#example_ResponseWriter_trailers)
func (*Response) Hijack ¶
Hijack implements the http.Hijacker interface to allow an HTTP handler to take over the connection. See http.Hijacker(https://golang.org/pkg/net/http/#Hijacker)
func (*Response) SetWriter ¶
func (r *Response) SetWriter(w http.ResponseWriter)
SetWriter sets the http.ResponseWriter instance for this Response.
func (*Response) Size ¶
Size returns the current size, in bytes, of the response.
func (*Response) Status ¶
Status returns the HTTP status code of the response.
func (*Response) Write ¶
Write wraps and implements the http.Response.Write specification. Additionally, Write will increment the size of the current response. See http.Response.Write(https://golang.org/pkg/net/http/#Response.Write)
func (*Response) WriteHeader ¶
WriteHeader sends an HTTP response header with status code. If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly used to send error codes.
func (*Response) Writer ¶
func (r *Response) Writer() http.ResponseWriter
Writer returns the http.ResponseWriter instance for this Response.
type Route ¶
Route contains a handler and information for matching against requests.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is the registry of all registered routes for an Echo instance for request matching and handler dispatching.
Router implements the http.Handler specification and can be registered to serve requests.
func NewRouter ¶
NewRouter returns a new Router instance.
func (*Router) Add ¶
func (r *Router) Add(method, path string, h HandlerFunc, e *Echo)
Add registers a new route with a matcher for the URL path.
func (*Router) Find ¶
func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo)
Find dispatches the request to the handler whos route is matched with the specified request path.
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP implements the Handler interface and can be registered to serve a particular path or subtree in an HTTP server. See Router.Find()
type Validator ¶
type Validator interface { Validate() error }
Validator is the interface that wraps the Validate method.
Source Files ¶
binder.go context.go echo.go group.go response.go router.go
Directories ¶
Path | Synopsis |
---|---|
middleware |
- Version
- v1.4.2
- Published
- Jun 9, 2016
- Platform
- windows/amd64
- Imports
- 20 packages
- Last checked
- 1 week ago –
Tools for package owners.