2016-03-20 00:47:20 +02:00
|
|
|
/*
|
2016-04-13 22:28:13 +02:00
|
|
|
Package echo implements a fast and unfancy HTTP server framework for Go (Golang).
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/echo/engine/standard"
|
|
|
|
"github.com/labstack/echo/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler
|
2016-04-06 00:51:15 +02:00
|
|
|
func hello(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "Hello, World!")
|
2016-03-20 00:47:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Echo instance
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
e.Use(middleware.Logger())
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
|
|
|
|
// Routes
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/", hello)
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
// Start server
|
2016-10-21 01:57:31 +02:00
|
|
|
if err := e.Start(":1323"); err != nil {
|
2016-10-23 22:37:04 +02:00
|
|
|
e.Logger.Fatal(err)
|
2016-10-21 01:57:31 +02:00
|
|
|
}
|
2016-03-20 00:47:20 +02:00
|
|
|
}
|
|
|
|
|
2016-05-24 23:51:26 +02:00
|
|
|
Learn more at https://echo.labstack.com
|
2016-03-20 00:47:20 +02:00
|
|
|
*/
|
2015-03-27 23:35:15 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
2015-04-22 07:12:41 +02:00
|
|
|
"bytes"
|
2016-09-23 07:53:44 +02:00
|
|
|
"crypto/tls"
|
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"
|
2016-09-25 20:56:51 +02:00
|
|
|
slog "log"
|
2015-03-27 23:35:15 +02:00
|
|
|
"net/http"
|
2016-04-11 05:56:10 +02:00
|
|
|
"path"
|
2015-04-24 16:44:30 +02:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2015-03-27 23:35:15 +02:00
|
|
|
"sync"
|
2016-09-23 07:53:44 +02:00
|
|
|
"time"
|
2015-04-19 06:46:00 +02:00
|
|
|
|
2016-09-28 19:33:58 +02:00
|
|
|
"github.com/rsc/letsencrypt"
|
2016-09-26 04:48:15 +02:00
|
|
|
|
2016-09-25 20:41:08 +02:00
|
|
|
"github.com/labstack/gommon/color"
|
2016-06-01 03:29:11 +02:00
|
|
|
glog "github.com/labstack/gommon/log"
|
2016-09-25 01:19:38 +02:00
|
|
|
"github.com/tylerb/graceful"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-03-20 00:47:20 +02:00
|
|
|
// Echo is the top-level framework instance.
|
2015-03-27 23:35:15 +02:00
|
|
|
Echo struct {
|
2016-09-25 01:19:38 +02:00
|
|
|
Server *http.Server
|
2016-09-25 02:19:50 +02:00
|
|
|
TLSServer *http.Server
|
2016-09-25 07:01:38 +02:00
|
|
|
TLSConfig *tls.Config
|
2016-09-26 04:48:15 +02:00
|
|
|
TLSCacheFile string
|
|
|
|
TLSHosts []string
|
2016-09-25 01:19:38 +02:00
|
|
|
ShutdownTimeout time.Duration
|
|
|
|
DisableHTTP2 bool
|
|
|
|
Debug bool
|
2016-09-23 07:53:44 +02:00
|
|
|
HTTPErrorHandler
|
|
|
|
Binder Binder
|
|
|
|
Renderer Renderer
|
2016-09-25 21:14:50 +02:00
|
|
|
Logger Logger
|
2016-09-26 04:48:15 +02:00
|
|
|
tlsManager letsencrypt.Manager
|
2016-09-25 01:19:38 +02:00
|
|
|
graceful *graceful.Server
|
2016-09-25 02:19:50 +02:00
|
|
|
gracefulTLS *graceful.Server
|
2016-09-23 07:53:44 +02:00
|
|
|
premiddleware []MiddlewareFunc
|
|
|
|
middleware []MiddlewareFunc
|
|
|
|
maxParam *int
|
2016-09-26 04:48:15 +02:00
|
|
|
router *Router
|
2016-09-23 07:53:44 +02:00
|
|
|
notFoundHandler HandlerFunc
|
|
|
|
pool sync.Pool
|
2016-09-26 04:48:15 +02:00
|
|
|
color *color.Color
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Route contains a handler and information for matching against requests.
|
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
|
|
|
|
2016-04-16 18:15:37 +02:00
|
|
|
// HTTPError represents an error that occurred while handling a request.
|
2015-05-06 06:55:49 +02:00
|
|
|
HTTPError struct {
|
2016-03-11 17:53:54 +02:00
|
|
|
Code int
|
|
|
|
Message string
|
2015-05-06 06:55:49 +02:00
|
|
|
}
|
2015-05-23 05:26:52 +02:00
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
// MiddlewareFunc defines a function to process middleware.
|
|
|
|
MiddlewareFunc func(HandlerFunc) HandlerFunc
|
2016-02-09 03:26:00 +02:00
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
// HandlerFunc defines a function to server HTTP requests.
|
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
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Validator is the interface that wraps the Validate function.
|
2015-08-01 04:25:03 +02:00
|
|
|
Validator interface {
|
|
|
|
Validate() error
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Renderer is the interface that wraps the Render function.
|
2015-04-15 19:10:05 +02:00
|
|
|
Renderer interface {
|
2016-03-06 06:03:11 +02:00
|
|
|
Render(io.Writer, string, interface{}, Context) error
|
2015-04-09 23:59:31 +02:00
|
|
|
}
|
2016-10-14 01:45:27 +02:00
|
|
|
|
|
|
|
// Map defines a generic map of type `map[string]interface{}`.
|
|
|
|
Map map[string]interface{}
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// HTTP methods
|
2015-03-27 23:35:15 +02:00
|
|
|
const (
|
2015-04-06 06:49:55 +02:00
|
|
|
CONNECT = "CONNECT"
|
2016-03-20 00:47:20 +02:00
|
|
|
DELETE = "DELETE"
|
|
|
|
GET = "GET"
|
|
|
|
HEAD = "HEAD"
|
2015-04-06 06:49:55 +02:00
|
|
|
OPTIONS = "OPTIONS"
|
2016-03-20 00:47:20 +02:00
|
|
|
PATCH = "PATCH"
|
|
|
|
POST = "POST"
|
|
|
|
PUT = "PUT"
|
|
|
|
TRACE = "TRACE"
|
|
|
|
)
|
2015-05-11 07:34:31 +02:00
|
|
|
|
2016-04-06 16:28:53 +02:00
|
|
|
// MIME types
|
2016-03-20 00:47:20 +02:00
|
|
|
const (
|
2016-04-06 16:28:53 +02:00
|
|
|
MIMEApplicationJSON = "application/json"
|
|
|
|
MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8
|
|
|
|
MIMEApplicationJavaScript = "application/javascript"
|
|
|
|
MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
|
|
|
|
MIMEApplicationXML = "application/xml"
|
|
|
|
MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8
|
|
|
|
MIMEApplicationForm = "application/x-www-form-urlencoded"
|
|
|
|
MIMEApplicationProtobuf = "application/protobuf"
|
|
|
|
MIMEApplicationMsgpack = "application/msgpack"
|
|
|
|
MIMETextHTML = "text/html"
|
|
|
|
MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8
|
|
|
|
MIMETextPlain = "text/plain"
|
|
|
|
MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8
|
|
|
|
MIMEMultipartForm = "multipart/form-data"
|
|
|
|
MIMEOctetStream = "application/octet-stream"
|
2016-03-20 00:47:20 +02:00
|
|
|
)
|
2015-05-11 07:34:31 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
const (
|
2016-04-06 16:28:53 +02:00
|
|
|
charsetUTF8 = "charset=utf-8"
|
2016-03-20 00:47:20 +02:00
|
|
|
)
|
2015-07-21 04:24:33 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Headers
|
|
|
|
const (
|
2016-04-08 01:16:58 +02:00
|
|
|
HeaderAcceptEncoding = "Accept-Encoding"
|
2016-06-02 22:40:21 +02:00
|
|
|
HeaderAllow = "Allow"
|
2016-04-08 01:16:58 +02:00
|
|
|
HeaderAuthorization = "Authorization"
|
|
|
|
HeaderContentDisposition = "Content-Disposition"
|
|
|
|
HeaderContentEncoding = "Content-Encoding"
|
|
|
|
HeaderContentLength = "Content-Length"
|
|
|
|
HeaderContentType = "Content-Type"
|
2016-05-03 01:19:35 +02:00
|
|
|
HeaderCookie = "Cookie"
|
|
|
|
HeaderSetCookie = "Set-Cookie"
|
2016-04-08 01:16:58 +02:00
|
|
|
HeaderIfModifiedSince = "If-Modified-Since"
|
|
|
|
HeaderLastModified = "Last-Modified"
|
|
|
|
HeaderLocation = "Location"
|
|
|
|
HeaderUpgrade = "Upgrade"
|
|
|
|
HeaderVary = "Vary"
|
|
|
|
HeaderWWWAuthenticate = "WWW-Authenticate"
|
2016-05-03 17:32:28 +02:00
|
|
|
HeaderXForwardedProto = "X-Forwarded-Proto"
|
2016-04-28 06:08:06 +02:00
|
|
|
HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
|
2016-04-08 01:16:58 +02:00
|
|
|
HeaderXForwardedFor = "X-Forwarded-For"
|
|
|
|
HeaderXRealIP = "X-Real-IP"
|
2016-04-14 21:26:54 +02:00
|
|
|
HeaderServer = "Server"
|
2016-04-08 01:16:58 +02:00
|
|
|
HeaderOrigin = "Origin"
|
|
|
|
HeaderAccessControlRequestMethod = "Access-Control-Request-Method"
|
|
|
|
HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers"
|
|
|
|
HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin"
|
|
|
|
HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods"
|
|
|
|
HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers"
|
|
|
|
HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
|
|
|
|
HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers"
|
|
|
|
HeaderAccessControlMaxAge = "Access-Control-Max-Age"
|
2016-05-03 07:41:07 +02:00
|
|
|
|
|
|
|
// Security
|
|
|
|
HeaderStrictTransportSecurity = "Strict-Transport-Security"
|
|
|
|
HeaderXContentTypeOptions = "X-Content-Type-Options"
|
|
|
|
HeaderXXSSProtection = "X-XSS-Protection"
|
|
|
|
HeaderXFrameOptions = "X-Frame-Options"
|
|
|
|
HeaderContentSecurityPolicy = "Content-Security-Policy"
|
2016-05-13 02:45:00 +02:00
|
|
|
HeaderXCSRFToken = "X-CSRF-Token"
|
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,
|
|
|
|
}
|
2016-03-20 00:47:20 +02:00
|
|
|
)
|
2015-08-26 05:36:15 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Errors
|
|
|
|
var (
|
2016-05-01 05:08:06 +02:00
|
|
|
ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType)
|
|
|
|
ErrNotFound = NewHTTPError(http.StatusNotFound)
|
|
|
|
ErrUnauthorized = NewHTTPError(http.StatusUnauthorized)
|
|
|
|
ErrMethodNotAllowed = NewHTTPError(http.StatusMethodNotAllowed)
|
|
|
|
ErrStatusRequestEntityTooLarge = NewHTTPError(http.StatusRequestEntityTooLarge)
|
|
|
|
ErrRendererNotRegistered = errors.New("renderer not registered")
|
|
|
|
ErrInvalidRedirectCode = errors.New("invalid redirect status code")
|
2016-05-03 07:41:07 +02:00
|
|
|
ErrCookieNotFound = errors.New("cookie not found")
|
2016-03-20 00:47:20 +02:00
|
|
|
)
|
2015-07-27 17:43:11 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Error handlers
|
|
|
|
var (
|
2016-07-05 17:43:46 +02:00
|
|
|
NotFoundHandler = func(c Context) error {
|
2016-03-12 20:18:40 +02:00
|
|
|
return ErrNotFound
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2015-07-24 21:03:36 +02:00
|
|
|
|
2016-07-05 17:43:46 +02:00
|
|
|
MethodNotAllowedHandler = func(c Context) error {
|
2016-03-12 20:18:40 +02:00
|
|
|
return ErrMethodNotAllowed
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2015-04-01 17:05:54 +02:00
|
|
|
)
|
|
|
|
|
2015-07-08 02:34:59 +02:00
|
|
|
// New creates an instance of Echo.
|
2016-09-26 04:48:15 +02:00
|
|
|
|
2015-03-30 08:35:08 +02:00
|
|
|
func New() (e *Echo) {
|
2016-09-23 17:29:16 +02:00
|
|
|
e = &Echo{
|
2016-09-25 07:36:53 +02:00
|
|
|
Server: new(http.Server),
|
|
|
|
TLSServer: new(http.Server),
|
|
|
|
// TODO: https://github.com/golang/go/commit/d24f446a90ea94b87591bf16228d7d871fec3d92
|
2016-09-25 07:01:38 +02:00
|
|
|
TLSConfig: new(tls.Config),
|
2016-09-26 04:48:15 +02:00
|
|
|
TLSCacheFile: "letsencrypt.cache",
|
2016-09-25 02:19:50 +02:00
|
|
|
ShutdownTimeout: 15 * time.Second,
|
2016-09-25 20:56:51 +02:00
|
|
|
Logger: glog.New("echo"),
|
2016-09-25 21:14:50 +02:00
|
|
|
graceful: new(graceful.Server),
|
2016-09-25 02:19:50 +02:00
|
|
|
gracefulTLS: new(graceful.Server),
|
2016-09-25 21:14:50 +02:00
|
|
|
maxParam: new(int),
|
2016-09-26 04:48:15 +02:00
|
|
|
color: color.New(),
|
2016-09-25 01:19:38 +02:00
|
|
|
}
|
2016-09-25 21:14:50 +02:00
|
|
|
e.HTTPErrorHandler = e.DefaultHTTPErrorHandler
|
|
|
|
e.Binder = &binder{}
|
2016-09-25 20:56:51 +02:00
|
|
|
e.Logger.SetLevel(glog.OFF)
|
2015-03-30 08:35:08 +02:00
|
|
|
e.pool.New = func() interface{} {
|
2016-04-17 00:53:27 +02:00
|
|
|
return e.NewContext(nil, nil)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
2015-06-04 01:19:03 +02:00
|
|
|
e.router = NewRouter(e)
|
2015-03-27 23:35:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-17 00:53:27 +02:00
|
|
|
// NewContext returns a Context instance.
|
2016-09-23 07:53:44 +02:00
|
|
|
func (e *Echo) NewContext(r *http.Request, w http.ResponseWriter) Context {
|
2016-10-11 02:31:26 +02:00
|
|
|
return &context{
|
2016-09-23 07:53:44 +02:00
|
|
|
request: r,
|
|
|
|
response: NewResponse(w, e),
|
2016-10-14 01:45:27 +02:00
|
|
|
store: make(Map),
|
2016-04-17 00:53:27 +02:00
|
|
|
echo: e,
|
|
|
|
pvalues: make([]string, *e.maxParam),
|
2016-07-05 17:43:46 +02:00
|
|
|
handler: NotFoundHandler,
|
2016-04-17 00:53:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 05:26:52 +02:00
|
|
|
// Router returns router.
|
|
|
|
func (e *Echo) Router() *Router {
|
|
|
|
return e.router
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-03-11 17:53:54 +02:00
|
|
|
code = he.Code
|
|
|
|
msg = he.Message
|
2016-02-09 08:17:20 +02:00
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
if e.Debug {
|
2016-02-09 08:17:20 +02:00
|
|
|
msg = err.Error()
|
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
if !c.Response().Committed {
|
|
|
|
if c.Request().Method == HEAD { // Issue #608
|
2016-08-02 22:11:12 +02:00
|
|
|
c.NoContent(code)
|
|
|
|
} else {
|
|
|
|
c.String(code, msg)
|
|
|
|
}
|
2016-02-09 08:17:20 +02:00
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
e.Logger.Error(err)
|
2015-05-18 07:54:29 +02:00
|
|
|
}
|
|
|
|
|
2016-03-17 01:15:40 +02:00
|
|
|
// Pre adds middleware to the chain which is run before router.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Pre(middleware ...MiddlewareFunc) {
|
2016-04-09 23:00:23 +02:00
|
|
|
e.premiddleware = append(e.premiddleware, middleware...)
|
2016-03-16 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
2016-03-17 01:15:40 +02:00
|
|
|
// Use adds middleware to the chain which is run after router.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Use(middleware ...MiddlewareFunc) {
|
2016-02-16 03:12:15 +02:00
|
|
|
e.middleware = append(e.middleware, middleware...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2016-04-19 01:59:58 +02:00
|
|
|
// CONNECT registers a new CONNECT route for a path with matching handler in the
|
2016-03-20 00:47:20 +02:00
|
|
|
// router with optional route-level middleware.
|
2016-04-19 01:59:58 +02:00
|
|
|
func (e *Echo) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
e.add(CONNECT, path, h, m...)
|
|
|
|
}
|
|
|
|
|
2016-04-20 16:32:51 +02:00
|
|
|
// DELETE registers a new DELETE route for a path with matching handler in the router
|
2016-03-20 00:47:20 +02:00
|
|
|
// with optional route-level middleware.
|
2016-04-20 16:32:51 +02:00
|
|
|
func (e *Echo) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
e.add(DELETE, path, h, m...)
|
|
|
|
}
|
|
|
|
|
2016-04-19 01:59:58 +02:00
|
|
|
// GET registers a new GET route for a path with matching handler in the router
|
2016-03-20 00:47:20 +02:00
|
|
|
// with optional route-level middleware.
|
2016-04-19 01:59:58 +02:00
|
|
|
func (e *Echo) GET(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
e.add(GET, path, h, m...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HEAD registers a new HEAD route for a path with matching handler in the
|
2016-03-20 00:47:20 +02:00
|
|
|
// router with optional route-level middleware.
|
2016-04-19 01:59:58 +02:00
|
|
|
func (e *Echo) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
e.add(HEAD, path, h, m...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OPTIONS registers a new OPTIONS route for a path with matching handler in the
|
2016-03-20 00:47:20 +02:00
|
|
|
// router with optional route-level middleware.
|
2016-04-19 01:59:58 +02:00
|
|
|
func (e *Echo) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
e.add(OPTIONS, path, h, m...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH registers a new PATCH route for a path with matching handler in the
|
2016-03-20 00:47:20 +02:00
|
|
|
// router with optional route-level middleware.
|
2016-04-19 01:59:58 +02:00
|
|
|
func (e *Echo) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
e.add(PATCH, path, h, m...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST registers a new POST route for a path with matching handler in the
|
2016-03-20 00:47:20 +02:00
|
|
|
// router with optional route-level middleware.
|
2016-04-19 01:59:58 +02:00
|
|
|
func (e *Echo) POST(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
e.add(POST, path, h, m...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT registers a new PUT route for a path with matching handler in the
|
2016-03-20 00:47:20 +02:00
|
|
|
// router with optional route-level middleware.
|
2016-04-19 01:59:58 +02:00
|
|
|
func (e *Echo) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
e.add(PUT, path, h, m...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TRACE registers a new TRACE route for a path with matching handler in the
|
2016-03-20 00:47:20 +02:00
|
|
|
// router with optional route-level middleware.
|
2016-04-19 01:59:58 +02:00
|
|
|
func (e *Echo) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) {
|
|
|
|
e.add(TRACE, path, h, m...)
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Any registers a new route for all HTTP methods and path with matching handler
|
|
|
|
// in the router with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
|
2015-08-26 05:36:15 +02:00
|
|
|
for _, m := range methods {
|
2016-02-15 18:11:29 +02:00
|
|
|
e.add(m, path, handler, middleware...)
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Match registers a new route for multiple HTTP methods and path with matching
|
|
|
|
// handler in the router with optional route-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Match(methods []string, path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
|
2015-08-26 05:36:15 +02:00
|
|
|
for _, m := range methods {
|
2016-02-15 18:11:29 +02:00
|
|
|
e.add(m, path, handler, middleware...)
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:41:24 +02:00
|
|
|
// Static registers a new route with path prefix to serve static files from the
|
|
|
|
// provided root directory.
|
2016-04-11 05:56:10 +02:00
|
|
|
func (e *Echo) Static(prefix, root string) {
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET(prefix+"*", func(c Context) error {
|
2016-10-11 03:36:48 +02:00
|
|
|
return c.File(path.Join(root, c.Param("*")))
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2016-03-12 15:14:15 +02:00
|
|
|
}
|
|
|
|
|
2016-04-11 06:41:24 +02:00
|
|
|
// File registers a new route with path to serve a static file.
|
2016-03-12 15:14:15 +02:00
|
|
|
func (e *Echo) File(path, file string) {
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET(path, func(c Context) error {
|
2016-03-12 15:14:15 +02:00
|
|
|
return c.File(file)
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2016-03-12 15:14:15 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
|
2016-02-15 18:11:29 +02:00
|
|
|
name := handlerName(handler)
|
2016-04-02 23:19:39 +02:00
|
|
|
e.router.Add(method, path, func(c Context) error {
|
2016-03-15 17:33:57 +02:00
|
|
|
h := handler
|
2016-03-17 05:15:38 +02:00
|
|
|
// Chain middleware
|
|
|
|
for i := len(middleware) - 1; i >= 0; i-- {
|
2016-04-02 23:19:39 +02:00
|
|
|
h = middleware[i](h)
|
2016-02-15 18:11:29 +02:00
|
|
|
}
|
2016-04-02 23:19:39 +02:00
|
|
|
return h(c)
|
2016-10-22 05:36:49 +02:00
|
|
|
})
|
2015-06-01 09:07:53 +02:00
|
|
|
r := Route{
|
|
|
|
Method: method,
|
|
|
|
Path: path,
|
2016-02-15 18:11:29 +02:00
|
|
|
Handler: name,
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
2016-05-22 17:45:36 +02:00
|
|
|
e.router.routes[method+path] = r
|
2015-04-22 07:12:41 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Group creates a new router group with prefix and optional group-level middleware.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) Group(prefix string, m ...MiddlewareFunc) (g *Group) {
|
2016-02-15 18:11:29 +02:00
|
|
|
g = &Group{prefix: prefix, echo: e}
|
2016-02-16 03:12:15 +02:00
|
|
|
g.Use(m...)
|
2015-11-24 06:33:13 +02:00
|
|
|
return
|
2015-05-27 23:07:52 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 03:54:31 +02:00
|
|
|
// URI generates a URI from handler.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) URI(handler HandlerFunc, params ...interface{}) string {
|
2015-05-20 03:54:31 +02:00
|
|
|
uri := new(bytes.Buffer)
|
2016-02-15 18:11:29 +02:00
|
|
|
ln := len(params)
|
2015-05-20 03:54:31 +02:00
|
|
|
n := 0
|
2016-02-15 18:11:29 +02:00
|
|
|
name := handlerName(handler)
|
2015-06-01 09:07:53 +02:00
|
|
|
for _, r := range e.router.routes {
|
2016-02-15 18:11:29 +02:00
|
|
|
if r.Handler == name {
|
2015-06-01 09:07:53 +02:00
|
|
|
for i, l := 0, len(r.Path); i < l; i++ {
|
2016-02-15 18:11:29 +02:00
|
|
|
if r.Path[i] == ':' && n < ln {
|
2015-06-01 09:07:53 +02:00
|
|
|
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.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) URL(h HandlerFunc, params ...interface{}) string {
|
2016-02-16 03:12:15 +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 {
|
2016-05-22 17:45:36 +02:00
|
|
|
routes := []Route{}
|
|
|
|
for _, v := range e.router.routes {
|
|
|
|
routes = append(routes, v)
|
|
|
|
}
|
|
|
|
return routes
|
2015-05-23 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 02:23:31 +02:00
|
|
|
// AcquireContext returns an empty `Context` instance from the pool.
|
|
|
|
// You must be return the context by calling `ReleaseContext()`.
|
|
|
|
func (e *Echo) AcquireContext() Context {
|
|
|
|
return e.pool.Get().(Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseContext returns the `Context` instance back to the pool.
|
|
|
|
// You must call it after `AcquireContext()`.
|
|
|
|
func (e *Echo) ReleaseContext(c Context) {
|
2016-03-18 06:49:06 +02:00
|
|
|
e.pool.Put(c)
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
|
|
|
|
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2016-10-11 02:31:26 +02:00
|
|
|
c := e.pool.Get().(*context)
|
2016-09-23 07:53:44 +02:00
|
|
|
c.Reset(r, w)
|
2016-01-29 09:46:11 +02:00
|
|
|
|
2016-04-09 23:00:23 +02:00
|
|
|
// Middleware
|
2016-10-21 21:49:08 +02:00
|
|
|
h := func(c Context) error {
|
2016-09-23 07:53:44 +02:00
|
|
|
method := r.Method
|
|
|
|
path := r.URL.Path
|
2016-04-09 23:00:23 +02:00
|
|
|
e.router.Find(method, path, c)
|
2016-10-21 21:49:08 +02:00
|
|
|
h := c.Handler()
|
2016-04-09 23:00:23 +02:00
|
|
|
for i := len(e.middleware) - 1; i >= 0; i-- {
|
|
|
|
h = e.middleware[i](h)
|
|
|
|
}
|
|
|
|
return h(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Premiddleware
|
|
|
|
for i := len(e.premiddleware) - 1; i >= 0; i-- {
|
|
|
|
h = e.premiddleware[i](h)
|
|
|
|
}
|
|
|
|
|
2016-01-29 09:46:11 +02:00
|
|
|
// Execute chain
|
2016-04-09 23:00:23 +02:00
|
|
|
if err := h(c); err != nil {
|
2016-09-23 07:53:44 +02:00
|
|
|
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-09-26 04:48:15 +02:00
|
|
|
func (e *Echo) config(gs *graceful.Server, s *http.Server, address string) {
|
|
|
|
e.color.SetOutput(e.Logger.Output())
|
2016-09-25 21:14:50 +02:00
|
|
|
gs.Server = s
|
|
|
|
gs.Addr = address
|
|
|
|
gs.Handler = e
|
|
|
|
gs.Timeout = e.ShutdownTimeout
|
|
|
|
gs.Logger = slog.New(e.Logger.Output(), e.Logger.Prefix()+": ", 0)
|
2016-09-26 04:48:15 +02:00
|
|
|
if gs == e.gracefulTLS && !e.DisableHTTP2 {
|
|
|
|
e.TLSConfig.NextProtos = append(e.TLSConfig.NextProtos, "h2")
|
|
|
|
}
|
2016-09-25 21:14:50 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 17:29:16 +02:00
|
|
|
// Start starts the HTTP server.
|
2016-09-26 04:48:15 +02:00
|
|
|
// Note: If custom `Echo#Server` is used, it's Addr and Handler properties are
|
|
|
|
// ignored.
|
2016-09-23 17:29:16 +02:00
|
|
|
func (e *Echo) Start(address string) (err error) {
|
2016-09-26 04:48:15 +02:00
|
|
|
e.config(e.graceful, e.Server, address)
|
2016-09-25 20:41:08 +02:00
|
|
|
color.Printf(" ⇛ http server started on %s\n", color.Green(address))
|
2016-09-25 01:19:38 +02:00
|
|
|
return e.graceful.ListenAndServe()
|
2016-09-23 20:29:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartTLS starts the TLS server.
|
2016-09-26 04:48:15 +02:00
|
|
|
// Note: If custom `Echo#TLSServer` is used, it's Addr and Handler properties are
|
|
|
|
// ignored.
|
2016-09-23 20:29:50 +02:00
|
|
|
func (e *Echo) StartTLS(address string, certFile, keyFile string) (err error) {
|
2016-09-26 04:48:15 +02:00
|
|
|
e.config(e.gracefulTLS, e.TLSServer, address)
|
2016-09-25 01:19:38 +02:00
|
|
|
if certFile == "" || keyFile == "" {
|
|
|
|
return errors.New("invalid tls configuration")
|
2016-09-23 20:29:50 +02:00
|
|
|
}
|
2016-09-25 07:36:53 +02:00
|
|
|
e.TLSConfig.Certificates = make([]tls.Certificate, 1)
|
|
|
|
e.TLSConfig.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
|
2016-09-23 07:53:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-09-25 20:41:08 +02:00
|
|
|
color.Printf(" ⇛ https server started on %s\n", color.Green(address))
|
2016-09-25 07:36:53 +02:00
|
|
|
return e.gracefulTLS.ListenAndServeTLSConfig(e.TLSConfig)
|
2016-09-25 01:19:38 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 04:48:15 +02:00
|
|
|
// StartAutoTLS starts the TLS server using certificates automatically from https://letsencrypt.org.
|
|
|
|
// Note: If custom `Echo#TLSServer` is used, it's Addr and Handler properties are
|
|
|
|
// ignored.
|
|
|
|
func (e *Echo) StartAutoTLS() (err error) {
|
|
|
|
address := ":443"
|
|
|
|
e.config(e.gracefulTLS, e.TLSServer, address)
|
|
|
|
e.TLSConfig.GetCertificate = e.tlsManager.GetCertificate
|
|
|
|
if err = e.tlsManager.CacheFile(e.TLSCacheFile); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// NOTE: Added security
|
|
|
|
e.tlsManager.SetHosts(e.TLSHosts)
|
|
|
|
color.Printf(" ⇛ https auto tls server started on %s\n", color.Green(address))
|
|
|
|
return e.gracefulTLS.ListenAndServeTLSConfig(e.TLSConfig)
|
|
|
|
}
|
|
|
|
|
2016-09-25 02:19:50 +02:00
|
|
|
// Shutdown gracefully shutdown the HTTP server with timeout.
|
2016-09-25 01:19:38 +02:00
|
|
|
func (e *Echo) Shutdown(timeout time.Duration) {
|
|
|
|
e.graceful.Stop(timeout)
|
2015-04-03 14:24:47 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 02:19:50 +02:00
|
|
|
// ShutdownTLS gracefully shutdown the TLS server with timeout.
|
|
|
|
func (e *Echo) ShutdownTLS(timeout time.Duration) {
|
|
|
|
e.gracefulTLS.Stop(timeout)
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// NewHTTPError creates a new HTTPError instance.
|
2015-05-23 05:26:52 +02:00
|
|
|
func NewHTTPError(code int, msg ...string) *HTTPError {
|
2016-03-11 17:53:54 +02:00
|
|
|
he := &HTTPError{Code: code, Message: http.StatusText(code)}
|
2015-05-29 01:50:49 +02:00
|
|
|
if len(msg) > 0 {
|
|
|
|
m := msg[0]
|
2016-03-11 17:53:54 +02:00
|
|
|
he.Message = m
|
2015-05-23 05:26:52 +02:00
|
|
|
}
|
|
|
|
return he
|
|
|
|
}
|
|
|
|
|
2016-03-11 17:53:54 +02:00
|
|
|
// Error makes it compatible with `error` interface.
|
2015-05-23 05:26:52 +02:00
|
|
|
func (e *HTTPError) Error() string {
|
2016-03-11 17:53:54 +02:00
|
|
|
return e.Message
|
2015-05-23 05:26:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 14:31:48 +02:00
|
|
|
// WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
|
|
|
|
func WrapHandler(h http.Handler) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
h.ServeHTTP(c.Response(), c.Request())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrapMiddleware wraps `func(http.Handler) http.Handler` into `echo.MiddlewareFunc`
|
|
|
|
func WrapMiddleware(m func(http.Handler) http.Handler) MiddlewareFunc {
|
2016-04-02 23:19:39 +02:00
|
|
|
return func(next HandlerFunc) HandlerFunc {
|
2016-09-23 14:31:48 +02:00
|
|
|
return func(c Context) (err error) {
|
|
|
|
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-10-24 17:21:36 +02:00
|
|
|
c.SetRequest(r)
|
2016-09-23 14:31:48 +02:00
|
|
|
err = next(c)
|
|
|
|
})).ServeHTTP(c.Response(), c.Request())
|
|
|
|
return
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2016-03-07 17:55:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
func handlerName(h HandlerFunc) string {
|
2016-02-17 21:58:03 +02:00
|
|
|
t := reflect.ValueOf(h).Type()
|
|
|
|
if t.Kind() == reflect.Func {
|
|
|
|
return runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
|
|
|
|
}
|
|
|
|
return t.String()
|
2016-02-09 08:17:20 +02:00
|
|
|
}
|