2024-03-09 11:21:24 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
/*
|
2016-11-29 06:30:42 +02:00
|
|
|
Package echo implements high performance, minimalist Go web framework.
|
2016-03-20 00:47:20 +02:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2022-11-19 22:05:53 +02:00
|
|
|
package main
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2022-11-19 22:05:53 +02:00
|
|
|
import (
|
|
|
|
"net/http"
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2022-11-19 22:05:53 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
)
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2022-11-19 22:05:53 +02:00
|
|
|
// Handler
|
|
|
|
func hello(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "Hello, World!")
|
|
|
|
}
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2022-11-19 22:05:53 +02:00
|
|
|
func main() {
|
|
|
|
// Echo instance
|
|
|
|
e := echo.New()
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2022-11-19 22:05:53 +02:00
|
|
|
// Middleware
|
|
|
|
e.Use(middleware.Logger())
|
|
|
|
e.Use(middleware.Recover())
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2022-11-19 22:05:53 +02:00
|
|
|
// Routes
|
|
|
|
e.GET("/", hello)
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2022-11-19 22:05:53 +02:00
|
|
|
// Start server
|
|
|
|
e.Logger.Fatal(e.Start(":1323"))
|
|
|
|
}
|
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 (
|
2018-04-03 13:09:21 +02:00
|
|
|
stdContext "context"
|
2016-09-23 07:53:44 +02:00
|
|
|
"crypto/tls"
|
2023-05-29 22:26:53 +02:00
|
|
|
"encoding/json"
|
2015-04-03 05:18:34 +02:00
|
|
|
"errors"
|
2015-04-29 03:53:57 +02:00
|
|
|
"fmt"
|
2017-02-20 05:40:18 +02:00
|
|
|
stdLog "log"
|
2017-01-14 04:40:27 +02:00
|
|
|
"net"
|
2015-03-27 23:35:15 +02:00
|
|
|
"net/http"
|
2022-11-21 14:29:43 +02:00
|
|
|
"os"
|
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
|
|
|
|
2017-01-14 22:20:01 +02:00
|
|
|
"github.com/labstack/gommon/color"
|
2016-12-11 08:05:41 +02:00
|
|
|
"github.com/labstack/gommon/log"
|
2019-03-13 02:54:16 +02:00
|
|
|
"golang.org/x/crypto/acme"
|
2018-03-12 22:01:59 +02:00
|
|
|
"golang.org/x/crypto/acme/autocert"
|
2020-01-29 00:46:00 +02:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/h2c"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// Echo is the top-level framework instance.
|
|
|
|
//
|
|
|
|
// Goroutine safety: Do not mutate Echo instance fields after server has started. Accessing these
|
|
|
|
// fields from handlers/middlewares and changing field values at the same time leads to data-races.
|
|
|
|
// Adding new routes after the server has been started is also not safe!
|
|
|
|
type Echo struct {
|
|
|
|
filesystem
|
|
|
|
common
|
|
|
|
// startupMutex is mutex to lock Echo instance access during server configuration and startup. Useful for to get
|
|
|
|
// listener address info (on which interface/port was listener bound) without having data races.
|
|
|
|
startupMutex sync.RWMutex
|
|
|
|
colorer *color.Color
|
|
|
|
|
|
|
|
// premiddleware are middlewares that are run before routing is done. In case a pre-middleware returns
|
|
|
|
// an error the router is not executed and the request will end up in the global error handler.
|
|
|
|
premiddleware []MiddlewareFunc
|
|
|
|
middleware []MiddlewareFunc
|
|
|
|
maxParam *int
|
|
|
|
router *Router
|
|
|
|
routers map[string]*Router
|
|
|
|
pool sync.Pool
|
|
|
|
|
|
|
|
StdLogger *stdLog.Logger
|
|
|
|
Server *http.Server
|
|
|
|
TLSServer *http.Server
|
|
|
|
Listener net.Listener
|
|
|
|
TLSListener net.Listener
|
|
|
|
AutoTLSManager autocert.Manager
|
|
|
|
HTTPErrorHandler HTTPErrorHandler
|
|
|
|
Binder Binder
|
|
|
|
JSONSerializer JSONSerializer
|
|
|
|
Validator Validator
|
|
|
|
Renderer Renderer
|
|
|
|
Logger Logger
|
|
|
|
IPExtractor IPExtractor
|
|
|
|
ListenerNetwork string
|
|
|
|
|
|
|
|
// OnAddRouteHandler is called when Echo adds new route to specific host router.
|
|
|
|
OnAddRouteHandler func(host string, route Route, handler HandlerFunc, middleware []MiddlewareFunc)
|
2024-05-30 21:50:01 +02:00
|
|
|
DisableHTTP2 bool
|
|
|
|
Debug bool
|
|
|
|
HideBanner bool
|
|
|
|
HidePort bool
|
2024-03-09 17:53:07 +02:00
|
|
|
}
|
2015-06-01 09:07:53 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// Route contains a handler and information for matching against requests.
|
|
|
|
type Route struct {
|
|
|
|
Method string `json:"method"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
2015-05-23 05:26:52 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// HTTPError represents an error that occurred while handling a request.
|
|
|
|
type HTTPError struct {
|
|
|
|
Internal error `json:"-"` // Stores the error returned by an external dependency
|
2024-05-30 21:50:01 +02:00
|
|
|
Message interface{} `json:"message"`
|
|
|
|
Code int `json:"-"`
|
2024-03-09 17:53:07 +02:00
|
|
|
}
|
2015-05-23 05:26:52 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// MiddlewareFunc defines a function to process middleware.
|
|
|
|
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
|
2016-02-09 03:26:00 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// HandlerFunc defines a function to serve HTTP requests.
|
|
|
|
type HandlerFunc func(c Context) error
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// HTTPErrorHandler is a centralized HTTP error handler.
|
|
|
|
type HTTPErrorHandler func(err error, c Context)
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// Validator is the interface that wraps the Validate function.
|
|
|
|
type Validator interface {
|
|
|
|
Validate(i interface{}) error
|
|
|
|
}
|
2015-08-01 04:25:03 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// JSONSerializer is the interface that encodes and decodes JSON to and from interfaces.
|
|
|
|
type JSONSerializer interface {
|
|
|
|
Serialize(c Context, i interface{}, indent string) error
|
|
|
|
Deserialize(c Context, i interface{}) error
|
|
|
|
}
|
2021-07-05 21:33:19 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// Map defines a generic map of type `map[string]interface{}`.
|
|
|
|
type Map map[string]interface{}
|
2016-10-31 00:03:18 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// Common struct for Echo & Group.
|
|
|
|
type common struct{}
|
2015-03-27 23:35:15 +02:00
|
|
|
|
2018-10-15 09:31:26 +02:00
|
|
|
// HTTP methods
|
|
|
|
// NOTE: Deprecated, please use the stdlib constants directly instead.
|
|
|
|
const (
|
|
|
|
CONNECT = http.MethodConnect
|
|
|
|
DELETE = http.MethodDelete
|
|
|
|
GET = http.MethodGet
|
|
|
|
HEAD = http.MethodHead
|
|
|
|
OPTIONS = http.MethodOptions
|
|
|
|
PATCH = http.MethodPatch
|
|
|
|
POST = http.MethodPost
|
2019-01-10 01:19:48 +02:00
|
|
|
// PROPFIND = "PROPFIND"
|
2018-10-15 09:31:26 +02:00
|
|
|
PUT = http.MethodPut
|
|
|
|
TRACE = http.MethodTrace
|
|
|
|
)
|
|
|
|
|
2016-04-06 16:28:53 +02:00
|
|
|
// MIME types
|
2016-03-20 00:47:20 +02:00
|
|
|
const (
|
2024-02-06 07:41:33 +02:00
|
|
|
// MIMEApplicationJSON JavaScript Object Notation (JSON) https://www.rfc-editor.org/rfc/rfc8259
|
|
|
|
MIMEApplicationJSON = "application/json"
|
|
|
|
// Deprecated: Please use MIMEApplicationJSON instead. JSON should be encoded using UTF-8 by default.
|
|
|
|
// No "charset" parameter is defined for this registration.
|
|
|
|
// Adding one really has no effect on compliant recipients.
|
|
|
|
// See RFC 8259, section 8.1. https://datatracker.ietf.org/doc/html/rfc8259#section-8.1
|
2016-04-06 16:28:53 +02:00
|
|
|
MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8
|
|
|
|
MIMEApplicationJavaScript = "application/javascript"
|
|
|
|
MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
|
|
|
|
MIMEApplicationXML = "application/xml"
|
|
|
|
MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8
|
2017-02-28 22:04:29 +02:00
|
|
|
MIMETextXML = "text/xml"
|
|
|
|
MIMETextXMLCharsetUTF8 = MIMETextXML + "; " + charsetUTF8
|
2016-04-06 16:28:53 +02:00
|
|
|
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-12-26 07:42:46 +02:00
|
|
|
charsetUTF8 = "charset=UTF-8"
|
2019-01-10 01:19:48 +02:00
|
|
|
// PROPFIND Method can be used on collection and property resources.
|
2019-01-27 00:39:45 +02:00
|
|
|
PROPFIND = "PROPFIND"
|
2019-06-09 18:11:18 +02:00
|
|
|
// REPORT Method can be used to get information about a resource, see rfc 3253
|
|
|
|
REPORT = "REPORT"
|
2022-07-12 20:53:41 +02:00
|
|
|
// RouteNotFound is special method type for routes handling "route not found" (404) cases
|
|
|
|
RouteNotFound = "echo_route_not_found"
|
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 (
|
2021-12-04 20:02:11 +02:00
|
|
|
HeaderAccept = "Accept"
|
|
|
|
HeaderAcceptEncoding = "Accept-Encoding"
|
2021-12-09 21:57:20 +02:00
|
|
|
// HeaderAllow is the name of the "Allow" header field used to list the set of methods
|
|
|
|
// advertised as supported by the target resource. Returning an Allow header is mandatory
|
|
|
|
// for status 405 (method not found) and useful for the OPTIONS method in responses.
|
|
|
|
// See RFC 7231: https://datatracker.ietf.org/doc/html/rfc7231#section-7.4.1
|
2017-06-04 19:39:08 +02:00
|
|
|
HeaderAllow = "Allow"
|
|
|
|
HeaderAuthorization = "Authorization"
|
|
|
|
HeaderContentDisposition = "Content-Disposition"
|
|
|
|
HeaderContentEncoding = "Content-Encoding"
|
|
|
|
HeaderContentLength = "Content-Length"
|
|
|
|
HeaderContentType = "Content-Type"
|
|
|
|
HeaderCookie = "Cookie"
|
|
|
|
HeaderSetCookie = "Set-Cookie"
|
|
|
|
HeaderIfModifiedSince = "If-Modified-Since"
|
|
|
|
HeaderLastModified = "Last-Modified"
|
|
|
|
HeaderLocation = "Location"
|
2022-01-21 18:32:53 +02:00
|
|
|
HeaderRetryAfter = "Retry-After"
|
2017-06-04 19:39:08 +02:00
|
|
|
HeaderUpgrade = "Upgrade"
|
|
|
|
HeaderVary = "Vary"
|
|
|
|
HeaderWWWAuthenticate = "WWW-Authenticate"
|
|
|
|
HeaderXForwardedFor = "X-Forwarded-For"
|
|
|
|
HeaderXForwardedProto = "X-Forwarded-Proto"
|
|
|
|
HeaderXForwardedProtocol = "X-Forwarded-Protocol"
|
|
|
|
HeaderXForwardedSsl = "X-Forwarded-Ssl"
|
|
|
|
HeaderXUrlScheme = "X-Url-Scheme"
|
|
|
|
HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
|
2022-03-01 09:56:46 +02:00
|
|
|
HeaderXRealIP = "X-Real-Ip"
|
|
|
|
HeaderXRequestID = "X-Request-Id"
|
|
|
|
HeaderXCorrelationID = "X-Correlation-Id"
|
2018-03-06 11:52:28 +02:00
|
|
|
HeaderXRequestedWith = "X-Requested-With"
|
2017-06-04 19:39:08 +02:00
|
|
|
HeaderServer = "Server"
|
|
|
|
HeaderOrigin = "Origin"
|
2022-03-02 01:11:28 +02:00
|
|
|
HeaderCacheControl = "Cache-Control"
|
|
|
|
HeaderConnection = "Connection"
|
2017-06-04 19:39:08 +02:00
|
|
|
|
|
|
|
// Access control
|
2016-04-08 01:16:58 +02:00
|
|
|
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
|
2019-02-27 08:32:07 +02:00
|
|
|
HeaderStrictTransportSecurity = "Strict-Transport-Security"
|
|
|
|
HeaderXContentTypeOptions = "X-Content-Type-Options"
|
|
|
|
HeaderXXSSProtection = "X-XSS-Protection"
|
|
|
|
HeaderXFrameOptions = "X-Frame-Options"
|
|
|
|
HeaderContentSecurityPolicy = "Content-Security-Policy"
|
|
|
|
HeaderContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only"
|
|
|
|
HeaderXCSRFToken = "X-CSRF-Token"
|
2019-08-02 00:27:09 +02:00
|
|
|
HeaderReferrerPolicy = "Referrer-Policy"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
2017-05-12 19:38:02 +02:00
|
|
|
const (
|
2019-01-10 01:19:48 +02:00
|
|
|
// Version of Echo
|
2024-12-12 07:47:56 +02:00
|
|
|
Version = "4.13.2"
|
2017-05-12 19:38:02 +02:00
|
|
|
website = "https://echo.labstack.com"
|
|
|
|
// http://patorjk.com/software/taag/#p=display&f=Small%20Slant&t=Echo
|
|
|
|
banner = `
|
|
|
|
____ __
|
|
|
|
/ __/___/ / ___
|
|
|
|
/ _// __/ _ \/ _ \
|
|
|
|
/___/\__/_//_/\___/ %s
|
|
|
|
High performance, minimalist Go web framework
|
|
|
|
%s
|
|
|
|
____________________________________O/_______
|
|
|
|
O\
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
var methods = [...]string{
|
|
|
|
http.MethodConnect,
|
|
|
|
http.MethodDelete,
|
|
|
|
http.MethodGet,
|
|
|
|
http.MethodHead,
|
|
|
|
http.MethodOptions,
|
|
|
|
http.MethodPatch,
|
|
|
|
http.MethodPost,
|
|
|
|
PROPFIND,
|
|
|
|
http.MethodPut,
|
|
|
|
http.MethodTrace,
|
|
|
|
REPORT,
|
|
|
|
}
|
2015-08-26 05:36:15 +02:00
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Errors
|
|
|
|
var (
|
2023-02-21 23:32:11 +02:00
|
|
|
ErrBadRequest = NewHTTPError(http.StatusBadRequest) // HTTP 400 Bad Request
|
|
|
|
ErrUnauthorized = NewHTTPError(http.StatusUnauthorized) // HTTP 401 Unauthorized
|
|
|
|
ErrPaymentRequired = NewHTTPError(http.StatusPaymentRequired) // HTTP 402 Payment Required
|
|
|
|
ErrForbidden = NewHTTPError(http.StatusForbidden) // HTTP 403 Forbidden
|
|
|
|
ErrNotFound = NewHTTPError(http.StatusNotFound) // HTTP 404 Not Found
|
|
|
|
ErrMethodNotAllowed = NewHTTPError(http.StatusMethodNotAllowed) // HTTP 405 Method Not Allowed
|
|
|
|
ErrNotAcceptable = NewHTTPError(http.StatusNotAcceptable) // HTTP 406 Not Acceptable
|
|
|
|
ErrProxyAuthRequired = NewHTTPError(http.StatusProxyAuthRequired) // HTTP 407 Proxy AuthRequired
|
|
|
|
ErrRequestTimeout = NewHTTPError(http.StatusRequestTimeout) // HTTP 408 Request Timeout
|
|
|
|
ErrConflict = NewHTTPError(http.StatusConflict) // HTTP 409 Conflict
|
|
|
|
ErrGone = NewHTTPError(http.StatusGone) // HTTP 410 Gone
|
|
|
|
ErrLengthRequired = NewHTTPError(http.StatusLengthRequired) // HTTP 411 Length Required
|
|
|
|
ErrPreconditionFailed = NewHTTPError(http.StatusPreconditionFailed) // HTTP 412 Precondition Failed
|
|
|
|
ErrStatusRequestEntityTooLarge = NewHTTPError(http.StatusRequestEntityTooLarge) // HTTP 413 Payload Too Large
|
|
|
|
ErrRequestURITooLong = NewHTTPError(http.StatusRequestURITooLong) // HTTP 414 URI Too Long
|
|
|
|
ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType) // HTTP 415 Unsupported Media Type
|
|
|
|
ErrRequestedRangeNotSatisfiable = NewHTTPError(http.StatusRequestedRangeNotSatisfiable) // HTTP 416 Range Not Satisfiable
|
|
|
|
ErrExpectationFailed = NewHTTPError(http.StatusExpectationFailed) // HTTP 417 Expectation Failed
|
|
|
|
ErrTeapot = NewHTTPError(http.StatusTeapot) // HTTP 418 I'm a teapot
|
|
|
|
ErrMisdirectedRequest = NewHTTPError(http.StatusMisdirectedRequest) // HTTP 421 Misdirected Request
|
|
|
|
ErrUnprocessableEntity = NewHTTPError(http.StatusUnprocessableEntity) // HTTP 422 Unprocessable Entity
|
|
|
|
ErrLocked = NewHTTPError(http.StatusLocked) // HTTP 423 Locked
|
|
|
|
ErrFailedDependency = NewHTTPError(http.StatusFailedDependency) // HTTP 424 Failed Dependency
|
|
|
|
ErrTooEarly = NewHTTPError(http.StatusTooEarly) // HTTP 425 Too Early
|
|
|
|
ErrUpgradeRequired = NewHTTPError(http.StatusUpgradeRequired) // HTTP 426 Upgrade Required
|
|
|
|
ErrPreconditionRequired = NewHTTPError(http.StatusPreconditionRequired) // HTTP 428 Precondition Required
|
|
|
|
ErrTooManyRequests = NewHTTPError(http.StatusTooManyRequests) // HTTP 429 Too Many Requests
|
|
|
|
ErrRequestHeaderFieldsTooLarge = NewHTTPError(http.StatusRequestHeaderFieldsTooLarge) // HTTP 431 Request Header Fields Too Large
|
|
|
|
ErrUnavailableForLegalReasons = NewHTTPError(http.StatusUnavailableForLegalReasons) // HTTP 451 Unavailable For Legal Reasons
|
|
|
|
ErrInternalServerError = NewHTTPError(http.StatusInternalServerError) // HTTP 500 Internal Server Error
|
|
|
|
ErrNotImplemented = NewHTTPError(http.StatusNotImplemented) // HTTP 501 Not Implemented
|
|
|
|
ErrBadGateway = NewHTTPError(http.StatusBadGateway) // HTTP 502 Bad Gateway
|
|
|
|
ErrServiceUnavailable = NewHTTPError(http.StatusServiceUnavailable) // HTTP 503 Service Unavailable
|
|
|
|
ErrGatewayTimeout = NewHTTPError(http.StatusGatewayTimeout) // HTTP 504 Gateway Timeout
|
|
|
|
ErrHTTPVersionNotSupported = NewHTTPError(http.StatusHTTPVersionNotSupported) // HTTP 505 HTTP Version Not Supported
|
|
|
|
ErrVariantAlsoNegotiates = NewHTTPError(http.StatusVariantAlsoNegotiates) // HTTP 506 Variant Also Negotiates
|
|
|
|
ErrInsufficientStorage = NewHTTPError(http.StatusInsufficientStorage) // HTTP 507 Insufficient Storage
|
|
|
|
ErrLoopDetected = NewHTTPError(http.StatusLoopDetected) // HTTP 508 Loop Detected
|
|
|
|
ErrNotExtended = NewHTTPError(http.StatusNotExtended) // HTTP 510 Not Extended
|
|
|
|
ErrNetworkAuthenticationRequired = NewHTTPError(http.StatusNetworkAuthenticationRequired) // HTTP 511 Network Authentication Required
|
|
|
|
|
|
|
|
ErrValidatorNotRegistered = errors.New("validator not registered")
|
|
|
|
ErrRendererNotRegistered = errors.New("renderer not registered")
|
|
|
|
ErrInvalidRedirectCode = errors.New("invalid redirect status code")
|
|
|
|
ErrCookieNotFound = errors.New("cookie not found")
|
|
|
|
ErrInvalidCertOrKeyType = errors.New("invalid cert or key type, must be string or []byte")
|
|
|
|
ErrInvalidListenerNetwork = errors.New("invalid listener network")
|
2016-03-20 00:47:20 +02:00
|
|
|
)
|
2015-07-27 17:43:11 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// NotFoundHandler is the handler that router uses in case there was no matching route found. Returns an error that results
|
|
|
|
// HTTP 404 status code.
|
|
|
|
var NotFoundHandler = func(c Context) error {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
2015-07-24 21:03:36 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// MethodNotAllowedHandler is the handler thar router uses in case there was no matching route found but there was
|
|
|
|
// another matching routes for that requested URL. Returns an error that results HTTP 405 Method Not Allowed status code.
|
|
|
|
var MethodNotAllowedHandler = func(c Context) error {
|
|
|
|
// See RFC 7231 section 7.4.1: An origin server MUST generate an Allow field in a 405 (Method Not Allowed)
|
|
|
|
// response and MAY do so in any other response. For disabled resources an empty Allow header may be returned
|
|
|
|
routerAllowMethods, ok := c.Get(ContextKeyHeaderAllow).(string)
|
|
|
|
if ok && routerAllowMethods != "" {
|
|
|
|
c.Response().Header().Set(HeaderAllow, routerAllowMethods)
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2024-03-09 17:53:07 +02:00
|
|
|
return ErrMethodNotAllowed
|
|
|
|
}
|
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) {
|
2016-09-23 17:29:16 +02:00
|
|
|
e = &Echo{
|
2022-01-08 22:41:34 +02:00
|
|
|
filesystem: createFilesystem(),
|
|
|
|
Server: new(http.Server),
|
|
|
|
TLSServer: new(http.Server),
|
2016-12-02 19:28:18 +02:00
|
|
|
AutoTLSManager: autocert.Manager{
|
|
|
|
Prompt: autocert.AcceptTOS,
|
|
|
|
},
|
2020-11-05 06:15:32 +02:00
|
|
|
Logger: log.New("echo"),
|
|
|
|
colorer: color.New(),
|
|
|
|
maxParam: new(int),
|
|
|
|
ListenerNetwork: "tcp",
|
2016-09-25 01:19:38 +02:00
|
|
|
}
|
2017-01-14 04:40:27 +02:00
|
|
|
e.Server.Handler = e
|
|
|
|
e.TLSServer.Handler = e
|
2016-09-25 21:14:50 +02:00
|
|
|
e.HTTPErrorHandler = e.DefaultHTTPErrorHandler
|
2016-12-10 18:38:27 +02:00
|
|
|
e.Binder = &DefaultBinder{}
|
2021-07-05 21:33:19 +02:00
|
|
|
e.JSONSerializer = &DefaultJSONSerializer{}
|
2017-08-18 04:46:17 +02:00
|
|
|
e.Logger.SetLevel(log.ERROR)
|
2018-09-28 19:09:35 +02:00
|
|
|
e.StdLogger = stdLog.New(e.Logger.Output(), e.Logger.Prefix()+": ", 0)
|
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)
|
2019-04-29 07:22:35 +02:00
|
|
|
e.routers = map[string]*Router{}
|
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 {
|
2017-06-29 19:59:21 +02:00
|
|
|
return &context{
|
2016-09-23 07:53:44 +02:00
|
|
|
request: r,
|
2017-08-24 17:27:15 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 07:49:18 +02:00
|
|
|
// Router returns the default router.
|
2015-05-23 05:26:52 +02:00
|
|
|
func (e *Echo) Router() *Router {
|
|
|
|
return e.router
|
|
|
|
}
|
|
|
|
|
2019-04-29 07:49:18 +02:00
|
|
|
// Routers returns the map of host => router.
|
|
|
|
func (e *Echo) Routers() map[string]*Router {
|
|
|
|
return e.routers
|
|
|
|
}
|
|
|
|
|
2016-12-11 08:05:41 +02:00
|
|
|
// DefaultHTTPErrorHandler is the default HTTP error handler. It sends a JSON response
|
|
|
|
// with status code.
|
2021-09-14 19:57:47 +02:00
|
|
|
//
|
|
|
|
// NOTE: In case errors happens in middleware call-chain that is returning from handler (which did not return an error).
|
|
|
|
// When handler has already sent response (ala c.JSON()) and there is error in middleware that is returning from
|
2024-03-06 21:43:59 +02:00
|
|
|
// handler. Then the error that global error handler received will be ignored because we have already "committed" the
|
2021-09-14 19:57:47 +02:00
|
|
|
// response and status code header has been sent to the client.
|
2015-12-04 03:23:53 +02:00
|
|
|
func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {
|
2021-08-12 12:56:04 +02:00
|
|
|
|
|
|
|
if c.Response().Committed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-02 07:20:33 +02:00
|
|
|
he, ok := err.(*HTTPError)
|
|
|
|
if ok {
|
2018-04-10 21:06:31 +02:00
|
|
|
if he.Internal != nil {
|
2019-08-11 21:48:50 +02:00
|
|
|
if herr, ok := he.Internal.(*HTTPError); ok {
|
|
|
|
he = herr
|
|
|
|
}
|
2017-08-31 18:18:42 +02:00
|
|
|
}
|
2016-12-11 08:05:41 +02:00
|
|
|
} else {
|
2019-08-02 07:20:33 +02:00
|
|
|
he = &HTTPError{
|
2019-08-11 21:48:50 +02:00
|
|
|
Code: http.StatusInternalServerError,
|
2019-08-05 06:35:30 +02:00
|
|
|
Message: http.StatusText(http.StatusInternalServerError),
|
2019-08-02 07:20:33 +02:00
|
|
|
}
|
2016-12-12 00:57:14 +02:00
|
|
|
}
|
2019-10-31 04:01:23 +02:00
|
|
|
|
|
|
|
// Issue #1426
|
|
|
|
code := he.Code
|
|
|
|
message := he.Message
|
2023-05-29 22:26:53 +02:00
|
|
|
|
|
|
|
switch m := he.Message.(type) {
|
|
|
|
case string:
|
2020-11-05 04:37:15 +02:00
|
|
|
if e.Debug {
|
|
|
|
message = Map{"message": m, "error": err.Error()}
|
|
|
|
} else {
|
|
|
|
message = Map{"message": m}
|
|
|
|
}
|
2023-05-29 22:26:53 +02:00
|
|
|
case json.Marshaler:
|
|
|
|
// do nothing - this type knows how to format itself to JSON
|
|
|
|
case error:
|
|
|
|
message = Map{"message": m.Error()}
|
2016-02-09 08:17:20 +02:00
|
|
|
}
|
2016-12-11 08:05:41 +02:00
|
|
|
|
2017-09-08 17:48:23 +02:00
|
|
|
// Send response
|
2021-08-12 12:56:04 +02:00
|
|
|
if c.Request().Method == http.MethodHead { // Issue #608
|
|
|
|
err = c.NoContent(he.Code)
|
|
|
|
} else {
|
|
|
|
err = c.JSON(code, message)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
e.Logger.Error(err)
|
2016-02-09 08:17:20 +02:00
|
|
|
}
|
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.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
|
2018-10-14 17:16:58 +02:00
|
|
|
return e.Add(http.MethodConnect, path, h, m...)
|
2016-04-19 01:59:58 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
|
2018-10-14 17:16:58 +02:00
|
|
|
return e.Add(http.MethodDelete, path, h, m...)
|
2016-04-20 16:32:51 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) GET(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
|
2018-10-14 17:16:58 +02:00
|
|
|
return e.Add(http.MethodGet, path, h, m...)
|
2016-04-19 01:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
|
2018-10-14 17:16:58 +02:00
|
|
|
return e.Add(http.MethodHead, path, h, m...)
|
2016-04-19 01:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
|
2018-10-14 17:16:58 +02:00
|
|
|
return e.Add(http.MethodOptions, path, h, m...)
|
2016-04-19 01:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
|
2018-10-14 17:16:58 +02:00
|
|
|
return e.Add(http.MethodPatch, path, h, m...)
|
2016-04-19 01:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) POST(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
|
2018-10-14 17:16:58 +02:00
|
|
|
return e.Add(http.MethodPost, path, h, m...)
|
2016-04-19 01:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
|
2018-10-14 17:16:58 +02:00
|
|
|
return e.Add(http.MethodPut, path, h, m...)
|
2016-04-19 01:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
|
2018-10-14 17:16:58 +02:00
|
|
|
return e.Add(http.MethodTrace, path, h, m...)
|
2016-04-19 01:59:58 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 20:53:41 +02:00
|
|
|
// RouteNotFound registers a special-case route which is executed when no other route is found (i.e. HTTP 404 cases)
|
|
|
|
// for current request URL.
|
|
|
|
// Path supports static and named/any parameters just like other http method is defined. Generally path is ended with
|
|
|
|
// wildcard/match-any character (`/*`, `/download/*` etc).
|
|
|
|
//
|
|
|
|
// Example: `e.RouteNotFound("/*", func(c echo.Context) error { return c.NoContent(http.StatusNotFound) })`
|
|
|
|
func (e *Echo) RouteNotFound(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
|
|
|
|
return e.Add(RouteNotFound, path, h, m...)
|
|
|
|
}
|
|
|
|
|
2022-08-06 22:25:43 +02:00
|
|
|
// Any registers a new route for all HTTP methods (supported by Echo) and path with matching handler
|
2016-03-20 00:47:20 +02:00
|
|
|
// in the router with optional route-level middleware.
|
2022-08-06 22:25:43 +02:00
|
|
|
//
|
|
|
|
// Note: this method only adds specific set of supported HTTP methods as handler and is not true
|
|
|
|
// "catch-any-arbitrary-method" way of matching requests.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
|
2017-12-22 22:09:52 +02:00
|
|
|
routes := make([]*Route, len(methods))
|
|
|
|
for i, m := range methods {
|
|
|
|
routes[i] = e.Add(m, path, handler, middleware...)
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
2017-06-22 19:59:02 +02:00
|
|
|
return routes
|
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.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) Match(methods []string, path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
|
2017-12-22 22:09:52 +02:00
|
|
|
routes := make([]*Route, len(methods))
|
|
|
|
for i, m := range methods {
|
|
|
|
routes[i] = e.Add(m, path, handler, middleware...)
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
2017-06-22 19:59:02 +02:00
|
|
|
return routes
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
|
|
|
|
2019-04-29 22:53:12 +02:00
|
|
|
func (common) file(path, file string, get func(string, HandlerFunc, ...MiddlewareFunc) *Route,
|
2019-04-29 22:21:11 +02:00
|
|
|
m ...MiddlewareFunc) *Route {
|
|
|
|
return get(path, func(c Context) error {
|
2016-03-12 15:14:15 +02:00
|
|
|
return c.File(file)
|
2017-10-30 04:55:50 +02:00
|
|
|
}, m...)
|
2016-03-12 15:14:15 +02:00
|
|
|
}
|
|
|
|
|
2019-04-29 22:21:11 +02:00
|
|
|
// File registers a new route with path to serve a static file with optional route-level middleware.
|
|
|
|
func (e *Echo) File(path, file string, m ...MiddlewareFunc) *Route {
|
|
|
|
return e.file(path, file, e.GET, m...)
|
|
|
|
}
|
|
|
|
|
2022-11-12 19:38:02 +02:00
|
|
|
func (e *Echo) add(host, method, path string, handler HandlerFunc, middlewares ...MiddlewareFunc) *Route {
|
2019-04-29 07:22:35 +02:00
|
|
|
router := e.findRouter(host)
|
2022-11-12 18:35:19 +02:00
|
|
|
//FIXME: when handler+middleware are both nil ... make it behave like handler removal
|
|
|
|
name := handlerName(handler)
|
2022-11-12 19:38:02 +02:00
|
|
|
route := router.add(method, path, name, func(c Context) error {
|
|
|
|
h := applyMiddleware(handler, middlewares...)
|
2016-04-02 23:19:39 +02:00
|
|
|
return h(c)
|
2016-10-22 05:36:49 +02:00
|
|
|
})
|
2022-11-12 19:38:02 +02:00
|
|
|
|
|
|
|
if e.OnAddRouteHandler != nil {
|
|
|
|
e.OnAddRouteHandler(host, *route, handler, middlewares)
|
|
|
|
}
|
|
|
|
|
|
|
|
return route
|
2015-04-22 07:12:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-29 07:22:35 +02:00
|
|
|
// Add registers a new route for an HTTP method and path with matching handler
|
|
|
|
// in the router with optional route-level middleware.
|
|
|
|
func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
|
|
|
|
return e.add("", method, path, handler, middleware...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Host creates a new router group for the provided host and optional host-level middleware.
|
|
|
|
func (e *Echo) Host(name string, m ...MiddlewareFunc) (g *Group) {
|
|
|
|
e.routers[name] = NewRouter(e)
|
|
|
|
g = &Group{host: name, echo: e}
|
|
|
|
g.Use(m...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-06 16:00:51 +02:00
|
|
|
// URI generates an URI from handler.
|
2016-04-02 23:19:39 +02:00
|
|
|
func (e *Echo) URI(handler HandlerFunc, params ...interface{}) string {
|
2017-06-22 19:59:02 +02:00
|
|
|
name := handlerName(handler)
|
|
|
|
return e.Reverse(name, params...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// URL is an alias for `URI` function.
|
|
|
|
func (e *Echo) URL(h HandlerFunc, params ...interface{}) string {
|
|
|
|
return e.URI(h, params...)
|
|
|
|
}
|
|
|
|
|
2023-04-21 10:06:57 +02:00
|
|
|
// Reverse generates a URL from route name and provided parameters.
|
2017-06-22 19:59:02 +02:00
|
|
|
func (e *Echo) Reverse(name string, params ...interface{}) string {
|
2022-11-12 18:35:19 +02:00
|
|
|
return e.router.Reverse(name, params...)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-12 18:35:19 +02:00
|
|
|
// Routes returns the registered routes for default router.
|
|
|
|
// In case when Echo serves multiple hosts/domains use `e.Routers()["domain2.site"].Routes()` to get specific host routes.
|
2017-05-17 01:29:33 +02:00
|
|
|
func (e *Echo) Routes() []*Route {
|
2022-11-12 18:35:19 +02:00
|
|
|
return e.router.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.
|
2016-11-21 19:43:14 +02:00
|
|
|
// You must return the context by calling `ReleaseContext()`.
|
2016-05-04 02:23:31 +02:00
|
|
|
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) {
|
2017-01-24 20:48:11 +02:00
|
|
|
// Acquire context
|
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)
|
2022-10-25 16:12:15 +02:00
|
|
|
var h HandlerFunc
|
2018-03-15 08:53:32 +02:00
|
|
|
|
|
|
|
if e.premiddleware == nil {
|
2021-03-08 03:01:02 +02:00
|
|
|
e.findRouter(r.Host).Find(r.Method, GetPath(r), c)
|
2018-03-15 08:53:32 +02:00
|
|
|
h = c.Handler()
|
2019-03-05 18:24:59 +02:00
|
|
|
h = applyMiddleware(h, e.middleware...)
|
2018-03-15 08:53:32 +02:00
|
|
|
} else {
|
|
|
|
h = func(c Context) error {
|
2021-03-08 03:01:02 +02:00
|
|
|
e.findRouter(r.Host).Find(r.Method, GetPath(r), c)
|
2018-03-15 08:53:32 +02:00
|
|
|
h := c.Handler()
|
2019-03-05 18:24:59 +02:00
|
|
|
h = applyMiddleware(h, e.middleware...)
|
2018-03-15 08:53:32 +02:00
|
|
|
return h(c)
|
|
|
|
}
|
2019-03-05 18:24:59 +02:00
|
|
|
h = applyMiddleware(h, e.premiddleware...)
|
2016-04-09 23:00:23 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2018-03-14 23:03:59 +02:00
|
|
|
|
|
|
|
// Release context
|
|
|
|
e.pool.Put(c)
|
2015-11-15 23:32:21 +02:00
|
|
|
}
|
2015-06-27 23:34:22 +02:00
|
|
|
|
2017-01-14 04:40:27 +02:00
|
|
|
// Start starts an HTTP server.
|
2016-11-05 03:51:17 +02:00
|
|
|
func (e *Echo) Start(address string) error {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Lock()
|
2017-01-14 04:40:27 +02:00
|
|
|
e.Server.Addr = address
|
2020-12-29 11:46:09 +02:00
|
|
|
if err := e.configureServer(e.Server); err != nil {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2020-12-29 11:46:09 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2021-02-28 20:13:04 +02:00
|
|
|
return e.Server.Serve(e.Listener)
|
2016-09-23 20:29:50 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 04:40:27 +02:00
|
|
|
// StartTLS starts an HTTPS server.
|
2019-02-15 19:51:54 +02:00
|
|
|
// If `certFile` or `keyFile` is `string` the values are treated as file paths.
|
|
|
|
// If `certFile` or `keyFile` is `[]byte` the values are treated as the certificate or key as-is.
|
|
|
|
func (e *Echo) StartTLS(address string, certFile, keyFile interface{}) (err error) {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Lock()
|
2019-02-15 19:51:54 +02:00
|
|
|
var cert []byte
|
|
|
|
if cert, err = filepathOrContent(certFile); err != nil {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2019-02-15 19:51:54 +02:00
|
|
|
return
|
2016-09-23 20:29:50 +02:00
|
|
|
}
|
2019-02-15 19:51:54 +02:00
|
|
|
|
|
|
|
var key []byte
|
|
|
|
if key, err = filepathOrContent(keyFile); err != nil {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2019-02-15 19:51:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-14 04:40:27 +02:00
|
|
|
s := e.TLSServer
|
|
|
|
s.TLSConfig = new(tls.Config)
|
|
|
|
s.TLSConfig.Certificates = make([]tls.Certificate, 1)
|
2019-02-15 19:51:54 +02:00
|
|
|
if s.TLSConfig.Certificates[0], err = tls.X509KeyPair(cert, key); err != nil {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2016-09-23 07:53:44 +02:00
|
|
|
return
|
|
|
|
}
|
2019-02-15 19:51:54 +02:00
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
e.configureTLS(address)
|
|
|
|
if err := e.configureServer(s); err != nil {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2020-12-29 11:46:09 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2020-12-29 11:46:09 +02:00
|
|
|
return s.Serve(e.TLSListener)
|
2016-09-25 01:19:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 19:51:54 +02:00
|
|
|
func filepathOrContent(fileOrContent interface{}) (content []byte, err error) {
|
|
|
|
switch v := fileOrContent.(type) {
|
|
|
|
case string:
|
2022-11-21 14:29:43 +02:00
|
|
|
return os.ReadFile(v)
|
2019-02-15 19:51:54 +02:00
|
|
|
case []byte:
|
|
|
|
return v, nil
|
|
|
|
default:
|
|
|
|
return nil, ErrInvalidCertOrKeyType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 01:39:19 +02:00
|
|
|
// StartAutoTLS starts an HTTPS server using certificates automatically installed from https://letsencrypt.org.
|
2016-12-02 19:28:18 +02:00
|
|
|
func (e *Echo) StartAutoTLS(address string) error {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Lock()
|
2017-01-14 04:40:27 +02:00
|
|
|
s := e.TLSServer
|
|
|
|
s.TLSConfig = new(tls.Config)
|
|
|
|
s.TLSConfig.GetCertificate = e.AutoTLSManager.GetCertificate
|
2019-03-13 02:54:16 +02:00
|
|
|
s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, acme.ALPNProto)
|
2020-12-29 11:46:09 +02:00
|
|
|
|
|
|
|
e.configureTLS(address)
|
|
|
|
if err := e.configureServer(s); err != nil {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2020-12-29 11:46:09 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2020-12-29 11:46:09 +02:00
|
|
|
return s.Serve(e.TLSListener)
|
2016-11-05 03:51:17 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 11:46:09 +02:00
|
|
|
func (e *Echo) configureTLS(address string) {
|
2017-01-14 04:40:27 +02:00
|
|
|
s := e.TLSServer
|
|
|
|
s.Addr = address
|
2016-11-05 03:51:17 +02:00
|
|
|
if !e.DisableHTTP2 {
|
2017-01-14 04:40:27 +02:00
|
|
|
s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, "h2")
|
|
|
|
}
|
2016-11-05 03:51:17 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 04:40:27 +02:00
|
|
|
// StartServer starts a custom http server.
|
2017-01-18 22:17:44 +02:00
|
|
|
func (e *Echo) StartServer(s *http.Server) (err error) {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Lock()
|
2020-12-29 11:46:09 +02:00
|
|
|
if err := e.configureServer(s); err != nil {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2020-12-29 11:46:09 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-02-28 20:13:04 +02:00
|
|
|
if s.TLSConfig != nil {
|
|
|
|
e.startupMutex.Unlock()
|
|
|
|
return s.Serve(e.TLSListener)
|
|
|
|
}
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2021-02-28 20:13:04 +02:00
|
|
|
return s.Serve(e.Listener)
|
2020-12-29 11:46:09 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 11:44:07 +02:00
|
|
|
func (e *Echo) configureServer(s *http.Server) error {
|
2017-01-14 22:20:01 +02:00
|
|
|
// Setup
|
|
|
|
e.colorer.SetOutput(e.Logger.Output())
|
2018-09-28 19:09:35 +02:00
|
|
|
s.ErrorLog = e.StdLogger
|
2017-05-10 19:28:50 +02:00
|
|
|
s.Handler = e
|
|
|
|
if e.Debug {
|
|
|
|
e.Logger.SetLevel(log.DEBUG)
|
|
|
|
}
|
2017-01-14 22:20:01 +02:00
|
|
|
|
2017-05-12 19:38:02 +02:00
|
|
|
if !e.HideBanner {
|
2018-03-22 06:15:41 +02:00
|
|
|
e.colorer.Printf(banner, e.colorer.Red("v"+Version), e.colorer.Blue(website))
|
2017-05-12 19:38:02 +02:00
|
|
|
}
|
|
|
|
|
2016-11-05 03:51:17 +02:00
|
|
|
if s.TLSConfig == nil {
|
2017-01-14 04:40:27 +02:00
|
|
|
if e.Listener == nil {
|
2022-03-14 11:44:07 +02:00
|
|
|
l, err := newListener(s.Addr, e.ListenerNetwork)
|
2017-01-18 22:17:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-14 11:44:07 +02:00
|
|
|
e.Listener = l
|
2017-01-14 04:40:27 +02:00
|
|
|
}
|
2017-12-29 20:57:59 +02:00
|
|
|
if !e.HidePort {
|
2017-05-13 23:12:35 +02:00
|
|
|
e.colorer.Printf("⇨ http server started on %s\n", e.colorer.Green(e.Listener.Addr()))
|
2017-05-12 19:38:02 +02:00
|
|
|
}
|
2020-12-29 11:46:09 +02:00
|
|
|
return nil
|
2016-11-05 03:51:17 +02:00
|
|
|
}
|
2017-01-14 04:40:27 +02:00
|
|
|
if e.TLSListener == nil {
|
2020-11-05 06:15:32 +02:00
|
|
|
l, err := newListener(s.Addr, e.ListenerNetwork)
|
2017-01-18 22:17:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
e.TLSListener = tls.NewListener(l, s.TLSConfig)
|
2017-01-14 04:40:27 +02:00
|
|
|
}
|
2017-12-29 20:57:59 +02:00
|
|
|
if !e.HidePort {
|
2017-05-13 23:12:35 +02:00
|
|
|
e.colorer.Printf("⇨ https server started on %s\n", e.colorer.Green(e.TLSListener.Addr()))
|
2017-05-12 19:38:02 +02:00
|
|
|
}
|
2020-12-29 11:46:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerAddr returns net.Addr for Listener
|
|
|
|
func (e *Echo) ListenerAddr() net.Addr {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.RLock()
|
|
|
|
defer e.startupMutex.RUnlock()
|
2020-12-29 11:46:09 +02:00
|
|
|
if e.Listener == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.Listener.Addr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLSListenerAddr returns net.Addr for TLSListener
|
|
|
|
func (e *Echo) TLSListenerAddr() net.Addr {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.RLock()
|
|
|
|
defer e.startupMutex.RUnlock()
|
2020-12-29 11:46:09 +02:00
|
|
|
if e.TLSListener == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.TLSListener.Addr()
|
2016-09-25 02:19:50 +02:00
|
|
|
}
|
|
|
|
|
2020-01-29 00:46:00 +02:00
|
|
|
// StartH2CServer starts a custom http/2 server with h2c (HTTP/2 Cleartext).
|
2022-03-14 11:44:07 +02:00
|
|
|
func (e *Echo) StartH2CServer(address string, h2s *http2.Server) error {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Lock()
|
2020-01-29 00:46:00 +02:00
|
|
|
// Setup
|
|
|
|
s := e.Server
|
|
|
|
s.Addr = address
|
|
|
|
e.colorer.SetOutput(e.Logger.Output())
|
|
|
|
s.ErrorLog = e.StdLogger
|
|
|
|
s.Handler = h2c.NewHandler(e, h2s)
|
|
|
|
if e.Debug {
|
|
|
|
e.Logger.SetLevel(log.DEBUG)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !e.HideBanner {
|
|
|
|
e.colorer.Printf(banner, e.colorer.Red("v"+Version), e.colorer.Blue(website))
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Listener == nil {
|
2022-03-14 11:44:07 +02:00
|
|
|
l, err := newListener(s.Addr, e.ListenerNetwork)
|
2020-01-29 00:46:00 +02:00
|
|
|
if err != nil {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2020-01-29 00:46:00 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-03-14 11:44:07 +02:00
|
|
|
e.Listener = l
|
2020-01-29 00:46:00 +02:00
|
|
|
}
|
|
|
|
if !e.HidePort {
|
|
|
|
e.colorer.Printf("⇨ http server started on %s\n", e.colorer.Green(e.Listener.Addr()))
|
|
|
|
}
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Unlock()
|
2020-01-29 00:46:00 +02:00
|
|
|
return s.Serve(e.Listener)
|
|
|
|
}
|
|
|
|
|
2018-04-03 13:09:21 +02:00
|
|
|
// Close immediately stops the server.
|
|
|
|
// It internally calls `http.Server#Close()`.
|
|
|
|
func (e *Echo) Close() error {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Lock()
|
|
|
|
defer e.startupMutex.Unlock()
|
2018-04-03 13:09:21 +02:00
|
|
|
if err := e.TLSServer.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.Server.Close()
|
|
|
|
}
|
|
|
|
|
2019-01-10 01:14:26 +02:00
|
|
|
// Shutdown stops the server gracefully.
|
2018-04-03 13:09:21 +02:00
|
|
|
// It internally calls `http.Server#Shutdown()`.
|
|
|
|
func (e *Echo) Shutdown(ctx stdContext.Context) error {
|
2020-12-29 13:25:43 +02:00
|
|
|
e.startupMutex.Lock()
|
|
|
|
defer e.startupMutex.Unlock()
|
2018-04-03 13:09:21 +02:00
|
|
|
if err := e.TLSServer.Shutdown(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.Server.Shutdown(ctx)
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// NewHTTPError creates a new HTTPError instance.
|
2016-12-11 08:05:41 +02:00
|
|
|
func NewHTTPError(code int, message ...interface{}) *HTTPError {
|
2016-12-12 00:57:14 +02:00
|
|
|
he := &HTTPError{Code: code, Message: http.StatusText(code)}
|
2016-12-11 08:05:41 +02:00
|
|
|
if len(message) > 0 {
|
|
|
|
he.Message = message[0]
|
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.
|
2016-12-11 08:05:41 +02:00
|
|
|
func (he *HTTPError) Error() string {
|
2020-03-05 16:36:43 +02:00
|
|
|
if he.Internal == nil {
|
|
|
|
return fmt.Sprintf("code=%d, message=%v", he.Code, he.Message)
|
|
|
|
}
|
2019-08-02 07:20:33 +02:00
|
|
|
return fmt.Sprintf("code=%d, message=%v, internal=%v", he.Code, he.Message, he.Internal)
|
2015-05-23 05:26:52 +02:00
|
|
|
}
|
|
|
|
|
2019-01-10 01:19:48 +02:00
|
|
|
// SetInternal sets error to HTTPError.Internal
|
2018-09-28 19:41:13 +02:00
|
|
|
func (he *HTTPError) SetInternal(err error) *HTTPError {
|
|
|
|
he.Internal = err
|
|
|
|
return he
|
|
|
|
}
|
|
|
|
|
2022-11-19 22:05:53 +02:00
|
|
|
// WithInternal returns clone of HTTPError with err set to HTTPError.Internal field
|
|
|
|
func (he *HTTPError) WithInternal(err error) *HTTPError {
|
|
|
|
return &HTTPError{
|
|
|
|
Code: he.Code,
|
|
|
|
Message: he.Message,
|
|
|
|
Internal: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:13:30 +02:00
|
|
|
// Unwrap satisfies the Go 1.13 error wrapper interface.
|
|
|
|
func (he *HTTPError) Unwrap() error {
|
|
|
|
return he.Internal
|
|
|
|
}
|
|
|
|
|
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)
|
2019-08-07 20:10:18 +02:00
|
|
|
c.SetResponse(NewResponse(w, c.Echo()))
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 03:01:02 +02:00
|
|
|
// GetPath returns RawPath, if it's empty returns Path from URL
|
|
|
|
// Difference between RawPath and Path is:
|
2022-11-19 22:05:53 +02:00
|
|
|
// - Path is where request path is stored. Value is stored in decoded form: /%47%6f%2f becomes /Go/.
|
|
|
|
// - RawPath is an optional field which only gets set if the default encoding is different from Path.
|
2021-03-08 03:01:02 +02:00
|
|
|
func GetPath(r *http.Request) string {
|
|
|
|
path := r.URL.RawPath
|
|
|
|
if path == "" {
|
|
|
|
path = r.URL.Path
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2019-04-29 07:22:35 +02:00
|
|
|
func (e *Echo) findRouter(host string) *Router {
|
|
|
|
if len(e.routers) > 0 {
|
|
|
|
if r, ok := e.routers[host]; ok {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e.router
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-01-14 04:40:27 +02:00
|
|
|
|
2018-04-03 17:04:28 +02:00
|
|
|
// // PathUnescape is wraps `url.PathUnescape`
|
|
|
|
// func PathUnescape(s string) (string, error) {
|
|
|
|
// return url.PathUnescape(s)
|
|
|
|
// }
|
2018-04-03 13:09:21 +02:00
|
|
|
|
2017-01-14 04:40:27 +02:00
|
|
|
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
|
|
|
|
// connections. It's used by ListenAndServe and ListenAndServeTLS so
|
|
|
|
// dead TCP connections (e.g. closing laptop mid-download) eventually
|
|
|
|
// go away.
|
|
|
|
type tcpKeepAliveListener struct {
|
|
|
|
*net.TCPListener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
|
2019-02-01 16:33:56 +02:00
|
|
|
if c, err = ln.AcceptTCP(); err != nil {
|
|
|
|
return
|
|
|
|
} else if err = c.(*net.TCPConn).SetKeepAlive(true); err != nil {
|
|
|
|
return
|
2017-01-14 04:40:27 +02:00
|
|
|
}
|
2020-02-03 20:09:27 +02:00
|
|
|
// Ignore error from setting the KeepAlivePeriod as some systems, such as
|
|
|
|
// OpenBSD, do not support setting TCP_USER_TIMEOUT on IPPROTO_TCP
|
|
|
|
_ = c.(*net.TCPConn).SetKeepAlivePeriod(3 * time.Minute)
|
2019-02-01 16:33:56 +02:00
|
|
|
return
|
2017-01-14 04:40:27 +02:00
|
|
|
}
|
2017-01-18 22:17:44 +02:00
|
|
|
|
2020-11-05 06:15:32 +02:00
|
|
|
func newListener(address, network string) (*tcpKeepAliveListener, error) {
|
|
|
|
if network != "tcp" && network != "tcp4" && network != "tcp6" {
|
|
|
|
return nil, ErrInvalidListenerNetwork
|
|
|
|
}
|
|
|
|
l, err := net.Listen(network, address)
|
2017-01-18 22:17:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &tcpKeepAliveListener{l.(*net.TCPListener)}, nil
|
|
|
|
}
|
2019-03-05 18:24:59 +02:00
|
|
|
|
|
|
|
func applyMiddleware(h HandlerFunc, middleware ...MiddlewareFunc) HandlerFunc {
|
|
|
|
for i := len(middleware) - 1; i >= 0; i-- {
|
|
|
|
h = middleware[i](h)
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|