mirror of
https://github.com/labstack/echo.git
synced 2025-07-03 00:56:59 +02:00
38
echo.go
38
echo.go
@ -15,6 +15,7 @@ import (
|
|||||||
|
|
||||||
"github.com/mattn/go-colorable"
|
"github.com/mattn/go-colorable"
|
||||||
"golang.org/x/net/websocket"
|
"golang.org/x/net/websocket"
|
||||||
|
"github.com/bradfitz/http2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -22,6 +23,7 @@ type (
|
|||||||
Router *router
|
Router *router
|
||||||
prefix string
|
prefix string
|
||||||
middleware []MiddlewareFunc
|
middleware []MiddlewareFunc
|
||||||
|
http2 bool
|
||||||
maxParam byte
|
maxParam byte
|
||||||
notFoundHandler HandlerFunc
|
notFoundHandler HandlerFunc
|
||||||
httpErrorHandler HTTPErrorHandler
|
httpErrorHandler HTTPErrorHandler
|
||||||
@ -146,6 +148,7 @@ func New() (e *Echo) {
|
|||||||
// Defaults
|
// Defaults
|
||||||
//----------
|
//----------
|
||||||
|
|
||||||
|
e.HTTP2(true)
|
||||||
e.SetMaxParam(5)
|
e.SetMaxParam(5)
|
||||||
e.notFoundHandler = func(c *Context) error {
|
e.notFoundHandler = func(c *Context) error {
|
||||||
return NewHTTPError(http.StatusNotFound)
|
return NewHTTPError(http.StatusNotFound)
|
||||||
@ -187,6 +190,11 @@ func (e *Echo) Group(pfx string, m ...Middleware) *Echo {
|
|||||||
return &g
|
return &g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTP2 enables HTTP2 support.
|
||||||
|
func (e *Echo) HTTP2(on bool) {
|
||||||
|
e.http2 = on
|
||||||
|
}
|
||||||
|
|
||||||
// SetMaxParam sets the maximum number of path parameters allowed for the application.
|
// SetMaxParam sets the maximum number of path parameters allowed for the application.
|
||||||
// Default value is 5, good enough for many use cases.
|
// Default value is 5, good enough for many use cases.
|
||||||
func (e *Echo) SetMaxParam(n uint8) {
|
func (e *Echo) SetMaxParam(n uint8) {
|
||||||
@ -371,24 +379,38 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// Run runs a server.
|
// Run runs a server.
|
||||||
func (e *Echo) Run(addr string) {
|
func (e *Echo) Run(addr string) {
|
||||||
log.Fatal(http.ListenAndServe(addr, e))
|
s := &http.Server{Addr: addr}
|
||||||
|
e.run(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunTLS runs a server with TLS configuration.
|
// RunTLS runs a server with TLS configuration.
|
||||||
func (e *Echo) RunTLS(addr, certFile, keyFile string) {
|
func (e *Echo) RunTLS(addr, certFile, keyFile string) {
|
||||||
log.Fatal(http.ListenAndServeTLS(addr, certFile, keyFile, e))
|
s := &http.Server{Addr: addr}
|
||||||
|
e.run(s, certFile, keyFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunServer runs a custom server.
|
// RunServer runs a custom server.
|
||||||
func (e *Echo) RunServer(server *http.Server) {
|
func (e *Echo) RunServer(srv *http.Server) {
|
||||||
server.Handler = e
|
e.run(srv)
|
||||||
log.Fatal(server.ListenAndServe())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunTLSServer runs a custom server with TLS configuration.
|
// RunTLSServer runs a custom server with TLS configuration.
|
||||||
func (e *Echo) RunTLSServer(server *http.Server, certFile, keyFile string) {
|
func (e *Echo) RunTLSServer(srv *http.Server, certFile, keyFile string) {
|
||||||
server.Handler = e
|
e.run(srv, certFile, keyFile)
|
||||||
log.Fatal(server.ListenAndServeTLS(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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// wraps middleware
|
// wraps middleware
|
||||||
|
Reference in New Issue
Block a user