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"
|
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"
|
2016-02-10 03:16:46 +02:00
|
|
|
"github.com/labstack/echo/logger"
|
2015-11-21 05:13:22 +02:00
|
|
|
"github.com/labstack/gommon/log"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Echo struct {
|
2016-02-09 18:12:37 +02:00
|
|
|
prefix string
|
2016-02-15 18:11:29 +02:00
|
|
|
middleware []Middleware
|
2016-02-09 18:12:37 +02:00
|
|
|
http2 bool
|
|
|
|
maxParam *int
|
|
|
|
notFoundHandler HandlerFunc
|
|
|
|
httpErrorHandler HTTPErrorHandler
|
|
|
|
binder Binder
|
|
|
|
renderer Renderer
|
|
|
|
pool sync.Pool
|
|
|
|
debug bool
|
|
|
|
router *Router
|
2016-02-10 03:16:46 +02:00
|
|
|
logger logger.Logger
|
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
|
|
|
|
2015-05-06 06:55:49 +02:00
|
|
|
HTTPError struct {
|
2015-05-22 13:40:01 +02:00
|
|
|
code int
|
|
|
|
message string
|
2015-05-06 06:55:49 +02:00
|
|
|
}
|
2015-05-23 05:26:52 +02:00
|
|
|
|
2016-02-09 03:26:00 +02:00
|
|
|
Middleware interface {
|
2016-02-15 18:11:29 +02:00
|
|
|
Handle(Handler) Handler
|
|
|
|
Priority() int
|
2016-02-09 03:26:00 +02:00
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
MiddlewareFunc func(Handler) Handler
|
2016-02-09 03:26:00 +02:00
|
|
|
|
2016-02-09 08:17:20 +02:00
|
|
|
Handler interface {
|
|
|
|
Handle(Context) error
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-07-30 23:43:22 +02:00
|
|
|
// Binder is the interface that wraps the Bind method.
|
|
|
|
Binder interface {
|
2015-12-22 01:20:49 +02:00
|
|
|
Bind(engine.Request, interface{}) error
|
2015-07-30 23:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
binder struct {
|
|
|
|
}
|
2015-04-15 19:10:05 +02:00
|
|
|
|
2015-08-01 04:25:03 +02:00
|
|
|
// Validator is the interface that wraps the Validate method.
|
|
|
|
Validator interface {
|
|
|
|
Validate() error
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:10:05 +02:00
|
|
|
// Renderer is the interface that wraps the Render method.
|
|
|
|
Renderer interface {
|
2015-05-20 23:38:51 +02:00
|
|
|
Render(w io.Writer, name string, data interface{}) error
|
2015-04-09 23:59:31 +02:00
|
|
|
}
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-04-22 07:28:17 +02:00
|
|
|
// CONNECT HTTP method
|
2015-04-06 06:49:55 +02:00
|
|
|
CONNECT = "CONNECT"
|
2015-04-22 07:28:17 +02:00
|
|
|
// DELETE HTTP method
|
|
|
|
DELETE = "DELETE"
|
|
|
|
// GET HTTP method
|
|
|
|
GET = "GET"
|
|
|
|
// HEAD HTTP method
|
|
|
|
HEAD = "HEAD"
|
|
|
|
// OPTIONS HTTP method
|
2015-04-06 06:49:55 +02:00
|
|
|
OPTIONS = "OPTIONS"
|
2015-04-22 07:28:17 +02:00
|
|
|
// PATCH HTTP method
|
|
|
|
PATCH = "PATCH"
|
|
|
|
// POST HTTP method
|
|
|
|
POST = "POST"
|
|
|
|
// PUT HTTP method
|
|
|
|
PUT = "PUT"
|
|
|
|
// TRACE HTTP method
|
|
|
|
TRACE = "TRACE"
|
2015-04-01 17:05:54 +02:00
|
|
|
|
2015-05-11 07:34:31 +02:00
|
|
|
//-------------
|
|
|
|
// Media types
|
|
|
|
//-------------
|
|
|
|
|
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"
|
2015-05-11 07:34:31 +02:00
|
|
|
|
2015-07-24 21:03:36 +02:00
|
|
|
//---------
|
2015-07-21 04:24:33 +02:00
|
|
|
// Charset
|
2015-07-24 21:03:36 +02:00
|
|
|
//---------
|
|
|
|
|
2015-07-22 13:44:29 +02:00
|
|
|
CharsetUTF8 = "charset=utf-8"
|
2015-07-21 04:24:33 +02:00
|
|
|
|
2015-05-11 07:34:31 +02:00
|
|
|
//---------
|
|
|
|
// Headers
|
|
|
|
//---------
|
|
|
|
|
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"
|
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-05-23 05:26:52 +02:00
|
|
|
//-----------
|
|
|
|
// Protocols
|
|
|
|
//-----------
|
|
|
|
|
|
|
|
WebSocket = "websocket"
|
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,
|
|
|
|
}
|
|
|
|
|
2015-04-22 07:28:17 +02:00
|
|
|
//--------
|
2015-04-03 05:18:34 +02:00
|
|
|
// Errors
|
2015-04-22 07:28:17 +02:00
|
|
|
//--------
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType)
|
|
|
|
ErrNotFound = NewHTTPError(http.StatusNotFound)
|
|
|
|
ErrRendererNotRegistered = errors.New("renderer not registered")
|
|
|
|
ErrInvalidRedirectCode = errors.New("invalid redirect status code")
|
2015-07-27 17:43:11 +02:00
|
|
|
|
2015-07-24 21:03:36 +02:00
|
|
|
//----------------
|
|
|
|
// Error handlers
|
|
|
|
//----------------
|
|
|
|
|
2016-02-09 08:17:20 +02:00
|
|
|
notFoundHandler = HandlerFunc(func(c Context) error {
|
2015-07-24 21:03:36 +02:00
|
|
|
return NewHTTPError(http.StatusNotFound)
|
2016-02-09 08:17:20 +02:00
|
|
|
})
|
2015-07-24 21:03:36 +02:00
|
|
|
|
2016-02-09 08:17:20 +02:00
|
|
|
methodNotAllowedHandler = HandlerFunc(func(c Context) error {
|
2015-10-06 15:48:33 +02:00
|
|
|
return NewHTTPError(http.StatusMethodNotAllowed)
|
2016-02-09 08:17:20 +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
|
|
|
// NOTE: v2
|
|
|
|
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-02-15 18:11:29 +02:00
|
|
|
e.middleware = []Middleware{e.router}
|
2015-04-19 06:46:00 +02:00
|
|
|
|
|
|
|
//----------
|
|
|
|
// Defaults
|
|
|
|
//----------
|
2015-04-26 07:32:20 +02:00
|
|
|
|
2015-11-24 08:15:59 +02:00
|
|
|
e.HTTP2(true)
|
2016-02-09 08:17:20 +02:00
|
|
|
e.SetHTTPErrorHandler(e.DefaultHTTPErrorHandler)
|
2015-07-30 23:43:22 +02:00
|
|
|
e.SetBinder(&binder{})
|
2015-11-21 05:13:22 +02:00
|
|
|
|
|
|
|
// Logger
|
2015-12-01 21:22:45 +02:00
|
|
|
e.logger = log.New("echo")
|
2015-11-21 05:13:22 +02:00
|
|
|
|
2015-03-27 23:35:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
func (f MiddlewareFunc) Handle(h Handler) Handler {
|
2016-02-09 03:26:00 +02:00
|
|
|
return f(h)
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
func (f MiddlewareFunc) Priority() int {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-02-09 08:17:20 +02:00
|
|
|
func (f HandlerFunc) Handle(c Context) error {
|
|
|
|
return f(c)
|
|
|
|
}
|
|
|
|
|
2015-05-23 05:26:52 +02:00
|
|
|
// Router returns router.
|
|
|
|
func (e *Echo) Router() *Router {
|
|
|
|
return e.router
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:29:49 +02:00
|
|
|
// SetLogger sets the logger instance.
|
2016-02-10 03:16:46 +02:00
|
|
|
func (e *Echo) SetLogger(l logger.Logger) {
|
2016-02-09 23:37:38 +02:00
|
|
|
e.logger = l
|
2015-11-21 05:13:22 +02:00
|
|
|
}
|
|
|
|
|
2015-12-01 21:22:45 +02:00
|
|
|
// Logger returns the logger instance.
|
2016-02-10 03:16:46 +02:00
|
|
|
func (e *Echo) Logger() logger.Logger {
|
2015-12-01 21:22:45 +02:00
|
|
|
return e.logger
|
2015-07-13 06:40:27 +02:00
|
|
|
}
|
|
|
|
|
2015-12-04 00:14:14 +02:00
|
|
|
// HTTP2 enable/disable HTTP2 support.
|
2015-11-24 08:15:59 +02:00
|
|
|
func (e *Echo) HTTP2(on bool) {
|
|
|
|
e.http2 = on
|
2015-05-22 08:11: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 {
|
|
|
|
code = he.code
|
|
|
|
msg = he.message
|
|
|
|
}
|
|
|
|
if e.debug {
|
|
|
|
msg = err.Error()
|
|
|
|
}
|
|
|
|
if !c.Response().Committed() {
|
|
|
|
c.String(code, msg)
|
|
|
|
}
|
|
|
|
e.logger.Error(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
|
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +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
|
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
// SetRenderer registers an HTML template renderer. It's invoked by Context.Render().
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug returns debug mode (enabled or disabled).
|
|
|
|
func (e *Echo) Debug() bool {
|
|
|
|
return e.debug
|
2015-05-18 07:54:29 +02:00
|
|
|
}
|
|
|
|
|
2015-03-27 23:35:15 +02:00
|
|
|
// Use adds handler to the middleware chain.
|
2016-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) Use(middleware ...interface{}) {
|
|
|
|
for _, m := range middleware {
|
|
|
|
e.middleware = append(e.middleware, wrapMiddleware(m))
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect adds a CONNECT route > handler to the router.
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) Connect(path string, handler interface{}, middleware ...interface{}) {
|
|
|
|
e.add(CONNECT, path, handler, middleware...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete adds a DELETE route > handler to the router.
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) Delete(path string, handler interface{}, middleware ...interface{}) {
|
|
|
|
e.add(DELETE, path, handler, middleware...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get adds a GET route > handler to the router.
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) Get(path string, handler interface{}, middleware ...interface{}) {
|
|
|
|
e.add(GET, path, handler, middleware...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Head adds a HEAD route > handler to the router.
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) Head(path string, handler interface{}, middleware ...interface{}) {
|
|
|
|
e.add(HEAD, path, handler, middleware...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Options adds an OPTIONS route > handler to the router.
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) Options(path string, handler interface{}, middleware ...interface{}) {
|
|
|
|
e.add(OPTIONS, path, handler, middleware...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Patch adds a PATCH route > handler to the router.
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) Patch(path string, handler interface{}, middleware ...interface{}) {
|
|
|
|
e.add(PATCH, path, handler, middleware...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post adds a POST route > handler to the router.
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) Post(path string, handler interface{}, middleware ...interface{}) {
|
|
|
|
e.add(POST, path, handler, middleware...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put adds a PUT route > handler to the router.
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) Put(path string, handler interface{}, middleware ...interface{}) {
|
|
|
|
e.add(PUT, path, handler, middleware...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trace adds a TRACE route > handler to the router.
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) Trace(path string, handler interface{}, middleware ...interface{}) {
|
|
|
|
e.add(TRACE, path, handler, middleware...)
|
2015-04-02 14:02:52 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 05:36:15 +02:00
|
|
|
// Any adds a route > handler to the router for all HTTP methods.
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) Any(path string, handler interface{}, middleware ...interface{}) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match adds a route > handler to the router for multiple HTTP methods provided.
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) Match(methods []string, path string, handler interface{}, middleware ...interface{}) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 01:20:49 +02:00
|
|
|
// NOTE: v2
|
2016-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) add(method, path string, handler interface{}, middleware ...interface{}) {
|
|
|
|
h := wrapHandler(handler)
|
|
|
|
name := handlerName(handler)
|
|
|
|
e.router.Add(method, path, HandlerFunc(func(c Context) error {
|
|
|
|
for _, m := range middleware {
|
|
|
|
h = wrapMiddleware(m).Handle(h)
|
|
|
|
}
|
|
|
|
return h.Handle(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-02-15 18:11:29 +02:00
|
|
|
// Group creates a new sub-router with prefix.
|
|
|
|
func (e *Echo) Group(prefix string, middleware ...interface{}) (g *Group) {
|
|
|
|
g = &Group{prefix: prefix, echo: e}
|
|
|
|
g.Use(middleware...)
|
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-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) URI(handler interface{}, 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-02-15 18:11:29 +02:00
|
|
|
func (e *Echo) URL(handler interface{}, params ...interface{}) string {
|
|
|
|
return e.URI(handler, 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-02-09 23:37:38 +02:00
|
|
|
func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
|
2016-01-29 09:46:11 +02:00
|
|
|
c := e.pool.Get().(*context)
|
2016-02-15 18:11:29 +02:00
|
|
|
c.reset(req, res)
|
|
|
|
h := Handler(c)
|
2016-01-29 09:46:11 +02:00
|
|
|
|
|
|
|
// Chain middleware with handler in the end
|
|
|
|
for i := len(e.middleware) - 1; i >= 0; i-- {
|
2016-02-15 18:11:29 +02:00
|
|
|
h = e.middleware[i].Handle(h)
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute chain
|
2016-02-15 18:11:29 +02:00
|
|
|
if err := h.Handle(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-02-09 23:37:38 +02:00
|
|
|
// Run starts the HTTP engine.
|
2016-02-10 03:16:46 +02:00
|
|
|
func (e *Echo) Run(eng engine.Engine) {
|
|
|
|
eng.SetHandler(e.ServeHTTP)
|
|
|
|
eng.SetLogger(e.logger)
|
|
|
|
eng.Start()
|
2015-04-03 14:24:47 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 05:26:52 +02:00
|
|
|
func NewHTTPError(code int, msg ...string) *HTTPError {
|
|
|
|
he := &HTTPError{code: code, message: http.StatusText(code)}
|
2015-05-29 01:50:49 +02:00
|
|
|
if len(msg) > 0 {
|
|
|
|
m := msg[0]
|
2015-05-23 05:26:52 +02:00
|
|
|
he.message = m
|
|
|
|
}
|
|
|
|
return he
|
|
|
|
}
|
|
|
|
|
2015-06-10 05:06:51 +02:00
|
|
|
// SetCode sets code.
|
|
|
|
func (e *HTTPError) SetCode(code int) {
|
|
|
|
e.code = code
|
|
|
|
}
|
|
|
|
|
2015-05-23 05:26:52 +02:00
|
|
|
// Code returns code.
|
|
|
|
func (e *HTTPError) Code() int {
|
|
|
|
return e.code
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns message.
|
|
|
|
func (e *HTTPError) Error() string {
|
|
|
|
return e.message
|
|
|
|
}
|
|
|
|
|
2015-12-22 01:20:49 +02:00
|
|
|
func (binder) Bind(r engine.Request, i interface{}) (err error) {
|
|
|
|
ct := r.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-01-29 12:22:18 +02:00
|
|
|
if err = json.NewDecoder(r.Body()).Decode(i); err != nil {
|
|
|
|
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2015-07-30 23:43:22 +02:00
|
|
|
} else if strings.HasPrefix(ct, ApplicationXML) {
|
2016-01-29 12:22:18 +02:00
|
|
|
if err = xml.NewDecoder(r.Body()).Decode(i); err != nil {
|
|
|
|
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2015-07-30 23:43:22 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2016-02-09 08:17:20 +02:00
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
func wrapMiddleware(m interface{}) Middleware {
|
2016-02-09 08:17:20 +02:00
|
|
|
switch m := m.(type) {
|
|
|
|
case Middleware:
|
|
|
|
return m
|
2016-02-15 18:11:29 +02:00
|
|
|
case MiddlewareFunc:
|
2016-02-09 08:17:20 +02:00
|
|
|
return m
|
2016-02-15 18:11:29 +02:00
|
|
|
case func(Handler) Handler:
|
|
|
|
return MiddlewareFunc(m)
|
2016-02-09 08:17:20 +02:00
|
|
|
default:
|
|
|
|
panic("invalid middleware")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
func wrapHandler(h interface{}) Handler {
|
2016-02-09 08:17:20 +02:00
|
|
|
switch h := h.(type) {
|
|
|
|
case Handler:
|
2016-02-15 18:11:29 +02:00
|
|
|
return h
|
2016-02-09 08:17:20 +02:00
|
|
|
case HandlerFunc:
|
|
|
|
return h
|
|
|
|
case func(Context) error:
|
2016-02-15 18:11:29 +02:00
|
|
|
return HandlerFunc(h)
|
|
|
|
default:
|
|
|
|
panic("echo => invalid handler")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerName(h interface{}) string {
|
|
|
|
switch h := h.(type) {
|
|
|
|
case Handler:
|
|
|
|
t := reflect.TypeOf(h)
|
|
|
|
return fmt.Sprintf("%s » %s", t.PkgPath(), t.Name())
|
|
|
|
case HandlerFunc, func(Context) error:
|
|
|
|
return runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
|
2016-02-09 08:17:20 +02:00
|
|
|
default:
|
2016-02-15 18:11:29 +02:00
|
|
|
panic("echo => invalid handler")
|
2016-02-09 08:17:20 +02:00
|
|
|
}
|
|
|
|
}
|