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-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
|
|
|
|
|
|
|
"github.com/mattn/go-colorable"
|
2015-05-20 23:38:51 +02:00
|
|
|
"golang.org/x/net/websocket"
|
2015-05-22 08:11:27 +02:00
|
|
|
"github.com/bradfitz/http2"
|
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
|
2015-05-22 08:11:27 +02:00
|
|
|
http2 bool
|
2015-04-19 01:47:48 +02:00
|
|
|
maxParam byte
|
2015-05-18 20:33:11 +02:00
|
|
|
notFoundHandler HandlerFunc
|
2015-04-19 01:47:48 +02:00
|
|
|
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-05-18 07:54:29 +02:00
|
|
|
debug bool
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
2015-05-06 06:55:49 +02:00
|
|
|
HTTPError struct {
|
|
|
|
Code int
|
|
|
|
Message string
|
|
|
|
}
|
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-05-20 23:38:51 +02:00
|
|
|
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 {
|
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
|
|
|
|
//-------------
|
|
|
|
|
|
|
|
ApplicationJSON = "application/json"
|
|
|
|
ApplicationProtobuf = "application/protobuf"
|
2015-05-12 00:43:54 +02:00
|
|
|
ApplicationMsgpack = "application/msgpack"
|
2015-05-11 07:34:31 +02:00
|
|
|
TextPlain = "text/plain"
|
|
|
|
TextHTML = "text/html"
|
|
|
|
ApplicationForm = "application/x-www-form-urlencoded"
|
|
|
|
MultipartForm = "multipart/form-data"
|
|
|
|
|
|
|
|
//---------
|
|
|
|
// Headers
|
|
|
|
//---------
|
|
|
|
|
|
|
|
Accept = "Accept"
|
2015-05-15 21:29:14 +02:00
|
|
|
AcceptEncoding = "Accept-Encoding"
|
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-05-12 00:43:54 +02:00
|
|
|
Authorization = "Authorization"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
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-05-18 07:54:29 +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-05-22 07:48:34 +02:00
|
|
|
func NewHTTPError(code int, msg ...string) *HTTPError {
|
2015-05-21 17:55:22 +02:00
|
|
|
he := &HTTPError{Code: code, Message: http.StatusText(code)}
|
2015-05-22 07:48:34 +02:00
|
|
|
for _, m := range msg {
|
2015-05-21 17:55:22 +02:00
|
|
|
he.Message = m
|
2015-05-21 00:59:36 +02:00
|
|
|
}
|
|
|
|
return he
|
|
|
|
}
|
|
|
|
|
2015-05-20 23:38:51 +02:00
|
|
|
func (e *HTTPError) Error() string {
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
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-05-08 20:52:06 +02:00
|
|
|
return NewContext(nil, new(Response), e)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
2015-04-19 06:46:00 +02:00
|
|
|
|
|
|
|
//----------
|
|
|
|
// Defaults
|
|
|
|
//----------
|
2015-04-26 07:32:20 +02:00
|
|
|
|
2015-05-22 08:11:27 +02:00
|
|
|
e.HTTP2(true)
|
2015-05-20 03:54:31 +02:00
|
|
|
e.SetMaxParam(5)
|
2015-05-20 23:38:51 +02:00
|
|
|
e.notFoundHandler = func(c *Context) error {
|
2015-05-21 00:59:36 +02:00
|
|
|
return NewHTTPError(http.StatusNotFound)
|
2015-05-18 20:33:11 +02:00
|
|
|
}
|
2015-05-20 23:38:51 +02:00
|
|
|
e.SetHTTPErrorHandler(func(err error, c *Context) {
|
|
|
|
code := http.StatusInternalServerError
|
|
|
|
msg := http.StatusText(code)
|
|
|
|
if he, ok := err.(*HTTPError); ok {
|
|
|
|
code = he.Code
|
|
|
|
msg = he.Message
|
2015-05-20 03:54:31 +02:00
|
|
|
}
|
2015-05-20 23:38:51 +02:00
|
|
|
if e.Debug() {
|
|
|
|
msg = err.Error()
|
2015-05-06 06:55:49 +02:00
|
|
|
}
|
2015-05-20 23:38:51 +02:00
|
|
|
http.Error(c.Response, msg, code)
|
2015-04-22 07:53:15 +02:00
|
|
|
})
|
2015-05-20 23:38:51 +02:00
|
|
|
e.SetBinder(func(r *http.Request, v interface{}) error {
|
2015-05-11 07:34:31 +02:00
|
|
|
ct := r.Header.Get(ContentType)
|
2015-05-06 06:55:49 +02:00
|
|
|
err := UnsupportedMediaType
|
2015-05-11 07:34:31 +02:00
|
|
|
if strings.HasPrefix(ct, ApplicationJSON) {
|
2015-05-06 06:55:49 +02:00
|
|
|
err = json.NewDecoder(r.Body).Decode(v)
|
2015-05-11 07:34:31 +02:00
|
|
|
} else if strings.HasPrefix(ct, ApplicationForm) {
|
2015-05-06 06:55:49 +02:00
|
|
|
err = nil
|
2015-04-19 06:46:00 +02:00
|
|
|
}
|
2015-05-20 23:38:51 +02:00
|
|
|
return err
|
2015-04-22 07:53:15 +02:00
|
|
|
})
|
2015-03-27 23:35:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:09:30 +02:00
|
|
|
// Group creates a new sub router with prefix. It inherits all properties from
|
2015-04-07 22:43:52 +02:00
|
|
|
// 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
|
2015-04-27 00:17:46 +02:00
|
|
|
g.prefix = 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
|
|
|
|
2015-05-22 08:11:27 +02:00
|
|
|
// HTTP2 enables HTTP2 support.
|
|
|
|
func (e *Echo) HTTP2(on bool) {
|
|
|
|
e.http2 = on
|
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
// SetMaxParam sets the maximum number of path parameters allowed for the application.
|
2015-04-29 03:53:57 +02:00
|
|
|
// Default value is 5, good enough for many use cases.
|
2015-05-20 03:54:31 +02:00
|
|
|
func (e *Echo) SetMaxParam(n uint8) {
|
2015-03-30 08:35:08 +02:00
|
|
|
e.maxParam = n
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
// SetHTTPErrorHandler registers an Echo.HTTPErrorHandler.
|
|
|
|
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().
|
|
|
|
func (e *Echo) SetBinder(b BindFunc) {
|
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) {
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
wss.ServeHTTP(c.Response.writer, 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-04-24 16:44:30 +02:00
|
|
|
key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
|
2015-05-20 03:54:31 +02:00
|
|
|
e.uris[key] = path
|
|
|
|
e.Router.Add(method, e.prefix+path, wrapHandler(h), e)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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-05-20 23:38:51 +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-05-20 23:38:51 +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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
lp := len(params)
|
|
|
|
n := 0
|
|
|
|
key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
|
|
|
|
if path, ok := e.uris[key]; ok {
|
|
|
|
for i, l := 0, len(path); i < l; i++ {
|
|
|
|
if path[i] == ':' && n < lp {
|
|
|
|
for ; i < l && path[i] != '/'; i++ {
|
|
|
|
}
|
|
|
|
uri.WriteString(fmt.Sprintf("%v", params[n]))
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if i < l {
|
|
|
|
uri.WriteByte(path[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uri.String()
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
// URL is an alias for URI
|
|
|
|
func (e *Echo) URL(h Handler, params ...interface{}) string {
|
|
|
|
return e.URI(h, params...)
|
2015-05-14 00:20:09 +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)
|
2015-04-26 07:32:20 +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-04-07 22:02:23 +02:00
|
|
|
c.reset(w, r, e)
|
2015-05-18 07:54:29 +02:00
|
|
|
if h == nil {
|
2015-05-18 20:33:11 +02:00
|
|
|
h = e.notFoundHandler
|
|
|
|
}
|
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
|
|
|
|
if he := h(c); he != nil {
|
|
|
|
e.httpErrorHandler(he, 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) {
|
2015-05-22 08:11:27 +02:00
|
|
|
s := &http.Server{Addr: addr}
|
|
|
|
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-05-22 08:11:27 +02:00
|
|
|
s := &http.Server{Addr: addr}
|
|
|
|
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-05-22 08:11:27 +02:00
|
|
|
func (e *Echo) RunServer(srv *http.Server) {
|
|
|
|
e.run(srv)
|
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-05-22 08:11:27 +02:00
|
|
|
func (e *Echo) RunTLSServer(srv *http.Server, certFile, keyFile string) {
|
|
|
|
e.run(srv, certFile, keyFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Echo) run(s *http.Server, f ...string) {
|
|
|
|
s.Handler = e
|
|
|
|
if e.http2 {
|
|
|
|
http2.ConfigureServer(s, nil)
|
|
|
|
}
|
|
|
|
if len(f) == 0 {
|
|
|
|
log.Fatal(s.ListenAndServe())
|
|
|
|
} else if len(f) == 2 {
|
|
|
|
log.Fatal(s.ListenAndServeTLS(f[0], f[1]))
|
|
|
|
} else {
|
|
|
|
log.Fatal("echo: invalid TLS configuration")
|
|
|
|
}
|
2015-04-03 14:24:47 +02:00
|
|
|
}
|
|
|
|
|
2015-05-12 05:41:28 +02:00
|
|
|
// wraps middleware
|
|
|
|
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-20 23:38:51 +02:00
|
|
|
c.Response.writer = w
|
2015-04-21 08:17:21 +02:00
|
|
|
c.Request = r
|
2015-05-20 23:38:51 +02:00
|
|
|
err = h(c)
|
|
|
|
})).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)
|
|
|
|
case http.HandlerFunc:
|
|
|
|
return wrapHTTPHandlerFuncMW(m)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wraps HandlerFunc middleware
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wraps http.HandlerFunc middleware
|
|
|
|
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-12 05:41:28 +02:00
|
|
|
if !c.Response.committed {
|
2015-05-20 23:38:51 +02:00
|
|
|
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-05-12 05:41:28 +02:00
|
|
|
// wraps handler
|
|
|
|
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-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-05-20 23:38:51 +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
|
|
|
|
}
|
2015-03-27 23:35:15 +02:00
|
|
|
default:
|
2015-05-18 07:54:29 +02:00
|
|
|
panic("echo ⇒ unknown handler")
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-19 06:46:00 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.SetOutput(colorable.NewColorableStdout())
|
|
|
|
}
|