mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
parent
0349e883f5
commit
152be1aaff
50
echo.go
50
echo.go
@ -51,6 +51,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"rsc.io/letsencrypt"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"golang.org/x/net/websocket"
|
||||
|
||||
@ -65,6 +67,8 @@ type (
|
||||
Server *http.Server
|
||||
TLSServer *http.Server
|
||||
TLSConfig *tls.Config
|
||||
TLSCacheFile string
|
||||
TLSHosts []string
|
||||
ShutdownTimeout time.Duration
|
||||
DisableHTTP2 bool
|
||||
Debug bool
|
||||
@ -72,14 +76,16 @@ type (
|
||||
Binder Binder
|
||||
Renderer Renderer
|
||||
Logger Logger
|
||||
tlsManager letsencrypt.Manager
|
||||
graceful *graceful.Server
|
||||
gracefulTLS *graceful.Server
|
||||
premiddleware []MiddlewareFunc
|
||||
middleware []MiddlewareFunc
|
||||
maxParam *int
|
||||
router *Router
|
||||
notFoundHandler HandlerFunc
|
||||
pool sync.Pool
|
||||
router *Router
|
||||
color *color.Color
|
||||
}
|
||||
|
||||
// Route contains a handler and information for matching against requests.
|
||||
@ -230,17 +236,20 @@ var (
|
||||
)
|
||||
|
||||
// New creates an instance of Echo.
|
||||
|
||||
func New() (e *Echo) {
|
||||
e = &Echo{
|
||||
Server: new(http.Server),
|
||||
TLSServer: new(http.Server),
|
||||
// TODO: https://github.com/golang/go/commit/d24f446a90ea94b87591bf16228d7d871fec3d92
|
||||
TLSConfig: new(tls.Config),
|
||||
TLSCacheFile: "letsencrypt.cache",
|
||||
ShutdownTimeout: 15 * time.Second,
|
||||
Logger: glog.New("echo"),
|
||||
graceful: new(graceful.Server),
|
||||
gracefulTLS: new(graceful.Server),
|
||||
maxParam: new(int),
|
||||
color: color.New(),
|
||||
}
|
||||
e.HTTPErrorHandler = e.DefaultHTTPErrorHandler
|
||||
e.Binder = &binder{}
|
||||
@ -505,32 +514,39 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
e.pool.Put(c)
|
||||
}
|
||||
|
||||
func (e *Echo) configGraceful(gs *graceful.Server, s *http.Server, address string) {
|
||||
func (e *Echo) config(gs *graceful.Server, s *http.Server, address string) {
|
||||
e.color.SetOutput(e.Logger.Output())
|
||||
gs.Server = s
|
||||
gs.Addr = address
|
||||
gs.Handler = e
|
||||
gs.Timeout = e.ShutdownTimeout
|
||||
gs.Logger = slog.New(e.Logger.Output(), e.Logger.Prefix()+": ", 0)
|
||||
if gs == e.gracefulTLS && !e.DisableHTTP2 {
|
||||
e.TLSConfig.NextProtos = append(e.TLSConfig.NextProtos, "h2")
|
||||
}
|
||||
// Global log
|
||||
slog.SetOutput(e.Logger.Output())
|
||||
slog.SetPrefix(e.Logger.Prefix() + ": ")
|
||||
slog.SetFlags(0)
|
||||
}
|
||||
|
||||
// Start starts the HTTP server.
|
||||
// Note: If custom `Echo#Server` is used, it's Addr and Handler properties are ignored.
|
||||
// Note: If custom `Echo#Server` is used, it's Addr and Handler properties are
|
||||
// ignored.
|
||||
func (e *Echo) Start(address string) (err error) {
|
||||
e.configGraceful(e.graceful, e.Server, address)
|
||||
e.config(e.graceful, e.Server, address)
|
||||
color.Printf(" ⇛ http server started on %s\n", color.Green(address))
|
||||
return e.graceful.ListenAndServe()
|
||||
}
|
||||
|
||||
// StartTLS starts the TLS server.
|
||||
// Note: If custom `Echo#TLSServer` is used, it's Addr and Handler properties are ignored.
|
||||
// Note: If custom `Echo#TLSServer` is used, it's Addr and Handler properties are
|
||||
// ignored.
|
||||
func (e *Echo) StartTLS(address string, certFile, keyFile string) (err error) {
|
||||
e.configGraceful(e.gracefulTLS, e.TLSServer, address)
|
||||
e.config(e.gracefulTLS, e.TLSServer, address)
|
||||
if certFile == "" || keyFile == "" {
|
||||
return errors.New("invalid tls configuration")
|
||||
}
|
||||
if !e.DisableHTTP2 {
|
||||
e.TLSConfig.NextProtos = append(e.TLSConfig.NextProtos, "h2")
|
||||
}
|
||||
e.TLSConfig.Certificates = make([]tls.Certificate, 1)
|
||||
e.TLSConfig.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
|
||||
if err != nil {
|
||||
@ -540,6 +556,22 @@ func (e *Echo) StartTLS(address string, certFile, keyFile string) (err error) {
|
||||
return e.gracefulTLS.ListenAndServeTLSConfig(e.TLSConfig)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Shutdown gracefully shutdown the HTTP server with timeout.
|
||||
func (e *Echo) Shutdown(timeout time.Duration) {
|
||||
e.graceful.Stop(timeout)
|
||||
|
30
glide.lock
generated
30
glide.lock
generated
@ -1,10 +1,10 @@
|
||||
hash: 9b52073f044ab24c64a0f01e3efef2aabdeb7ca21abc9129d2ee7a0c995e4412
|
||||
updated: 2016-09-25T11:05:56.886149316-07:00
|
||||
hash: bc7c67661e65a599e3ba58ed94c913571971be4acdb9f6c4238d666fc71caa8c
|
||||
updated: 2016-09-25T16:36:18.128445429-07:00
|
||||
imports:
|
||||
- name: github.com/dgrijalva/jwt-go
|
||||
version: 24c63f56522a87ec5339cc3567883f1039378fdb
|
||||
- name: github.com/labstack/gommon
|
||||
version: 0e66403a8c305a543b9a53c21778a28536f836c0
|
||||
version: f3b1a1b3bd4726161e1200863d278df7da5e66ff
|
||||
subpackages:
|
||||
- bytes
|
||||
- color
|
||||
@ -14,8 +14,10 @@ imports:
|
||||
version: ed8eb9e318d7a84ce5915b495b7d35e0cfe7b5a8
|
||||
- name: github.com/mattn/go-isatty
|
||||
version: 66b8e73f3f5cda9f96b69efd03dd3d7fc4a5cdb8
|
||||
- name: github.com/miekg/dns
|
||||
version: db96a2b759cdef4f11a34506a42eb8d1290c598e
|
||||
- name: github.com/stretchr/testify
|
||||
version: 69483b4bd14f5845b5a1e55bca19e954e827f1d0
|
||||
version: 976c720a22c8eb4eb6a0b4348ad85ad12491a506
|
||||
subpackages:
|
||||
- assert
|
||||
- name: github.com/tylerb/graceful
|
||||
@ -24,17 +26,37 @@ imports:
|
||||
version: d03a22720925ae6fd6f81db717d9288fc2771d79
|
||||
- name: github.com/valyala/fasttemplate
|
||||
version: 3b874956e03f1636d171bda64b130f9135f42cff
|
||||
- name: github.com/xenolf/lego
|
||||
version: 82ac43327b01319544c050d5d78a4edeff9565d2
|
||||
subpackages:
|
||||
- acme
|
||||
- name: golang.org/x/crypto
|
||||
version: 8e06e8ddd9629eb88639aba897641bff8031f1d3
|
||||
subpackages:
|
||||
- ocsp
|
||||
- name: golang.org/x/net
|
||||
version: f09c4662a0bd6bd8943ac7b4931e185df9471da4
|
||||
subpackages:
|
||||
- context
|
||||
- publicsuffix
|
||||
- websocket
|
||||
- name: golang.org/x/sys
|
||||
version: 8f0908ab3b2457e2e15403d3697c9ef5cb4b57a9
|
||||
subpackages:
|
||||
- unix
|
||||
- name: golang.org/x/time
|
||||
version: 2bc1b4fbcc7c3eda9a26f6e523cac1196c6fecd7
|
||||
subpackages:
|
||||
- rate
|
||||
- name: gopkg.in/square/go-jose.v1
|
||||
version: aa2e30fdd1fe9dd3394119af66451ae790d50e0d
|
||||
subpackages:
|
||||
- cipher
|
||||
- json
|
||||
- name: gopkg.in/tylerb/graceful.v1
|
||||
version: 50a48b6e73fcc75b45e22c05b79629a67c79e938
|
||||
- name: rsc.io/letsencrypt
|
||||
version: a18c646c3d0772313b7b53c78847bb3eb6463f0b
|
||||
testImports:
|
||||
- name: github.com/davecgh/go-spew
|
||||
version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9
|
||||
|
@ -17,3 +17,4 @@ import:
|
||||
subpackages:
|
||||
- context
|
||||
- package: gopkg.in/tylerb/graceful.v1
|
||||
- package: rsc.io/letsencrypt
|
||||
|
Loading…
Reference in New Issue
Block a user