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-22 07:12:41 +02:00
|
|
|
"fmt"
|
2015-04-07 22:02:23 +02:00
|
|
|
"io"
|
2015-03-27 23:35:15 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
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-04-19 07:00:44 +02:00
|
|
|
"github.com/labstack/gommon/color"
|
2015-04-19 06:46:00 +02:00
|
|
|
"github.com/mattn/go-colorable"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Echo struct {
|
2015-04-19 01:47:48 +02:00
|
|
|
Router *router
|
|
|
|
prefix string
|
|
|
|
middleware []MiddlewareFunc
|
|
|
|
maxParam byte
|
|
|
|
notFoundHandler HandlerFunc
|
|
|
|
httpErrorHandler HTTPErrorHandler
|
|
|
|
binder BindFunc
|
|
|
|
renderer Renderer
|
2015-04-22 07:12:41 +02:00
|
|
|
uris map[Handler]string
|
2015-04-19 01:47:48 +02:00
|
|
|
pool sync.Pool
|
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-04-19 01:47:48 +02:00
|
|
|
HandlerFunc func(*Context) error
|
|
|
|
|
|
|
|
// HTTPErrorHandler is a centralized HTTP error handler.
|
|
|
|
HTTPErrorHandler func(error, *Context)
|
|
|
|
|
|
|
|
BindFunc func(*http.Request, interface{}) error
|
2015-04-15 19:10:05 +02:00
|
|
|
|
|
|
|
// Renderer is the interface that wraps the Render method.
|
|
|
|
//
|
|
|
|
// Render renders the HTML template with given name and specified data.
|
|
|
|
// It writes the output to w.
|
|
|
|
Renderer interface {
|
|
|
|
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-04-04 00:40:36 +02:00
|
|
|
MIMEJSON = "application/json"
|
|
|
|
MIMEText = "text/plain"
|
2015-04-06 16:52:00 +02:00
|
|
|
MIMEHTML = "text/html"
|
2015-04-04 00:40:36 +02:00
|
|
|
MIMEForm = "application/x-www-form-urlencoded"
|
|
|
|
MIMEMultipartForm = "multipart/form-data"
|
2015-03-27 23:35:15 +02:00
|
|
|
|
|
|
|
HeaderAccept = "Accept"
|
|
|
|
HeaderContentDisposition = "Content-Disposition"
|
|
|
|
HeaderContentLength = "Content-Length"
|
|
|
|
HeaderContentType = "Content-Type"
|
|
|
|
)
|
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
var (
|
2015-04-02 14:02:52 +02:00
|
|
|
methods = [...]string{
|
2015-04-06 06:49:55 +02:00
|
|
|
CONNECT,
|
|
|
|
DELETE,
|
|
|
|
GET,
|
|
|
|
HEAD,
|
|
|
|
OPTIONS,
|
|
|
|
PATCH,
|
|
|
|
POST,
|
|
|
|
PUT,
|
|
|
|
TRACE,
|
2015-04-01 17:05:54 +02:00
|
|
|
}
|
2015-04-03 05:18:34 +02:00
|
|
|
|
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-04-20 07:04:30 +02:00
|
|
|
UnsupportedMediaType = errors.New("echo: unsupported media type")
|
|
|
|
RendererNotRegistered = errors.New("echo: renderer not registered")
|
2015-04-01 17:05:54 +02:00
|
|
|
)
|
|
|
|
|
2015-04-06 05:08:52 +02:00
|
|
|
// New creates an Echo instance.
|
2015-03-30 08:35:08 +02:00
|
|
|
func New() (e *Echo) {
|
2015-04-22 07:53:15 +02:00
|
|
|
e = &Echo{
|
|
|
|
uris: make(map[Handler]string),
|
|
|
|
}
|
2015-03-30 08:35:08 +02:00
|
|
|
e.Router = NewRouter(e)
|
|
|
|
e.pool.New = func() interface{} {
|
2015-03-27 23:35:15 +02:00
|
|
|
return &Context{
|
|
|
|
Response: &response{},
|
2015-03-30 08:35:08 +02:00
|
|
|
params: make(Params, e.maxParam),
|
2015-03-27 23:35:15 +02:00
|
|
|
store: make(store),
|
2015-04-02 23:41:36 +02:00
|
|
|
echo: e, // TODO: Do we need this?
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-19 06:46:00 +02:00
|
|
|
|
|
|
|
//----------
|
|
|
|
// Defaults
|
|
|
|
//----------
|
2015-04-22 07:53:15 +02:00
|
|
|
e.MaxParam(5)
|
|
|
|
e.NotFoundHandler(func(c *Context) {
|
2015-04-19 06:46:00 +02:00
|
|
|
http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
|
|
|
})
|
2015-04-22 07:53:15 +02:00
|
|
|
e.HTTPErrorHandler(func(err error, c *Context) {
|
2015-04-19 06:46:00 +02:00
|
|
|
if err != nil {
|
|
|
|
// TODO: Warning
|
2015-04-20 04:54:17 +02:00
|
|
|
log.Printf("echo: %s", color.Yellow("http error handler not registered"))
|
2015-04-19 06:46:00 +02:00
|
|
|
http.Error(c.Response, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2015-04-22 07:53:15 +02:00
|
|
|
})
|
|
|
|
e.Binder(func(r *http.Request, v interface{}) error {
|
2015-04-19 06:46:00 +02:00
|
|
|
ct := r.Header.Get(HeaderContentType)
|
|
|
|
if strings.HasPrefix(ct, MIMEJSON) {
|
|
|
|
return json.NewDecoder(r.Body).Decode(v)
|
|
|
|
} else if strings.HasPrefix(ct, MIMEForm) {
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-20 07:04:30 +02:00
|
|
|
return UnsupportedMediaType
|
2015-04-22 07:53:15 +02:00
|
|
|
})
|
2015-03-27 23:35:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:43:52 +02:00
|
|
|
// Group creates a new sub router with prefix and inherits all properties from
|
|
|
|
// the parent. Passing middleware overrides parent middleware.
|
2015-04-07 22:02:23 +02:00
|
|
|
func (e *Echo) Group(pfx string, m ...Middleware) *Echo {
|
2015-04-03 00:15:09 +02:00
|
|
|
g := *e
|
|
|
|
g.prefix = pfx
|
2015-04-07 22:02:23 +02:00
|
|
|
if len(m) > 0 {
|
|
|
|
g.middleware = nil
|
|
|
|
g.Use(m...)
|
|
|
|
}
|
2015-04-03 00:15:09 +02:00
|
|
|
return &g
|
2015-04-02 14:02:52 +02:00
|
|
|
}
|
2015-03-27 23:35:15 +02:00
|
|
|
|
|
|
|
// MaxParam sets the maximum allowed path parameters. Default is 5, good enough
|
|
|
|
// for many users.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) MaxParam(n uint8) {
|
|
|
|
e.maxParam = n
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-11 06:48:26 +02:00
|
|
|
// NotFoundHandler registers a custom NotFound handler.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) NotFoundHandler(h Handler) {
|
|
|
|
e.notFoundHandler = wrapH(h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-19 01:47:48 +02:00
|
|
|
// HTTPErrorHandler registers an HTTP error handler.
|
|
|
|
func (e *Echo) HTTPErrorHandler(h HTTPErrorHandler) {
|
|
|
|
e.httpErrorHandler = h
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:10:05 +02:00
|
|
|
// Binder registers a custom binder. It's invoked by Context.Bind API.
|
|
|
|
func (e *Echo) Binder(b BindFunc) {
|
|
|
|
e.binder = b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renderer registers an HTML template renderer. It's invoked by Context.Render
|
|
|
|
// API.
|
2015-04-09 23:59:31 +02:00
|
|
|
func (e *Echo) Renderer(r Renderer) {
|
|
|
|
e.renderer = r
|
2015-04-08 02:34:00 +02:00
|
|
|
}
|
|
|
|
|
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-03-30 08:35:08 +02:00
|
|
|
e.middleware = append(e.middleware, wrapM(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-04-22 07:12:41 +02:00
|
|
|
// URI generates a URI from handler.
|
|
|
|
func (e *Echo) URI(h Handler, params ...string) string {
|
|
|
|
uri := new(bytes.Buffer)
|
|
|
|
lp := len(params)
|
|
|
|
n := 0
|
|
|
|
if path, ok := e.uris[fmt.Sprintf("%v", h)]; ok {
|
|
|
|
for i, l := 0, len(path); i < l; i++ {
|
|
|
|
if path[i] == ':' && n < lp {
|
|
|
|
for ; i < l && path[i] != '/'; i++ {
|
|
|
|
}
|
|
|
|
uri.WriteString(params[n])
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if i < l {
|
|
|
|
uri.WriteByte(path[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uri.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// URL is an alias for URI
|
|
|
|
func (e *Echo) URL(h Handler, params ...string) string {
|
|
|
|
return e.URI(h, params...)
|
|
|
|
}
|
|
|
|
|
2015-04-02 14:02:52 +02:00
|
|
|
func (e *Echo) add(method, path string, h Handler) {
|
2015-04-22 07:12:41 +02:00
|
|
|
e.uris[fmt.Sprintf("%v", h)] = path
|
2015-04-02 23:41:36 +02:00
|
|
|
e.Router.Add(method, e.prefix+path, wrapH(h), e)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Static serves static files.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Static(path, root string) {
|
2015-03-27 23:35:15 +02:00
|
|
|
fs := http.StripPrefix(path, http.FileServer(http.Dir(root)))
|
2015-04-19 01:47:48 +02:00
|
|
|
e.Get(path+"/*", func(c *Context) error {
|
2015-03-27 23:35:15 +02:00
|
|
|
fs.ServeHTTP(c.Response, c.Request)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
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-04-19 01:47:48 +02:00
|
|
|
e.Get(path, func(c *Context) error {
|
2015-03-27 23:35:15 +02:00
|
|
|
http.ServeFile(c.Response, c.Request, file)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-03-27 23:35:15 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index serves index file.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) Index(file string) {
|
|
|
|
e.ServeFile("/", file)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
h, echo := e.Router.Find(r.Method, r.URL.Path, c.params)
|
2015-04-03 00:15:09 +02:00
|
|
|
if echo != nil {
|
|
|
|
e = echo
|
|
|
|
}
|
2015-04-02 14:02:52 +02:00
|
|
|
if h == nil {
|
|
|
|
h = e.notFoundHandler
|
|
|
|
}
|
2015-04-07 22:02:23 +02:00
|
|
|
c.reset(w, r, e)
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
// Chain middleware with handler in the end
|
2015-04-02 14:02:52 +02:00
|
|
|
for i := len(e.middleware) - 1; i >= 0; i-- {
|
2015-04-19 06:46:00 +02:00
|
|
|
h = e.middleware[i](h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
// Execute chain
|
2015-04-19 01:47:48 +02:00
|
|
|
if err := h(c); err != nil {
|
|
|
|
e.httpErrorHandler(err, c)
|
|
|
|
}
|
|
|
|
|
2015-04-09 07:23:47 +02:00
|
|
|
e.pool.Put(c)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
log.Fatal(http.ListenAndServe(addr, e))
|
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) {
|
|
|
|
log.Fatal(http.ListenAndServeTLS(addr, certFile, keyFile, e))
|
|
|
|
}
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
// RunServer runs a custom server.
|
2015-04-03 14:24:47 +02:00
|
|
|
func (e *Echo) RunServer(server *http.Server) {
|
|
|
|
server.Handler = e
|
|
|
|
log.Fatal(server.ListenAndServe())
|
|
|
|
}
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
// RunTLSServer runs a custom server with TLS configuration.
|
2015-04-03 14:24:47 +02:00
|
|
|
func (e *Echo) RunTLSServer(server *http.Server, certFile, keyFile string) {
|
|
|
|
server.Handler = e
|
|
|
|
log.Fatal(server.ListenAndServeTLS(certFile, keyFile))
|
|
|
|
}
|
|
|
|
|
2015-03-27 23:35:15 +02:00
|
|
|
// wraps Middleware
|
|
|
|
func wrapM(m Middleware) MiddlewareFunc {
|
|
|
|
switch m := m.(type) {
|
2015-03-30 08:35:08 +02:00
|
|
|
case func(*Context):
|
2015-04-19 06:46:00 +02:00
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
2015-04-19 01:47:48 +02:00
|
|
|
return func(c *Context) error {
|
2015-03-30 08:35:08 +02:00
|
|
|
m(c)
|
2015-04-19 01:47:48 +02:00
|
|
|
return h(c)
|
2015-04-19 06:46:00 +02:00
|
|
|
}
|
2015-04-19 01:47:48 +02:00
|
|
|
}
|
|
|
|
case func(*Context) error:
|
2015-04-19 06:46:00 +02:00
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
2015-04-19 01:47:48 +02:00
|
|
|
return func(c *Context) error {
|
2015-04-19 06:46:00 +02:00
|
|
|
if err := m(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-19 01:47:48 +02:00
|
|
|
return h(c)
|
2015-04-19 06:46:00 +02:00
|
|
|
}
|
2015-03-30 08:35:08 +02:00
|
|
|
}
|
2015-04-19 06:46:00 +02:00
|
|
|
case func(HandlerFunc) HandlerFunc:
|
|
|
|
return m
|
2015-04-21 08:17:21 +02:00
|
|
|
case func(http.Handler) http.Handler:
|
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
|
|
|
return func(c *Context) (err error) {
|
|
|
|
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c.Response.Writer = w
|
|
|
|
c.Request = r
|
|
|
|
err = h(c)
|
|
|
|
})).ServeHTTP(c.Response.Writer, c.Request)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 08:35:08 +02:00
|
|
|
case http.Handler, http.HandlerFunc:
|
2015-04-19 06:46:00 +02:00
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
2015-04-19 01:47:48 +02:00
|
|
|
return func(c *Context) error {
|
2015-04-21 08:17:21 +02:00
|
|
|
m.(http.Handler).ServeHTTP(c.Response.Writer, c.Request)
|
2015-04-19 01:47:48 +02:00
|
|
|
return h(c)
|
2015-04-19 06:46:00 +02:00
|
|
|
}
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
2015-03-30 08:35:08 +02:00
|
|
|
case func(http.ResponseWriter, *http.Request):
|
2015-04-19 06:46:00 +02:00
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
2015-04-19 01:47:48 +02:00
|
|
|
return func(c *Context) error {
|
2015-04-21 08:17:21 +02:00
|
|
|
m(c.Response.Writer, c.Request)
|
2015-04-19 01:47:48 +02:00
|
|
|
return h(c)
|
2015-04-19 06:46:00 +02:00
|
|
|
}
|
2015-04-19 01:47:48 +02:00
|
|
|
}
|
|
|
|
case func(http.ResponseWriter, *http.Request) error:
|
2015-04-19 06:46:00 +02:00
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
2015-04-19 01:47:48 +02:00
|
|
|
return func(c *Context) error {
|
2015-04-21 08:17:21 +02:00
|
|
|
if err := m(c.Response.Writer, c.Request); err != nil {
|
2015-04-19 06:46:00 +02:00
|
|
|
return err
|
|
|
|
}
|
2015-04-19 01:47:48 +02:00
|
|
|
return h(c)
|
2015-04-19 06:46:00 +02:00
|
|
|
}
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("echo: unknown middleware")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wraps Handler
|
|
|
|
func wrapH(h Handler) HandlerFunc {
|
|
|
|
switch h := h.(type) {
|
|
|
|
case func(*Context):
|
2015-04-19 01:47:48 +02:00
|
|
|
return func(c *Context) error {
|
|
|
|
h(c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case func(*Context) error:
|
2015-04-19 06:46:00 +02:00
|
|
|
return h
|
2015-03-30 08:35:08 +02:00
|
|
|
case http.Handler, http.HandlerFunc:
|
2015-04-19 01:47:48 +02:00
|
|
|
return func(c *Context) error {
|
2015-03-27 23:35:15 +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-04-19 01:47:48 +02:00
|
|
|
return func(c *Context) error {
|
2015-03-30 08:35:08 +02:00
|
|
|
h(c.Response, c.Request)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case func(http.ResponseWriter, *http.Request) error:
|
|
|
|
return func(c *Context) error {
|
|
|
|
return h(c.Response, c.Request)
|
2015-03-30 08:35:08 +02:00
|
|
|
}
|
2015-03-27 23:35:15 +02:00
|
|
|
default:
|
|
|
|
panic("echo: unknown handler")
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 06:46:00 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.SetOutput(colorable.NewColorableStdout())
|
|
|
|
}
|