2016-03-20 00:47:20 +02:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2015-03-27 23:35:15 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
2015-04-22 07:12:41 +02:00
|
|
|
"bytes"
|
2015-04-15 19:10:05 +02:00
|
|
|
"encoding/json"
|
2015-04-03 05:18:34 +02:00
|
|
|
"errors"
|
2015-04-29 03:53:57 +02:00
|
|
|
"fmt"
|
2015-04-07 22:02:23 +02:00
|
|
|
"io"
|
2015-03-27 23:35:15 +02:00
|
|
|
"net/http"
|
2016-03-12 15:14:15 +02:00
|
|
|
"path"
|
2015-04-24 16:44:30 +02:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2015-04-15 19:10:05 +02:00
|
|
|
"strings"
|
2015-03-27 23:35:15 +02:00
|
|
|
"sync"
|
2015-04-19 06:46:00 +02:00
|
|
|
|
2015-07-11 21:20:59 +02:00
|
|
|
"encoding/xml"
|
2015-07-13 06:40:27 +02:00
|
|
|
|
2015-12-22 01:20:49 +02:00
|
|
|
"github.com/labstack/echo/engine"
|
2015-11-21 05:13:22 +02:00
|
|
|
"github.com/labstack/gommon/log"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-03-20 00:47:20 +02:00
|
|
|
// Echo is the top-level framework instance.
|
2015-03-27 23:35:15 +02:00
|
|
|
Echo struct {
|
2016-02-09 18:12:37 +02:00
|
|
|
prefix string
|
2016-04-02 23:19:39 +02:00
|
|
|
middleware []MiddlewareFunc
|
|
|
|
head HandlerFunc
|
|
|
|
pristineHead HandlerFunc
|
2016-02-09 18:12:37 +02:00
|
|
|
maxParam *int
|
|
|
|
notFoundHandler HandlerFunc
|
|
|
|
httpErrorHandler HTTPErrorHandler
|
|
|
|
binder Binder
|
|
|
|
renderer Renderer
|
|
|
|
pool sync.Pool
|
|
|
|
debug bool
|
|
|
|
router *Router
|
2016-03-06 19:52:32 +02:00
|
|
|
logger *log.Logger
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Route contains a handler and information for matching against requests.
|
2015-06-01 09:07:53 +02:00
|
|
|
Route struct {
|
|
|
|
Method string
|
|
|
|
Path string
|
2015-12-22 01:20:49 +02:00
|
|
|
Handler string
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
2015-05-23 05:26:52 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// HTTPError represents an error that occured while handling a request.
|
2015-05-06 06:55:49 +02:00
|
|
|
HTTPError struct {
|
2016-03-11 17:53:54 +02:00
|
|
|
Code int
|
|
|
|
Message string
|
2015-05-06 06:55:49 +02:00
|
|
|
}
|
2015-05-23 05:26:52 +02:00
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
// MiddlewareFunc defines a function to process middleware.
|
|
|
|
MiddlewareFunc func(HandlerFunc) HandlerFunc
|
2016-02-09 03:26:00 +02:00
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
// HandlerFunc defines a function to server HTTP requests.
|
2015-12-22 01:20:49 +02:00
|
|
|
HandlerFunc func(Context) error
|
2015-04-19 01:47:48 +02:00
|
|
|
|
|
|
|
// HTTPErrorHandler is a centralized HTTP error handler.
|
2015-12-04 03:23:53 +02:00
|
|
|
HTTPErrorHandler func(error, Context)
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Binder is the interface that wraps the Bind function.
|
2015-07-30 23:43:22 +02:00
|
|
|
Binder interface {
|
2016-03-06 06:03:11 +02:00
|
|
|
Bind(interface{}, Context) error
|
2015-07-30 23:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
binder struct {
|
|
|
|
}
|
2015-04-15 19:10:05 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Validator is the interface that wraps the Validate function.
|
2015-08-01 04:25:03 +02:00
|
|
|
Validator interface {
|
|
|
|
Validate() error
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Renderer is the interface that wraps the Render function.
|
2015-04-15 19:10:05 +02:00
|
|
|
Renderer interface {
|
2016-03-06 06:03:11 +02:00
|
|
|
Render(io.Writer, string, interface{}, Context) error
|
2015-04-09 23:59:31 +02:00
|
|
|
}
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// HTTP methods
|
2015-03-27 23:35:15 +02:00
|
|
|
const (
|
2015-04-06 06:49:55 +02:00
|
|
|
CONNECT = "CONNECT"
|
2016-03-20 00:47:20 +02:00
|
|
|
DELETE = "DELETE"
|
|
|
|
GET = "GET"
|
|
|
|
HEAD = "HEAD"
|
2015-04-06 06:49:55 +02:00
|
|
|
OPTIONS = "OPTIONS"
|
2016-03-20 00:47:20 +02:00
|
|
|
PATCH = "PATCH"
|
|
|
|
POST = "POST"
|
|
|
|
PUT = "PUT"
|
|
|
|
TRACE = "TRACE"
|
|
|
|
)
|
2015-05-11 07:34:31 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Media types
|
|
|
|
const (
|
2015-07-24 21:28:35 +02:00
|
|
|
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"
|
2016-02-15 18:11:29 +02:00
|
|
|
OctetStream = "application/octet-stream"
|
2016-03-20 00:47:20 +02:00
|
|
|
)
|
2015-05-11 07:34:31 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Charset
|
|
|
|
const (
|
2015-07-22 13:44:29 +02:00
|
|
|
CharsetUTF8 = "charset=utf-8"
|
2016-03-20 00:47:20 +02:00
|
|
|
)
|
2015-07-21 04:24:33 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Headers
|
|
|
|
const (
|
2015-05-15 21:29:14 +02:00
|
|
|
AcceptEncoding = "Accept-Encoding"
|
2015-07-08 02:34:59 +02:00
|
|
|
Authorization = "Authorization"
|
2015-05-11 07:34:31 +02:00
|
|
|
ContentDisposition = "Content-Disposition"
|
2015-05-15 21:29:14 +02:00
|
|
|
ContentEncoding = "Content-Encoding"
|
2015-05-11 07:34:31 +02:00
|
|
|
ContentLength = "Content-Length"
|
|
|
|
ContentType = "Content-Type"
|
2016-03-28 01:29:41 +02:00
|
|
|
IfModifiedSince = "If-Modified-Since"
|
2016-03-12 20:18:40 +02:00
|
|
|
LastModified = "Last-Modified"
|
2015-07-08 02:34:59 +02:00
|
|
|
Location = "Location"
|
2015-05-23 05:26:52 +02:00
|
|
|
Upgrade = "Upgrade"
|
2015-06-24 22:36:47 +02:00
|
|
|
Vary = "Vary"
|
2015-09-15 22:14:30 +02:00
|
|
|
WWWAuthenticate = "WWW-Authenticate"
|
2015-09-18 01:17:54 +02:00
|
|
|
XForwardedFor = "X-Forwarded-For"
|
|
|
|
XRealIP = "X-Real-IP"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
var (
|
2015-08-26 05:36:15 +02:00
|
|
|
methods = [...]string{
|
|
|
|
CONNECT,
|
|
|
|
DELETE,
|
|
|
|
GET,
|
|
|
|
HEAD,
|
|
|
|
OPTIONS,
|
|
|
|
PATCH,
|
|
|
|
POST,
|
|
|
|
PUT,
|
|
|
|
TRACE,
|
|
|
|
}
|
2016-03-20 00:47:20 +02:00
|
|
|
)
|
2015-08-26 05:36:15 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Errors
|
|
|
|
var (
|
2016-02-15 18:11:29 +02:00
|
|
|
ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType)
|
|
|
|
ErrNotFound = NewHTTPError(http.StatusNotFound)
|
2016-03-12 20:18:40 +02:00
|
|
|
ErrUnauthorized = NewHTTPError(http.StatusUnauthorized)
|
|
|
|
ErrMethodNotAllowed = NewHTTPError(http.StatusMethodNotAllowed)
|
2016-02-15 18:11:29 +02:00
|
|
|
ErrRendererNotRegistered = errors.New("renderer not registered")
|
|
|
|
ErrInvalidRedirectCode = errors.New("invalid redirect status code")
|
2016-03-20 00:47:20 +02:00
|
|
|
)
|
2015-07-27 17:43:11 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Error handlers
|
|
|
|
var (
|
2016-04-02 23:19:39 +02:00
|
|
|
notFoundHandler = func(c Context) error {
|
2016-03-12 20:18:40 +02:00
|
|
|
return ErrNotFound
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2015-07-24 21:03:36 +02:00
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
methodNotAllowedHandler = func(c Context) error {
|
2016-03-12 20:18:40 +02:00
|
|
|
return ErrMethodNotAllowed
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2015-04-01 17:05:54 +02:00
|
|
|
)
|
|
|
|
|
2015-07-08 02:34:59 +02:00
|
|
|
// New creates an instance of Echo.
|
2015-03-30 08:35:08 +02:00
|
|
|
func New() (e *Echo) {
|
2015-06-04 01:19:03 +02:00
|
|
|
e = &Echo{maxParam: new(int)}
|
2015-03-30 08:35:08 +02:00
|
|
|
e.pool.New = func() interface{} {
|
2015-12-22 01:20:49 +02:00
|
|
|
return NewContext(nil, nil, e)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
2015-06-04 01:19:03 +02:00
|
|
|
e.router = NewRouter(e)
|
2016-04-02 23:19:39 +02:00
|
|
|
e.middleware = []MiddlewareFunc{e.router.Process}
|
|
|
|
e.head = func(c Context) error {
|
2016-03-16 23:26:34 +02:00
|
|
|
return c.Handle(c)
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2016-03-16 23:26:34 +02:00
|
|
|
e.pristineHead = e.head
|
2016-03-17 22:24:52 +02:00
|
|
|
e.chainMiddleware()
|
2015-04-19 06:46:00 +02:00
|
|
|
|
|
|
|
// Defaults
|
2016-02-09 08:17:20 +02:00
|
|
|
e.SetHTTPErrorHandler(e.DefaultHTTPErrorHandler)
|
2015-07-30 23:43:22 +02:00
|
|
|
e.SetBinder(&binder{})
|
2015-12-01 21:22:45 +02:00
|
|
|
e.logger = log.New("echo")
|
2016-03-21 17:43:28 +02:00
|
|
|
e.logger.SetLevel(log.ERROR)
|
2015-11-21 05:13:22 +02:00
|
|
|
|
2015-03-27 23:35:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-23 05:26:52 +02:00
|
|
|
// Router returns router.
|
|
|
|
func (e *Echo) Router() *Router {
|
|
|
|
return e.router
|
|
|
|
}
|
|
|
|
|
2016-03-06 19:52:32 +02:00
|
|
|
// SetLogPrefix sets the prefix for the logger. Default value is `echo`.
|
|
|
|
func (e *Echo) SetLogPrefix(prefix string) {
|
|
|
|
e.logger.SetPrefix(prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogOutput sets the output destination for the logger. Default value is `os.Std*`
|
|
|
|
func (e *Echo) SetLogOutput(w io.Writer) {
|
|
|
|
e.logger.SetOutput(w)
|
|
|
|
}
|
|
|
|
|
2016-03-21 17:43:28 +02:00
|
|
|
// SetLogLevel sets the log level for the logger. Default value is `log.ERROR`.
|
2016-03-19 05:02:02 +02:00
|
|
|
func (e *Echo) SetLogLevel(l uint8) {
|
2016-03-06 19:52:32 +02:00
|
|
|
e.logger.SetLevel(l)
|
2015-11-21 05:13:22 +02:00
|
|
|
}
|
|
|
|
|
2015-12-01 21:22:45 +02:00
|
|
|
// Logger returns the logger instance.
|
2016-03-06 19:52:32 +02:00
|
|
|
func (e *Echo) Logger() *log.Logger {
|
2015-12-01 21:22:45 +02:00
|
|
|
return e.logger
|
2015-07-13 06:40:27 +02:00
|
|
|
}
|
|
|
|
|
2015-05-29 01:50:49 +02:00
|
|
|
// DefaultHTTPErrorHandler invokes the default HTTP error handler.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {
|
2016-02-09 08:17:20 +02:00
|
|
|
code := http.StatusInternalServerError
|
|
|
|
msg := http.StatusText(code)
|
|
|
|
if he, ok := err.(*HTTPError); ok {
|
2016-03-11 17:53:54 +02:00
|
|
|
code = he.Code
|
|
|
|
msg = he.Message
|
2016-02-09 08:17:20 +02:00
|
|
|
}
|
|
|
|
if e.debug {
|
|
|
|
msg = err.Error()
|
|
|
|
}
|
|
|
|
if !c.Response().Committed() {
|
|
|
|
c.String(code, msg)
|
|
|
|
}
|
2016-03-21 17:43:28 +02:00
|
|
|
e.logger.Debug(err)
|
2015-05-29 01:50:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetHTTPErrorHandler registers a custom Echo.HTTPErrorHandler.
|
2015-05-20 03:54:31 +02:00
|
|
|
func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler) {
|
2015-04-19 01:47:48 +02:00
|
|
|
e.httpErrorHandler = h
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// SetBinder registers a custom binder. It's invoked by `Context#Bind()`.
|
2015-07-30 23:43:22 +02:00
|
|
|
func (e *Echo) SetBinder(b Binder) {
|
2015-04-15 19:10:05 +02:00
|
|
|
e.binder = b
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// SetRenderer registers an HTML template renderer. It's invoked by `Context#Render()`.
|
2015-05-20 03:54:31 +02:00
|
|
|
func (e *Echo) SetRenderer(r Renderer) {
|
2015-04-09 23:59:31 +02:00
|
|
|
e.renderer = r
|
2015-04-08 02:34:00 +02:00
|
|
|
}
|
|
|
|
|
2015-12-04 00:14:14 +02:00
|
|
|
// SetDebug enable/disable debug mode.
|
2015-09-13 19:03:20 +02:00
|
|
|
func (e *Echo) SetDebug(on bool) {
|
|
|
|
e.debug = on
|
2016-03-06 19:52:32 +02:00
|
|
|
e.SetLogLevel(log.DEBUG)
|
2015-09-13 19:03:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debug returns debug mode (enabled or disabled).
|
|
|
|
func (e *Echo) Debug() bool {
|
|
|
|
return e.debug
|
2015-05-18 07:54:29 +02:00
|
|
|
}
|
|
|
|
|
2016-03-17 01:15:40 +02:00
|
|
|
// Pre adds middleware to the chain which is run before router.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Pre(middleware ...MiddlewareFunc) {
|
2016-03-17 22:24:52 +02:00
|
|
|
e.middleware = append(middleware, e.middleware...)
|
|
|
|
e.chainMiddleware()
|
2016-03-16 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
2016-03-17 01:15:40 +02:00
|
|
|
// Use adds middleware to the chain which is run after router.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Use(middleware ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
e.middleware = append(e.middleware, middleware...)
|
2016-03-17 22:24:52 +02:00
|
|
|
e.chainMiddleware()
|
|
|
|
}
|
2016-02-16 03:12:15 +02:00
|
|
|
|
2016-03-17 22:24:52 +02:00
|
|
|
func (e *Echo) chainMiddleware() {
|
|
|
|
e.head = e.pristineHead
|
2016-03-16 23:26:34 +02:00
|
|
|
for i := len(e.middleware) - 1; i >= 0; i-- {
|
2016-04-02 23:19:39 +02:00
|
|
|
e.head = e.middleware[i](e.head)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Connect registers a new CONNECT route for a path with matching handler in the
|
|
|
|
// router with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Connect(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
e.add(CONNECT, path, h, m...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Delete registers a new DELETE route for a path with matching handler in the router
|
|
|
|
// with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Delete(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
e.add(DELETE, path, h, m...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Get registers a new GET route for a path with matching handler in the router
|
|
|
|
// with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Get(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
e.add(GET, path, h, m...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Head registers a new HEAD route for a path with matching handler in the
|
|
|
|
// router with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Head(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
e.add(HEAD, path, h, m...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Options registers a new OPTIONS route for a path with matching handler in the
|
|
|
|
// router with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Options(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
e.add(OPTIONS, path, h, m...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Patch registers a new PATCH route for a path with matching handler in the
|
|
|
|
// router with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Patch(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
e.add(PATCH, path, h, m...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Post registers a new POST route for a path with matching handler in the
|
|
|
|
// router with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Post(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
e.add(POST, path, h, m...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Put registers a new PUT route for a path with matching handler in the
|
|
|
|
// router with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Put(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
e.add(PUT, path, h, m...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Trace registers a new TRACE route for a path with matching handler in the
|
|
|
|
// router with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Trace(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
e.add(TRACE, path, h, m...)
|
2015-04-02 14:02:52 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Any registers a new route for all HTTP methods and path with matching handler
|
|
|
|
// in the router with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
|
2015-08-26 05:36:15 +02:00
|
|
|
for _, m := range methods {
|
2016-02-15 18:11:29 +02:00
|
|
|
e.add(m, path, handler, middleware...)
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Match registers a new route for multiple HTTP methods and path with matching
|
|
|
|
// handler in the router with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Match(methods []string, path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
|
2015-08-26 05:36:15 +02:00
|
|
|
for _, m := range methods {
|
2016-02-15 18:11:29 +02:00
|
|
|
e.add(m, path, handler, middleware...)
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 15:14:15 +02:00
|
|
|
// Static serves files from provided `root` directory for `/<prefix>*` HTTP path.
|
|
|
|
func (e *Echo) Static(prefix, root string) {
|
2016-04-02 23:19:39 +02:00
|
|
|
e.Get(prefix+"*", func(c Context) error {
|
2016-03-12 15:14:15 +02:00
|
|
|
return c.File(path.Join(root, c.P(0))) // Param `_`
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2016-03-12 15:14:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// File serves provided file for `/<path>` HTTP path.
|
|
|
|
func (e *Echo) File(path, file string) {
|
2016-04-02 23:19:39 +02:00
|
|
|
e.Get(path, func(c Context) error {
|
2016-03-12 15:14:15 +02:00
|
|
|
return c.File(file)
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2016-03-12 15:14:15 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
|
2016-02-15 18:11:29 +02:00
|
|
|
name := handlerName(handler)
|
2016-04-02 23:19:39 +02:00
|
|
|
e.router.Add(method, path, func(c Context) error {
|
2016-03-15 17:33:57 +02:00
|
|
|
h := handler
|
2016-03-17 05:15:38 +02:00
|
|
|
// Chain middleware
|
|
|
|
for i := len(middleware) - 1; i >= 0; i-- {
|
2016-04-02 23:19:39 +02:00
|
|
|
h = middleware[i](h)
|
2016-02-15 18:11:29 +02:00
|
|
|
}
|
2016-04-02 23:19:39 +02:00
|
|
|
return h(c)
|
|
|
|
}, e)
|
2015-06-01 09:07:53 +02:00
|
|
|
r := Route{
|
|
|
|
Method: method,
|
|
|
|
Path: path,
|
2016-02-15 18:11:29 +02:00
|
|
|
Handler: name,
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
|
|
|
e.router.routes = append(e.router.routes, r)
|
2015-04-22 07:12:41 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Group creates a new router group with prefix and optional group-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Group(prefix string, m ...MiddlewareFunc) (g *Group) {
|
2016-02-15 18:11:29 +02:00
|
|
|
g = &Group{prefix: prefix, echo: e}
|
2016-02-16 03:12:15 +02:00
|
|
|
g.Use(m...)
|
2016-03-31 17:41:02 +02:00
|
|
|
// Dummy handler so group can be used with static middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
g.Get("", func(c Context) error {
|
2016-03-31 17:41:02 +02:00
|
|
|
return c.NoContent(http.StatusNotFound)
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-11-24 06:33:13 +02:00
|
|
|
return
|
2015-05-27 23:07:52 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
// URI generates a URI from handler.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) URI(handler HandlerFunc, params ...interface{}) string {
|
2015-05-20 03:54:31 +02:00
|
|
|
uri := new(bytes.Buffer)
|
2016-02-15 18:11:29 +02:00
|
|
|
ln := len(params)
|
2015-05-20 03:54:31 +02:00
|
|
|
n := 0
|
2016-02-15 18:11:29 +02:00
|
|
|
name := handlerName(handler)
|
2015-06-01 09:07:53 +02:00
|
|
|
for _, r := range e.router.routes {
|
2016-02-15 18:11:29 +02:00
|
|
|
if r.Handler == name {
|
2015-06-01 09:07:53 +02:00
|
|
|
for i, l := 0, len(r.Path); i < l; i++ {
|
2016-02-15 18:11:29 +02:00
|
|
|
if r.Path[i] == ':' && n < ln {
|
2015-06-01 09:07:53 +02:00
|
|
|
for ; i < l && r.Path[i] != '/'; i++ {
|
|
|
|
}
|
|
|
|
uri.WriteString(fmt.Sprintf("%v", params[n]))
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if i < l {
|
|
|
|
uri.WriteByte(r.Path[i])
|
2015-05-20 03:54:31 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-01 09:07:53 +02:00
|
|
|
break
|
2015-05-20 03:54:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return uri.String()
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2015-07-02 20:14:09 +02:00
|
|
|
// URL is an alias for `URI` function.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) URL(h HandlerFunc, params ...interface{}) string {
|
2016-02-16 03:12:15 +02:00
|
|
|
return e.URI(h, params...)
|
2015-05-14 00:20:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-01 09:07:53 +02:00
|
|
|
// Routes returns the registered routes.
|
|
|
|
func (e *Echo) Routes() []Route {
|
|
|
|
return e.router.routes
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-18 06:49:06 +02:00
|
|
|
// GetContext returns `Context` from the sync.Pool. You must return the context by
|
|
|
|
// calling `PutContext()`.
|
|
|
|
func (e *Echo) GetContext() Context {
|
|
|
|
return e.pool.Get().(Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutContext returns `Context` instance back to the sync.Pool. You must call it after
|
|
|
|
// `GetContext()`.
|
|
|
|
func (e *Echo) PutContext(c Context) {
|
|
|
|
e.pool.Put(c)
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:27:14 +02:00
|
|
|
func (e *Echo) ServeHTTP(rq engine.Request, rs engine.Response) {
|
2016-01-29 09:46:11 +02:00
|
|
|
c := e.pool.Get().(*context)
|
2016-03-22 02:27:14 +02:00
|
|
|
c.Reset(rq, rs)
|
2016-01-29 09:46:11 +02:00
|
|
|
|
|
|
|
// Execute chain
|
2016-04-02 23:19:39 +02:00
|
|
|
if err := e.head(c); err != nil {
|
2016-01-29 09:46:11 +02:00
|
|
|
e.httpErrorHandler(err, c)
|
2015-11-15 23:32:21 +02:00
|
|
|
}
|
2016-01-29 09:46:11 +02:00
|
|
|
|
|
|
|
e.pool.Put(c)
|
2015-11-15 23:32:21 +02:00
|
|
|
}
|
2015-06-27 23:34:22 +02:00
|
|
|
|
2016-03-18 06:49:06 +02:00
|
|
|
// Run starts the HTTP server.
|
2016-03-21 17:43:28 +02:00
|
|
|
func (e *Echo) Run(s engine.Server) {
|
2016-03-18 06:49:06 +02:00
|
|
|
s.SetHandler(e)
|
|
|
|
s.SetLogger(e.logger)
|
2016-03-21 17:43:28 +02:00
|
|
|
e.logger.Error(s.Start())
|
2015-04-03 14:24:47 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// NewHTTPError creates a new HTTPError instance.
|
2015-05-23 05:26:52 +02:00
|
|
|
func NewHTTPError(code int, msg ...string) *HTTPError {
|
2016-03-11 17:53:54 +02:00
|
|
|
he := &HTTPError{Code: code, Message: http.StatusText(code)}
|
2015-05-29 01:50:49 +02:00
|
|
|
if len(msg) > 0 {
|
|
|
|
m := msg[0]
|
2016-03-11 17:53:54 +02:00
|
|
|
he.Message = m
|
2015-05-23 05:26:52 +02:00
|
|
|
}
|
|
|
|
return he
|
|
|
|
}
|
|
|
|
|
2016-03-11 17:53:54 +02:00
|
|
|
// Error makes it compatible with `error` interface.
|
2015-05-23 05:26:52 +02:00
|
|
|
func (e *HTTPError) Error() string {
|
2016-03-11 17:53:54 +02:00
|
|
|
return e.Message
|
2015-05-23 05:26:52 +02:00
|
|
|
}
|
|
|
|
|
2016-03-06 06:03:11 +02:00
|
|
|
func (binder) Bind(i interface{}, c Context) (err error) {
|
2016-03-22 02:27:14 +02:00
|
|
|
rq := c.Request()
|
|
|
|
ct := rq.Header().Get(ContentType)
|
2016-02-15 18:11:29 +02:00
|
|
|
err = ErrUnsupportedMediaType
|
2015-07-30 23:43:22 +02:00
|
|
|
if strings.HasPrefix(ct, ApplicationJSON) {
|
2016-03-22 02:27:14 +02:00
|
|
|
if err = json.NewDecoder(rq.Body()).Decode(i); err != nil {
|
2016-01-29 12:22:18 +02:00
|
|
|
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2015-07-30 23:43:22 +02:00
|
|
|
} else if strings.HasPrefix(ct, ApplicationXML) {
|
2016-03-22 02:27:14 +02:00
|
|
|
if err = xml.NewDecoder(rq.Body()).Decode(i); err != nil {
|
2016-01-29 12:22:18 +02:00
|
|
|
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2015-07-30 23:43:22 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2016-02-09 08:17:20 +02:00
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
// WrapMiddleware wrap `echo.HandlerFunc` into `echo.MiddlewareFunc`.
|
|
|
|
func WrapMiddleware(h HandlerFunc) MiddlewareFunc {
|
|
|
|
return func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
if err := h(c); err != nil {
|
2016-03-13 11:18:43 +02:00
|
|
|
return err
|
2016-03-07 17:55:26 +02:00
|
|
|
}
|
2016-04-02 23:19:39 +02:00
|
|
|
return next(c)
|
|
|
|
}
|
2016-03-07 17:55:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
func handlerName(h HandlerFunc) string {
|
2016-02-17 21:58:03 +02:00
|
|
|
t := reflect.ValueOf(h).Type()
|
|
|
|
if t.Kind() == reflect.Func {
|
|
|
|
return runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
|
|
|
|
}
|
|
|
|
return t.String()
|
2016-02-09 08:17:20 +02:00
|
|
|
}
|