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
|
|
|
"log"
|
|
|
|
"net/http"
|
2015-06-25 01:55:44 +02:00
|
|
|
spath "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-05-22 13:40:01 +02:00
|
|
|
"github.com/bradfitz/http2"
|
2015-07-13 06:40:27 +02:00
|
|
|
"github.com/labstack/gommon/color"
|
2015-05-20 23:38:51 +02:00
|
|
|
"golang.org/x/net/websocket"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Echo struct {
|
2015-05-29 01:50:49 +02:00
|
|
|
prefix string
|
|
|
|
middleware []MiddlewareFunc
|
|
|
|
http2 bool
|
2015-06-04 00:18:27 +02:00
|
|
|
maxParam *int
|
2015-05-29 01:50:49 +02:00
|
|
|
notFoundHandler HandlerFunc
|
|
|
|
defaultHTTPErrorHandler HTTPErrorHandler
|
|
|
|
httpErrorHandler HTTPErrorHandler
|
2015-07-30 23:43:22 +02:00
|
|
|
binder Binder
|
2015-05-29 01:50:49 +02:00
|
|
|
renderer Renderer
|
|
|
|
pool sync.Pool
|
|
|
|
debug bool
|
2015-06-01 09:07:53 +02:00
|
|
|
router *Router
|
|
|
|
}
|
|
|
|
|
|
|
|
Route struct {
|
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
Handler Handler
|
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
|
|
|
|
2015-03-27 23:35:15 +02:00
|
|
|
Middleware interface{}
|
2015-04-19 06:46:00 +02:00
|
|
|
MiddlewareFunc func(HandlerFunc) HandlerFunc
|
2015-04-02 14:02:52 +02:00
|
|
|
Handler interface{}
|
2015-05-20 23:38:51 +02:00
|
|
|
HandlerFunc func(*Context) error
|
2015-04-19 01:47:48 +02:00
|
|
|
|
|
|
|
// HTTPErrorHandler is a centralized HTTP error handler.
|
2015-05-20 23:38:51 +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 {
|
|
|
|
Bind(*http.Request, interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
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-05-23 05:26:52 +02:00
|
|
|
|
|
|
|
//-----------
|
|
|
|
// Protocols
|
|
|
|
//-----------
|
|
|
|
|
|
|
|
WebSocket = "websocket"
|
2015-06-25 01:55:44 +02:00
|
|
|
|
|
|
|
indexFile = "index.html"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
var (
|
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
|
|
|
//--------
|
|
|
|
|
2015-05-18 07:54:29 +02:00
|
|
|
UnsupportedMediaType = errors.New("echo ⇒ unsupported media type")
|
|
|
|
RendererNotRegistered = errors.New("echo ⇒ renderer not registered")
|
2015-07-22 18:44:52 +02:00
|
|
|
InvalidRedirectCode = errors.New("echo ⇒ invalid redirect status code")
|
2015-07-27 17:43:11 +02:00
|
|
|
|
2015-07-24 21:03:36 +02:00
|
|
|
//----------------
|
|
|
|
// Error handlers
|
|
|
|
//----------------
|
|
|
|
|
|
|
|
notFoundHandler = func(c *Context) error {
|
|
|
|
return NewHTTPError(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
badRequestHandler = func(c *Context) error {
|
|
|
|
return NewHTTPError(http.StatusBadRequest)
|
|
|
|
}
|
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-05-08 20:52:06 +02:00
|
|
|
return NewContext(nil, new(Response), e)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
2015-06-04 01:19:03 +02:00
|
|
|
e.router = NewRouter(e)
|
2015-04-19 06:46:00 +02:00
|
|
|
|
|
|
|
//----------
|
|
|
|
// Defaults
|
|
|
|
//----------
|
2015-04-26 07:32:20 +02:00
|
|
|
|
2015-07-13 06:40:27 +02:00
|
|
|
if runtime.GOOS == "windows" {
|
2015-07-18 08:00:19 +02:00
|
|
|
e.ColoredLog(false)
|
2015-07-13 06:40:27 +02:00
|
|
|
}
|
2015-06-27 23:34:22 +02:00
|
|
|
e.HTTP2(false)
|
2015-05-29 01:50:49 +02:00
|
|
|
e.defaultHTTPErrorHandler = func(err error, c *Context) {
|
2015-05-20 23:38:51 +02:00
|
|
|
code := http.StatusInternalServerError
|
|
|
|
msg := http.StatusText(code)
|
|
|
|
if he, ok := err.(*HTTPError); ok {
|
2015-05-22 13:40:01 +02:00
|
|
|
code = he.code
|
|
|
|
msg = he.message
|
2015-05-20 03:54:31 +02:00
|
|
|
}
|
2015-05-27 23:07:52 +02:00
|
|
|
if e.debug {
|
2015-05-20 23:38:51 +02:00
|
|
|
msg = err.Error()
|
2015-05-06 06:55:49 +02:00
|
|
|
}
|
2015-07-25 03:54:42 +02:00
|
|
|
if !c.response.committed {
|
|
|
|
http.Error(c.response, msg, code)
|
|
|
|
}
|
2015-07-28 07:51:53 +02:00
|
|
|
log.Println(err)
|
2015-05-29 01:50:49 +02:00
|
|
|
}
|
|
|
|
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
|
2015-07-30 23:43:22 +02:00
|
|
|
e.SetBinder(&binder{})
|
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
|
|
|
|
}
|
|
|
|
|
2015-07-18 08:00:19 +02:00
|
|
|
// ColoredLog enable/disable colored log.
|
|
|
|
func (e *Echo) ColoredLog(on bool) {
|
2015-07-27 17:43:11 +02:00
|
|
|
if on {
|
|
|
|
color.Enable()
|
|
|
|
} else {
|
|
|
|
color.Disable()
|
|
|
|
}
|
2015-07-13 06:40:27 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 08:00:19 +02:00
|
|
|
// HTTP2 enable/disable HTTP2 support.
|
2015-05-22 08:11:27 +02:00
|
|
|
func (e *Echo) HTTP2(on bool) {
|
|
|
|
e.http2 = on
|
|
|
|
}
|
|
|
|
|
2015-05-29 01:50:49 +02:00
|
|
|
// DefaultHTTPErrorHandler invokes the default HTTP error handler.
|
|
|
|
func (e *Echo) DefaultHTTPErrorHandler(err error, c *Context) {
|
|
|
|
e.defaultHTTPErrorHandler(err, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-05-20 03:54:31 +02:00
|
|
|
// SetDebug sets debug mode.
|
|
|
|
func (e *Echo) SetDebug(on bool) {
|
2015-05-18 07:54:29 +02:00
|
|
|
e.debug = on
|
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
// Debug returns debug mode.
|
|
|
|
func (e *Echo) Debug() bool {
|
|
|
|
return e.debug
|
|
|
|
}
|
|
|
|
|
2015-03-27 23:35:15 +02:00
|
|
|
// Use adds handler to the middleware chain.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Use(m ...Middleware) {
|
2015-03-27 23:35:15 +02:00
|
|
|
for _, h := range m {
|
2015-05-12 05:41:28 +02:00
|
|
|
e.middleware = append(e.middleware, wrapMiddleware(h))
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect adds a CONNECT route > handler to the router.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Connect(path string, h Handler) {
|
2015-04-06 06:49:55 +02:00
|
|
|
e.add(CONNECT, path, h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete adds a DELETE route > handler to the router.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Delete(path string, h Handler) {
|
2015-04-06 06:49:55 +02:00
|
|
|
e.add(DELETE, path, h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get adds a GET route > handler to the router.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Get(path string, h Handler) {
|
2015-04-06 06:49:55 +02:00
|
|
|
e.add(GET, path, h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Head adds a HEAD route > handler to the router.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Head(path string, h Handler) {
|
2015-04-06 06:49:55 +02:00
|
|
|
e.add(HEAD, path, h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Options adds an OPTIONS route > handler to the router.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Options(path string, h Handler) {
|
2015-04-06 06:49:55 +02:00
|
|
|
e.add(OPTIONS, path, h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Patch adds a PATCH route > handler to the router.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Patch(path string, h Handler) {
|
2015-04-06 06:49:55 +02:00
|
|
|
e.add(PATCH, path, h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post adds a POST route > handler to the router.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Post(path string, h Handler) {
|
2015-04-06 06:49:55 +02:00
|
|
|
e.add(POST, path, h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put adds a PUT route > handler to the router.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Put(path string, h Handler) {
|
2015-04-06 06:49:55 +02:00
|
|
|
e.add(PUT, path, h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trace adds a TRACE route > handler to the router.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Trace(path string, h Handler) {
|
2015-04-06 06:49:55 +02:00
|
|
|
e.add(TRACE, path, h)
|
2015-04-02 14:02:52 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 23:38:51 +02:00
|
|
|
// WebSocket adds a WebSocket route > handler to the router.
|
|
|
|
func (e *Echo) WebSocket(path string, h HandlerFunc) {
|
2015-05-22 07:48:34 +02:00
|
|
|
e.Get(path, func(c *Context) (err error) {
|
2015-05-20 23:38:51 +02:00
|
|
|
wss := websocket.Server{
|
|
|
|
Handler: func(ws *websocket.Conn) {
|
2015-05-22 13:40:01 +02:00
|
|
|
c.socket = ws
|
|
|
|
c.response.status = http.StatusSwitchingProtocols
|
2015-05-22 07:48:34 +02:00
|
|
|
err = h(c)
|
2015-05-20 23:38:51 +02:00
|
|
|
},
|
|
|
|
}
|
2015-06-10 00:24:01 +02:00
|
|
|
wss.ServeHTTP(c.response, c.request)
|
2015-05-22 07:48:34 +02:00
|
|
|
return err
|
2015-05-20 23:38:51 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
func (e *Echo) add(method, path string, h Handler) {
|
2015-05-31 18:37:26 +02:00
|
|
|
path = e.prefix + path
|
|
|
|
e.router.Add(method, path, wrapHandler(h), e)
|
2015-06-01 09:07:53 +02:00
|
|
|
r := Route{
|
|
|
|
Method: method,
|
|
|
|
Path: path,
|
|
|
|
Handler: runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name(),
|
|
|
|
}
|
|
|
|
e.router.routes = append(e.router.routes, r)
|
2015-04-22 07:12:41 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
// Index serves index file.
|
|
|
|
func (e *Echo) Index(file string) {
|
|
|
|
e.ServeFile("/", file)
|
2015-04-22 07:12:41 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
// Favicon serves the default favicon - GET /favicon.ico.
|
|
|
|
func (e *Echo) Favicon(file string) {
|
|
|
|
e.ServeFile("/favicon.ico", file)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2015-05-27 23:07:52 +02:00
|
|
|
// Static serves static files from a directory. It's an alias for `Echo.ServeDir`
|
|
|
|
func (e *Echo) Static(path, dir string) {
|
|
|
|
e.ServeDir(path, dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeDir serves files from a directory.
|
|
|
|
func (e *Echo) ServeDir(path, dir string) {
|
2015-05-20 23:38:51 +02:00
|
|
|
e.Get(path+"*", func(c *Context) error {
|
2015-05-27 23:07:52 +02:00
|
|
|
return serveFile(dir, c.P(0), c) // Param `_name`
|
2015-03-27 23:35:15 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeFile serves a file.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) ServeFile(path, file string) {
|
2015-05-20 23:38:51 +02:00
|
|
|
e.Get(path, func(c *Context) error {
|
2015-06-25 01:55:44 +02:00
|
|
|
dir, file := spath.Split(file)
|
2015-05-27 23:07:52 +02:00
|
|
|
return serveFile(dir, file, c)
|
2015-03-27 23:35:15 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-27 23:07:52 +02:00
|
|
|
func serveFile(dir, file string, c *Context) error {
|
|
|
|
fs := http.Dir(dir)
|
|
|
|
f, err := fs.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return NewHTTPError(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
fi, _ := f.Stat()
|
2015-05-27 23:07:52 +02:00
|
|
|
if fi.IsDir() {
|
2015-06-25 01:55:44 +02:00
|
|
|
file = spath.Join(file, indexFile)
|
2015-06-20 02:52:42 +02:00
|
|
|
f, err = fs.Open(file)
|
2015-06-20 00:37:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return NewHTTPError(http.StatusForbidden)
|
|
|
|
}
|
|
|
|
fi, _ = f.Stat()
|
2015-05-27 23:07:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeContent(c.response, c.request, fi.Name(), fi.ModTime(), f)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 09:07:53 +02:00
|
|
|
// Group creates a new sub router with prefix. It inherits all properties from
|
|
|
|
// the parent. Passing middleware overrides parent middleware.
|
|
|
|
func (e *Echo) Group(prefix string, m ...Middleware) *Group {
|
|
|
|
g := &Group{*e}
|
|
|
|
g.echo.prefix += prefix
|
|
|
|
if len(m) > 0 {
|
|
|
|
g.echo.middleware = nil
|
|
|
|
g.Use(m...)
|
|
|
|
}
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
// URI generates a URI from handler.
|
|
|
|
func (e *Echo) URI(h Handler, params ...interface{}) string {
|
|
|
|
uri := new(bytes.Buffer)
|
2015-06-01 09:07:53 +02:00
|
|
|
pl := len(params)
|
2015-05-20 03:54:31 +02:00
|
|
|
n := 0
|
2015-06-01 09:07:53 +02:00
|
|
|
hn := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
|
|
|
|
for _, r := range e.router.routes {
|
|
|
|
if r.Handler == hn {
|
|
|
|
for i, l := 0, len(r.Path); i < l; i++ {
|
|
|
|
if r.Path[i] == ':' && n < pl {
|
|
|
|
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.
|
2015-05-20 03:54:31 +02:00
|
|
|
func (e *Echo) URL(h Handler, params ...interface{}) string {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-07-02 20:14:09 +02:00
|
|
|
// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
|
2015-04-07 22:02:23 +02:00
|
|
|
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-04-09 23:59:31 +02:00
|
|
|
c := e.pool.Get().(*Context)
|
2015-05-23 05:26:52 +02:00
|
|
|
h, echo := e.router.Find(r.Method, r.URL.Path, c)
|
2015-04-03 00:15:09 +02:00
|
|
|
if echo != nil {
|
|
|
|
e = echo
|
|
|
|
}
|
2015-05-30 02:20:13 +02:00
|
|
|
c.reset(r, w, e)
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2015-05-18 20:33:11 +02:00
|
|
|
// Chain middleware with handler in the end
|
|
|
|
for i := len(e.middleware) - 1; i >= 0; i-- {
|
|
|
|
h = e.middleware[i](h)
|
2015-04-19 01:47:48 +02:00
|
|
|
}
|
2015-05-18 20:33:11 +02:00
|
|
|
|
|
|
|
// Execute chain
|
2015-06-01 09:07:53 +02:00
|
|
|
if err := h(c); err != nil {
|
|
|
|
e.httpErrorHandler(err, c)
|
2015-05-18 20:33:11 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 07:23:47 +02:00
|
|
|
e.pool.Put(c)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2015-07-02 20:14:09 +02:00
|
|
|
// Server returns the internal *http.Server.
|
2015-06-27 23:34:22 +02:00
|
|
|
func (e *Echo) Server(addr string) *http.Server {
|
|
|
|
s := &http.Server{Addr: addr}
|
|
|
|
s.Handler = e
|
|
|
|
if e.http2 {
|
|
|
|
http2.ConfigureServer(s, nil)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
// Run runs a server.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Run(addr string) {
|
2015-06-27 23:34:22 +02:00
|
|
|
s := e.Server(addr)
|
2015-05-22 08:11:27 +02:00
|
|
|
e.run(s)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
// RunTLS runs a server with TLS configuration.
|
2015-04-03 14:24:47 +02:00
|
|
|
func (e *Echo) RunTLS(addr, certFile, keyFile string) {
|
2015-06-27 23:34:22 +02:00
|
|
|
s := e.Server(addr)
|
2015-05-22 08:11:27 +02:00
|
|
|
e.run(s, certFile, keyFile)
|
2015-04-03 14:24:47 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
// RunServer runs a custom server.
|
2015-07-02 20:14:09 +02:00
|
|
|
func (e *Echo) RunServer(s *http.Server) {
|
|
|
|
e.run(s)
|
2015-04-03 14:24:47 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
// RunTLSServer runs a custom server with TLS configuration.
|
2015-07-02 20:14:09 +02:00
|
|
|
func (e *Echo) RunTLSServer(s *http.Server, certFile, keyFile string) {
|
|
|
|
e.run(s, certFile, keyFile)
|
2015-05-22 08:11:27 +02:00
|
|
|
}
|
|
|
|
|
2015-05-22 13:40:01 +02:00
|
|
|
func (e *Echo) run(s *http.Server, files ...string) {
|
|
|
|
if len(files) == 0 {
|
2015-05-22 08:11:27 +02:00
|
|
|
log.Fatal(s.ListenAndServe())
|
2015-05-22 13:40:01 +02:00
|
|
|
} else if len(files) == 2 {
|
|
|
|
log.Fatal(s.ListenAndServeTLS(files[0], files[1]))
|
2015-05-22 08:11:27 +02:00
|
|
|
} else {
|
2015-05-22 13:40:01 +02:00
|
|
|
log.Fatal("echo => invalid TLS configuration")
|
2015-05-22 08:11:27 +02:00
|
|
|
}
|
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-07-02 20:14:09 +02:00
|
|
|
// wrapMiddleware wraps middleware.
|
2015-05-12 05:41:28 +02:00
|
|
|
func wrapMiddleware(m Middleware) MiddlewareFunc {
|
2015-03-27 23:35:15 +02:00
|
|
|
switch m := m.(type) {
|
2015-05-11 06:09:28 +02:00
|
|
|
case MiddlewareFunc:
|
|
|
|
return m
|
|
|
|
case func(HandlerFunc) HandlerFunc:
|
|
|
|
return m
|
2015-05-12 05:41:28 +02:00
|
|
|
case HandlerFunc:
|
|
|
|
return wrapHandlerFuncMW(m)
|
2015-05-20 23:38:51 +02:00
|
|
|
case func(*Context) error:
|
2015-05-12 05:41:28 +02:00
|
|
|
return wrapHandlerFuncMW(m)
|
2015-04-21 08:17:21 +02:00
|
|
|
case func(http.Handler) http.Handler:
|
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
2015-05-20 23:38:51 +02:00
|
|
|
return func(c *Context) (err error) {
|
2015-04-21 08:17:21 +02:00
|
|
|
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-05-22 13:40:01 +02:00
|
|
|
c.response.writer = w
|
|
|
|
c.request = r
|
2015-05-20 23:38:51 +02:00
|
|
|
err = h(c)
|
2015-05-22 13:40:01 +02:00
|
|
|
})).ServeHTTP(c.response.writer, c.request)
|
2015-04-21 08:17:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 05:41:28 +02:00
|
|
|
case http.Handler:
|
|
|
|
return wrapHTTPHandlerFuncMW(m.ServeHTTP)
|
2015-03-30 08:35:08 +02:00
|
|
|
case func(http.ResponseWriter, *http.Request):
|
2015-05-12 05:41:28 +02:00
|
|
|
return wrapHTTPHandlerFuncMW(m)
|
|
|
|
default:
|
2015-05-18 22:38:35 +02:00
|
|
|
panic("echo => unknown middleware")
|
2015-05-12 05:41:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 20:14:09 +02:00
|
|
|
// wrapHandlerFuncMW wraps HandlerFunc middleware.
|
2015-05-12 05:41:28 +02:00
|
|
|
func wrapHandlerFuncMW(m HandlerFunc) MiddlewareFunc {
|
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
2015-05-20 23:38:51 +02:00
|
|
|
return func(c *Context) error {
|
|
|
|
if err := m(c); err != nil {
|
|
|
|
return err
|
2015-04-19 06:46:00 +02:00
|
|
|
}
|
2015-05-12 05:41:28 +02:00
|
|
|
return h(c)
|
2015-04-19 01:47:48 +02:00
|
|
|
}
|
2015-05-12 05:41:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 20:14:09 +02:00
|
|
|
// wrapHTTPHandlerFuncMW wraps http.HandlerFunc middleware.
|
2015-05-12 05:41:28 +02:00
|
|
|
func wrapHTTPHandlerFuncMW(m http.HandlerFunc) MiddlewareFunc {
|
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
2015-05-20 23:38:51 +02:00
|
|
|
return func(c *Context) error {
|
2015-05-22 13:40:01 +02:00
|
|
|
if !c.response.committed {
|
|
|
|
m.ServeHTTP(c.response.writer, c.request)
|
2015-04-19 06:46:00 +02:00
|
|
|
}
|
2015-05-12 05:41:28 +02:00
|
|
|
return h(c)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 20:14:09 +02:00
|
|
|
// wrapHandler wraps handler.
|
2015-05-12 05:41:28 +02:00
|
|
|
func wrapHandler(h Handler) HandlerFunc {
|
2015-03-27 23:35:15 +02:00
|
|
|
switch h := h.(type) {
|
2015-04-27 07:41:41 +02:00
|
|
|
case HandlerFunc:
|
|
|
|
return h
|
2015-05-20 23:38:51 +02:00
|
|
|
case func(*Context) error:
|
2015-04-27 07:41:41 +02:00
|
|
|
return h
|
2015-03-30 08:35:08 +02:00
|
|
|
case http.Handler, http.HandlerFunc:
|
2015-05-20 23:38:51 +02:00
|
|
|
return func(c *Context) error {
|
2015-05-22 13:40:01 +02:00
|
|
|
h.(http.Handler).ServeHTTP(c.response, c.request)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
2015-03-30 08:35:08 +02:00
|
|
|
case func(http.ResponseWriter, *http.Request):
|
2015-05-20 23:38:51 +02:00
|
|
|
return func(c *Context) error {
|
2015-05-22 13:40:01 +02:00
|
|
|
h(c.response, c.request)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
2015-03-27 23:35:15 +02:00
|
|
|
default:
|
2015-05-31 00:20:36 +02:00
|
|
|
panic("echo => unknown handler")
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-30 23:43:22 +02:00
|
|
|
|
2015-08-01 04:25:03 +02:00
|
|
|
func (binder) Bind(r *http.Request, i interface{}) (err error) {
|
2015-07-30 23:43:22 +02:00
|
|
|
ct := r.Header.Get(ContentType)
|
|
|
|
err = UnsupportedMediaType
|
|
|
|
if strings.HasPrefix(ct, ApplicationJSON) {
|
|
|
|
err = json.NewDecoder(r.Body).Decode(i)
|
|
|
|
} else if strings.HasPrefix(ct, ApplicationXML) {
|
|
|
|
err = xml.NewDecoder(r.Body).Decode(i)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|