2015-03-27 23:35:15 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
2015-04-03 05:18:34 +02:00
|
|
|
"errors"
|
2015-03-27 23:35:15 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Echo struct {
|
|
|
|
Router *router
|
2015-04-02 14:02:52 +02:00
|
|
|
prefix string
|
2015-03-27 23:35:15 +02:00
|
|
|
middleware []MiddlewareFunc
|
|
|
|
maxParam byte
|
|
|
|
notFoundHandler HandlerFunc
|
|
|
|
methodNotAllowedHandler HandlerFunc
|
|
|
|
internalServerErrorHandler HandlerFunc
|
|
|
|
pool sync.Pool
|
|
|
|
}
|
|
|
|
Middleware interface{}
|
|
|
|
MiddlewareFunc func(HandlerFunc) HandlerFunc
|
2015-04-02 14:02:52 +02:00
|
|
|
Handler interface{}
|
|
|
|
HandlerFunc func(*Context)
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-04-01 17:05:54 +02:00
|
|
|
MethodCONNECT = "CONNECT"
|
|
|
|
MethodDELETE = "DELETE"
|
|
|
|
MethodGET = "GET"
|
|
|
|
MethodHEAD = "HEAD"
|
|
|
|
MethodOPTIONS = "OPTIONS"
|
|
|
|
MethodPATCH = "PATCH"
|
|
|
|
MethodPOST = "POST"
|
|
|
|
MethodPUT = "PUT"
|
|
|
|
MethodTRACE = "TRACE"
|
|
|
|
|
2015-03-27 23:35:15 +02:00
|
|
|
MIMEJSON = "application/json"
|
2015-03-30 08:35:08 +02:00
|
|
|
MIMEText = "text/plain"
|
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-01 17:05:54 +02:00
|
|
|
MethodCONNECT,
|
|
|
|
MethodDELETE,
|
|
|
|
MethodGET,
|
|
|
|
MethodHEAD,
|
|
|
|
MethodOPTIONS,
|
|
|
|
MethodPATCH,
|
|
|
|
MethodPOST,
|
|
|
|
MethodPUT,
|
|
|
|
MethodTRACE,
|
|
|
|
}
|
2015-04-03 05:18:34 +02:00
|
|
|
|
|
|
|
// Errors
|
|
|
|
ErrUnsupportedContentType = errors.New("echo: unsupported content type")
|
|
|
|
ErrBindJSON = errors.New("echo: bind json error")
|
|
|
|
ErrRenderJSON = errors.New("echo: render json error")
|
2015-04-01 17:05:54 +02:00
|
|
|
)
|
|
|
|
|
2015-03-27 23:35:15 +02:00
|
|
|
// New creates a echo instance.
|
2015-03-30 08:35:08 +02:00
|
|
|
func New() (e *Echo) {
|
|
|
|
e = &Echo{
|
2015-03-27 23:35:15 +02:00
|
|
|
maxParam: 5,
|
|
|
|
notFoundHandler: func(c *Context) {
|
|
|
|
http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
|
|
|
},
|
|
|
|
methodNotAllowedHandler: func(c *Context) {
|
|
|
|
http.Error(c.Response, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
},
|
|
|
|
internalServerErrorHandler: func(c *Context) {
|
|
|
|
http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
},
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOP
|
2015-04-02 14:02:52 +02:00
|
|
|
func (h HandlerFunc) ServeHTTP(http.ResponseWriter, *http.Request) {
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-02 23:41:36 +02:00
|
|
|
// Sub creates a new sub router. It inherits all properties from the parent
|
|
|
|
// router, including middleware.
|
2015-04-02 14:02:52 +02:00
|
|
|
func (e *Echo) Sub(pfx string) *Echo {
|
|
|
|
s := *e
|
|
|
|
s.prefix = pfx
|
2015-04-02 23:41:36 +02:00
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2015-04-03 19:46:37 +02:00
|
|
|
// Group is similar to Sub but excludes inheriting middleware from the parent
|
2015-04-02 23:41:36 +02:00
|
|
|
// router.
|
|
|
|
func (e *Echo) Group(pfx string) *Echo {
|
2015-04-03 00:15:09 +02:00
|
|
|
g := *e
|
|
|
|
g.prefix = pfx
|
|
|
|
g.middleware = nil
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// NotFoundHandler sets 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
|
|
|
}
|
|
|
|
|
|
|
|
// MethodNotAllowedHandler sets a custom MethodNotAllowed handler.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) MethodNotAllowedHandler(h Handler) {
|
|
|
|
e.methodNotAllowedHandler = wrapH(h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// InternalServerErrorHandler sets a custom InternalServerError handler.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) InternalServerErrorHandler(h Handler) {
|
|
|
|
e.internalServerErrorHandler = wrapH(h)
|
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-02 14:02:52 +02:00
|
|
|
e.add(MethodCONNECT, 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-02 14:02:52 +02:00
|
|
|
e.add(MethodDELETE, 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-02 14:02:52 +02:00
|
|
|
e.add(MethodGET, 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-02 14:02:52 +02:00
|
|
|
e.add(MethodHEAD, 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-02 14:02:52 +02:00
|
|
|
e.add(MethodOPTIONS, 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-02 14:02:52 +02:00
|
|
|
e.add(MethodPATCH, 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-02 14:02:52 +02:00
|
|
|
e.add(MethodPOST, 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-02 14:02:52 +02:00
|
|
|
e.add(MethodPUT, 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-02 14:02:52 +02:00
|
|
|
e.add(MethodTRACE, path, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Echo) add(method, path string, h Handler) {
|
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-03-30 08:35:08 +02:00
|
|
|
e.Get(path+"/*", func(c *Context) {
|
2015-03-27 23:35:15 +02:00
|
|
|
fs.ServeHTTP(c.Response, c.Request)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeFile serves a file.
|
2015-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) ServeFile(path, file string) {
|
|
|
|
e.Get(path, func(c *Context) {
|
2015-03-27 23:35:15 +02:00
|
|
|
http.ServeFile(c.Response, c.Request, file)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-03-30 08:35:08 +02:00
|
|
|
func (e *Echo) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
2015-04-03 00:15:09 +02:00
|
|
|
h, c, echo := e.Router.Find(r.Method, r.URL.Path)
|
|
|
|
if echo != nil {
|
|
|
|
e = echo
|
|
|
|
}
|
2015-04-02 14:02:52 +02:00
|
|
|
if h == nil {
|
|
|
|
h = e.notFoundHandler
|
|
|
|
}
|
|
|
|
c.reset(rw, r, e)
|
|
|
|
// Middleware
|
|
|
|
for i := len(e.middleware) - 1; i >= 0; i-- {
|
|
|
|
h = e.middleware[i](h)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
2015-04-02 14:02:52 +02:00
|
|
|
// Handler
|
|
|
|
h(c)
|
2015-03-30 08:35:08 +02:00
|
|
|
e.pool.Put(c)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 14:24:47 +02:00
|
|
|
// Run 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-03 14:24:47 +02:00
|
|
|
// RunTLS a server
|
|
|
|
func (e *Echo) RunTLS(addr, certFile, keyFile string) {
|
|
|
|
log.Fatal(http.ListenAndServeTLS(addr, certFile, keyFile, e))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunServer runs a custom server
|
|
|
|
func (e *Echo) RunServer(server *http.Server) {
|
|
|
|
server.Handler = e
|
|
|
|
log.Fatal(server.ListenAndServe())
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunTLSServer runs a custom server with TLS configuration
|
|
|
|
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):
|
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
|
|
|
return func(c *Context) {
|
|
|
|
m(c)
|
|
|
|
h(c)
|
|
|
|
}
|
|
|
|
}
|
2015-03-27 23:35:15 +02:00
|
|
|
case func(HandlerFunc) HandlerFunc:
|
|
|
|
return MiddlewareFunc(m)
|
2015-03-30 08:35:08 +02:00
|
|
|
case func(http.Handler) http.Handler:
|
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
|
|
|
return func(c *Context) {
|
|
|
|
m(h).ServeHTTP(c.Response, c.Request)
|
|
|
|
h(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case http.Handler, http.HandlerFunc:
|
2015-03-27 23:35:15 +02:00
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
|
|
|
return func(c *Context) {
|
|
|
|
m.(http.Handler).ServeHTTP(c.Response, c.Request)
|
|
|
|
h(c)
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 08:35:08 +02:00
|
|
|
case func(http.ResponseWriter, *http.Request):
|
2015-03-27 23:35:15 +02:00
|
|
|
return func(h HandlerFunc) HandlerFunc {
|
|
|
|
return func(c *Context) {
|
2015-03-30 08:35:08 +02:00
|
|
|
m(c.Response, c.Request)
|
2015-03-27 23:35:15 +02:00
|
|
|
h(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("echo: unknown middleware")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wraps Handler
|
|
|
|
func wrapH(h Handler) HandlerFunc {
|
|
|
|
switch h := h.(type) {
|
|
|
|
case func(*Context):
|
|
|
|
return HandlerFunc(h)
|
2015-03-30 08:35:08 +02:00
|
|
|
case http.Handler, http.HandlerFunc:
|
2015-03-27 23:35:15 +02:00
|
|
|
return func(c *Context) {
|
|
|
|
h.(http.Handler).ServeHTTP(c.Response, c.Request)
|
|
|
|
}
|
2015-03-30 08:35:08 +02:00
|
|
|
case func(http.ResponseWriter, *http.Request):
|
|
|
|
return func(c *Context) {
|
|
|
|
h(c.Response, c.Request)
|
|
|
|
}
|
2015-03-27 23:35:15 +02:00
|
|
|
default:
|
|
|
|
panic("echo: unknown handler")
|
|
|
|
}
|
|
|
|
}
|