Documentation ¶
Overview ¶
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 (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 (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
- type HTTPErrorHandler
- type Handler
- type HandlerFunc
- type Middleware
- type MiddlewareFunc
- type Renderer
- type 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
- 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") )
Functions ¶
This section is empty.
Types ¶
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) Error ¶
Error invokes the registered HTTP error handler. Generally used by middleware.
func (*Context) File ¶ added in v1.2.0
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) JSONIndent ¶ added in v1.4.1
JSONIndent sends a JSON response with status code, but it applies prefix and indent to format the output.
func (*Context) JSONP ¶ added in v1.2.0
JSONP sends a JSONP response with status code. It uses `callback` to construct the JSONP payload.
func (*Context) ParamNames ¶ added in v1.4.1
ParamNames returns path parameter names.
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()`.
type Echo ¶
type Echo struct {
// contains filtered or unexported fields
}
Echo is the top-level framework instance.
func (*Echo) AutoIndex ¶ added in v1.4.1
AutoIndex enable/disable automatically creating an index page for the directory.
func (*Echo) DefaultHTTPErrorHandler ¶
DefaultHTTPErrorHandler invokes the default HTTP error handler.
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) Hook ¶ added in v1.4.1
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) Match ¶ added in v1.2.0
Match adds a route > handler to the router for multiple HTTP methods provided.
func (*Echo) RunTLSServer ¶
RunTLSServer runs a custom server with TLS configuration.
func (*Echo) ServeHTTP ¶
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
func (*Echo) SetHTTPErrorHandler ¶
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 FATAL.
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 ¶
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) 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 ¶ added in v1.4.1
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) Group ¶
func (g *Group) Group(prefix string, m ...Middleware) *Group
Group implements the echo.Group interface for subroutes within the Group.
func (*Group) Match ¶ added in v1.4.1
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) 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) 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.
type HTTPErrorHandler ¶
HTTPErrorHandler is a centralized HTTP error handler.
type HandlerFunc ¶
HandlerFunc ...
func Use ¶ added in v1.4.1
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 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 ¶ added in v1.2.0
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) 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 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 (*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.