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-01-29 19:16:05 +02:00
|
|
|
"path"
|
2015-09-17 21:12:01 +02:00
|
|
|
"path/filepath"
|
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"
|
|
|
|
"github.com/labstack/echo/engine/fasthttp"
|
|
|
|
"github.com/labstack/echo/engine/standard"
|
2016-02-05 05:29:49 +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 {
|
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-12-22 01:20:49 +02:00
|
|
|
hook engine.HandlerFunc
|
2015-11-24 06:33:13 +02:00
|
|
|
autoIndex bool
|
2015-12-22 01:20:49 +02:00
|
|
|
engineType engine.Type
|
|
|
|
engine engine.Engine
|
2015-11-21 05:13:22 +02:00
|
|
|
router *Router
|
2016-02-05 05:29:49 +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 {
|
|
|
|
Process(HandlerFunc) HandlerFunc
|
|
|
|
}
|
|
|
|
|
2015-04-19 06:46:00 +02:00
|
|
|
MiddlewareFunc func(HandlerFunc) HandlerFunc
|
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"
|
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-06-25 01:55:44 +02:00
|
|
|
|
2015-11-24 06:33:13 +02:00
|
|
|
indexPage = "index.html"
|
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-01-29 12:22:18 +02:00
|
|
|
UnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType)
|
2015-11-22 19:26:11 +02:00
|
|
|
RendererNotRegistered = errors.New("renderer not registered")
|
|
|
|
InvalidRedirectCode = 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)
|
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-09 03:26:00 +02:00
|
|
|
func (f MiddlewareFunc) Process(h HandlerFunc) HandlerFunc {
|
|
|
|
return f(h)
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (e *Echo) SetLogger(logger logger.Logger) {
|
|
|
|
e.logger = logger
|
2015-11-21 05:13:22 +02:00
|
|
|
}
|
|
|
|
|
2015-12-01 21:22:45 +02:00
|
|
|
// Logger returns the logger instance.
|
2016-02-05 05:29:49 +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-12-04 00:14:14 +02:00
|
|
|
// AutoIndex enable/disable automatically creating an index page for the directory.
|
2015-11-24 06:33:13 +02:00
|
|
|
func (e *Echo) AutoIndex(on bool) {
|
|
|
|
e.autoIndex = on
|
|
|
|
}
|
|
|
|
|
2015-11-19 17:40:24 +02:00
|
|
|
// 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.
|
2015-12-22 01:20:49 +02:00
|
|
|
func (e *Echo) Hook(h engine.HandlerFunc) {
|
2015-11-19 17:40:24 +02:00
|
|
|
e.hook = h
|
|
|
|
}
|
|
|
|
|
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-09 08:17:20 +02:00
|
|
|
func (e *Echo) Connect(path string, handler interface{}) {
|
|
|
|
e.add(CONNECT, path, handler)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete adds a DELETE route > handler to the router.
|
2016-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) Delete(path string, handler interface{}) {
|
|
|
|
e.add(DELETE, path, handler)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get adds a GET route > handler to the router.
|
2016-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) Get(path string, handler interface{}) {
|
|
|
|
e.add(GET, path, handler)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Head adds a HEAD route > handler to the router.
|
2016-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) Head(path string, handler interface{}) {
|
|
|
|
e.add(HEAD, path, handler)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Options adds an OPTIONS route > handler to the router.
|
2016-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) Options(path string, handler interface{}) {
|
|
|
|
e.add(OPTIONS, path, handler)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Patch adds a PATCH route > handler to the router.
|
2016-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) Patch(path string, handler interface{}) {
|
|
|
|
e.add(PATCH, path, handler)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post adds a POST route > handler to the router.
|
2016-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) Post(path string, handler interface{}) {
|
|
|
|
e.add(POST, path, handler)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put adds a PUT route > handler to the router.
|
2016-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) Put(path string, handler interface{}) {
|
|
|
|
e.add(PUT, path, handler)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trace adds a TRACE route > handler to the router.
|
2016-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) Trace(path string, handler interface{}) {
|
|
|
|
e.add(TRACE, path, handler)
|
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-09 08:17:20 +02:00
|
|
|
func (e *Echo) Any(path string, handler interface{}) {
|
2015-08-26 05:36:15 +02:00
|
|
|
for _, m := range methods {
|
2016-02-09 08:17:20 +02:00
|
|
|
e.add(m, path, handler)
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match adds a route > handler to the router for multiple HTTP methods provided.
|
2016-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) Match(methods []string, path string, handler interface{}) {
|
2015-08-26 05:36:15 +02:00
|
|
|
for _, m := range methods {
|
2016-02-09 08:17:20 +02:00
|
|
|
e.add(m, path, handler)
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 01:20:49 +02:00
|
|
|
// NOTE: v2
|
2016-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) add(method, path string, h interface{}) {
|
2015-05-31 18:37:26 +02:00
|
|
|
path = e.prefix + path
|
2016-02-09 08:17:20 +02:00
|
|
|
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-12-04 03:23:53 +02:00
|
|
|
e.Get(path+"*", func(c Context) error {
|
2015-11-24 06:33:13 +02:00
|
|
|
return e.serveFile(dir, c.P(0), c) // Param `_*`
|
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-12-04 03:23:53 +02:00
|
|
|
e.Get(path, func(c Context) error {
|
2015-09-17 21:12:01 +02:00
|
|
|
dir, file := filepath.Split(file)
|
2015-11-24 06:33:13 +02:00
|
|
|
return e.serveFile(dir, file, c)
|
2015-03-27 23:35:15 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-04 03:23:53 +02:00
|
|
|
func (e *Echo) serveFile(dir, file string, c Context) (err error) {
|
2016-01-29 09:46:11 +02:00
|
|
|
fs := http.Dir(dir)
|
|
|
|
f, err := fs.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return NewHTTPError(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, _ := f.Stat()
|
|
|
|
if fi.IsDir() {
|
|
|
|
/* NOTE:
|
|
|
|
Not checking the Last-Modified header as it caches the response `304` when
|
|
|
|
changing differnt directories for the same path.
|
|
|
|
*/
|
|
|
|
d := f
|
|
|
|
|
|
|
|
// Index file
|
2016-01-29 19:16:05 +02:00
|
|
|
file = path.Join(file, indexPage)
|
2016-01-29 09:46:11 +02:00
|
|
|
f, err = fs.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
if e.autoIndex {
|
|
|
|
// Auto index
|
|
|
|
return listDir(d, c)
|
|
|
|
}
|
|
|
|
return NewHTTPError(http.StatusForbidden)
|
|
|
|
}
|
|
|
|
fi, _ = f.Stat() // Index file stat
|
|
|
|
}
|
|
|
|
c.Response().WriteHeader(http.StatusOK)
|
|
|
|
io.Copy(c.Response(), f)
|
|
|
|
// TODO:
|
2016-01-09 19:44:18 +02:00
|
|
|
// http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f)
|
2015-11-24 06:33:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 03:23:53 +02:00
|
|
|
func listDir(d http.File, c Context) (err error) {
|
2015-11-24 06:33:13 +02:00
|
|
|
dirs, err := d.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create directory index
|
|
|
|
w := c.Response()
|
|
|
|
w.Header().Set(ContentType, TextHTMLCharsetUTF8)
|
|
|
|
fmt.Fprintf(w, "<pre>\n")
|
|
|
|
for _, d := range dirs {
|
|
|
|
name := d.Name()
|
|
|
|
color := "#212121"
|
|
|
|
if d.IsDir() {
|
|
|
|
color = "#e91e63"
|
|
|
|
name += "/"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "<a href=\"%s\" style=\"color: %s;\">%s</a>\n", name, color, name)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "</pre>\n")
|
|
|
|
return
|
2015-05-27 23:07:52 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2015-12-22 01:20:49 +02:00
|
|
|
func (e *Echo) Group(prefix string, m ...MiddlewareFunc) *Group {
|
2015-06-01 09:07:53 +02:00
|
|
|
g := &Group{*e}
|
|
|
|
g.echo.prefix += prefix
|
2015-09-03 08:08:37 +02:00
|
|
|
if len(m) == 0 {
|
|
|
|
mw := make([]MiddlewareFunc, len(g.echo.middleware))
|
|
|
|
copy(mw, g.echo.middleware)
|
|
|
|
g.echo.middleware = mw
|
|
|
|
} else {
|
2015-06-01 09:07:53 +02:00
|
|
|
g.echo.middleware = nil
|
|
|
|
g.Use(m...)
|
|
|
|
}
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
// URI generates a URI from handler.
|
2015-12-22 01:20:49 +02:00
|
|
|
func (e *Echo) URI(h HandlerFunc, params ...interface{}) string {
|
2015-05-20 03:54:31 +02:00
|
|
|
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-12-22 01:20:49 +02:00
|
|
|
func (e *Echo) URL(h HandlerFunc, params ...interface{}) string {
|
2015-05-20 03:54:31 +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-02-09 08:17:20 +02:00
|
|
|
func (e *Echo) handle(req engine.Request, res engine.Response) {
|
2016-01-29 09:46:11 +02:00
|
|
|
if e.hook != nil {
|
|
|
|
e.hook(req, res)
|
|
|
|
}
|
2015-03-27 23:35:15 +02:00
|
|
|
|
2016-01-29 09:46:11 +02:00
|
|
|
c := e.pool.Get().(*context)
|
|
|
|
h, e := e.router.Find(req.Method(), req.URL().Path(), c)
|
|
|
|
c.reset(req, res, e)
|
|
|
|
|
|
|
|
// Chain middleware with handler in the end
|
|
|
|
for i := len(e.middleware) - 1; i >= 0; i-- {
|
|
|
|
h = e.middleware[i](h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute chain
|
|
|
|
if err := h(c); err != nil {
|
|
|
|
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-01-29 09:46:11 +02:00
|
|
|
// Server returns the internal *http.Server.
|
|
|
|
// func (e *Echo) Server(addr string) *http.Server {
|
|
|
|
// s := &http.Server{Addr: addr, Handler: e}
|
|
|
|
// // TODO: Remove in Go 1.6+
|
|
|
|
// if e.http2 {
|
|
|
|
// http2.ConfigureServer(s, nil)
|
|
|
|
// }
|
|
|
|
// return s
|
|
|
|
// }
|
|
|
|
|
2015-12-22 01:20:49 +02:00
|
|
|
func (e *Echo) SetEngine(t engine.Type) {
|
|
|
|
e.engineType = t
|
|
|
|
}
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
// Run runs a server.
|
2016-02-05 00:40:08 +02:00
|
|
|
func (e *Echo) Run(addr string) {
|
|
|
|
c := &engine.Config{Address: addr}
|
2016-02-08 18:36:26 +02:00
|
|
|
e.RunConfig(c)
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunTLS runs a server with TLS configuration.
|
|
|
|
func (e *Echo) RunTLS(addr, certfile, keyfile string) {
|
|
|
|
c := &engine.Config{
|
|
|
|
Address: addr,
|
|
|
|
TLSCertfile: certfile,
|
|
|
|
TLSKeyfile: keyfile,
|
|
|
|
}
|
2016-02-08 18:36:26 +02:00
|
|
|
e.RunConfig(c)
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
|
|
|
|
2016-02-08 18:36:26 +02:00
|
|
|
// RunConfig runs a server with engine configuration.
|
|
|
|
func (e *Echo) RunConfig(config *engine.Config) {
|
2015-12-22 01:20:49 +02:00
|
|
|
switch e.engineType {
|
|
|
|
case engine.FastHTTP:
|
2016-02-09 08:17:20 +02:00
|
|
|
e.engine = fasthttp.NewServer(config, e.handle, e.logger)
|
2016-02-05 05:29:49 +02:00
|
|
|
default:
|
2016-02-09 08:17:20 +02:00
|
|
|
e.engine = standard.NewServer(config, e.handle, e.logger)
|
2015-12-22 01:20:49 +02:00
|
|
|
}
|
|
|
|
e.engine.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)
|
2015-07-30 23:43:22 +02:00
|
|
|
err = UnsupportedMediaType
|
|
|
|
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
|
|
|
|
|
|
|
func wrapMiddleware(m interface{}) MiddlewareFunc {
|
|
|
|
switch m := m.(type) {
|
|
|
|
case Middleware:
|
|
|
|
return m.Process
|
|
|
|
case MiddlewareFunc:
|
|
|
|
return m
|
|
|
|
case func(HandlerFunc) HandlerFunc:
|
|
|
|
return m
|
|
|
|
default:
|
|
|
|
panic("invalid middleware")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrapHandler(h interface{}) HandlerFunc {
|
|
|
|
switch h := h.(type) {
|
|
|
|
case Handler:
|
|
|
|
return h.Handle
|
|
|
|
case HandlerFunc:
|
|
|
|
return h
|
|
|
|
case func(Context) error:
|
|
|
|
return h
|
|
|
|
default:
|
|
|
|
panic("invalid handler")
|
|
|
|
}
|
|
|
|
}
|