From f8520b83f912848dea56595ca7ec2f652a72b4ee Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 17 Apr 2017 13:41:47 +0800 Subject: [PATCH] update Signed-off-by: Bo-Yi Wu --- README.md | 59 +++++++++++++++++++++++++++++++++++ examples/auto-tls/example1.go | 19 +++++++++++ examples/auto-tls/example2.go | 26 +++++++++++++++ examples/auto-tls/main.go | 21 ------------- gin1.7.go | 37 ---------------------- 5 files changed, 104 insertions(+), 58 deletions(-) create mode 100644 examples/auto-tls/example1.go create mode 100644 examples/auto-tls/example2.go delete mode 100644 examples/auto-tls/main.go delete mode 100644 gin1.7.go diff --git a/README.md b/README.md index 883f9d50..535ee4eb 100644 --- a/README.md +++ b/README.md @@ -777,6 +777,65 @@ func main() { } ``` +### Support Let's Encrypt + +example for 1-line LetsEncrypt HTTPS servers. + +[embedmd]:# (examples/auto-tls/example1.go go) +```go +package main + +import ( + "log" + + "github.com/gin-gonic/autotls" + "github.com/gin-gonic/gin" +) + +func main() { + r := gin.Default() + + // Ping handler + r.GET("/ping", func(c *gin.Context) { + c.String(200, "pong") + }) + + log.Fatal(autotls.Run(r, "example1.com", "example2.com")) +} +``` + +example for custom autocert manager. + +[embedmd]:# (examples/auto-tls/example2.go go) +```go +package main + +import ( + "log" + + "github.com/gin-gonic/autotls" + "github.com/gin-gonic/gin" + "golang.org/x/crypto/acme/autocert" +) + +func main() { + r := gin.Default() + + // Ping handler + r.GET("/ping", func(c *gin.Context) { + c.String(200, "pong") + }) + + m := autocert.Manager{ + Prompt: autocert.AcceptTOS, + HostPolicy: autocert.HostWhitelist("example1.com", "example2.com"), + Cache: autocert.DirCache("/var/www/.cache"), + } + + log.Fatal(autotls.RunWithManager(r, m)) +} +``` + ### Graceful restart or stop Do you want to graceful restart or stop your web server? diff --git a/examples/auto-tls/example1.go b/examples/auto-tls/example1.go new file mode 100644 index 00000000..fa9f4008 --- /dev/null +++ b/examples/auto-tls/example1.go @@ -0,0 +1,19 @@ +package main + +import ( + "log" + + "github.com/gin-gonic/autotls" + "github.com/gin-gonic/gin" +) + +func main() { + r := gin.Default() + + // Ping handler + r.GET("/ping", func(c *gin.Context) { + c.String(200, "pong") + }) + + log.Fatal(autotls.Run(r, "example1.com", "example2.com")) +} diff --git a/examples/auto-tls/example2.go b/examples/auto-tls/example2.go new file mode 100644 index 00000000..ab8b81e7 --- /dev/null +++ b/examples/auto-tls/example2.go @@ -0,0 +1,26 @@ +package main + +import ( + "log" + + "github.com/gin-gonic/autotls" + "github.com/gin-gonic/gin" + "golang.org/x/crypto/acme/autocert" +) + +func main() { + r := gin.Default() + + // Ping handler + r.GET("/ping", func(c *gin.Context) { + c.String(200, "pong") + }) + + m := autocert.Manager{ + Prompt: autocert.AcceptTOS, + HostPolicy: autocert.HostWhitelist("example1.com", "example2.com"), + Cache: autocert.DirCache("/var/www/.cache"), + } + + log.Fatal(autotls.RunWithManager(r, m)) +} diff --git a/examples/auto-tls/main.go b/examples/auto-tls/main.go deleted file mode 100644 index 49544c52..00000000 --- a/examples/auto-tls/main.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "github.com/gin-gonic/gin" - "golang.org/x/crypto/acme/autocert" -) - -func main() { - r := gin.Default() - - // folder for storing certificates - gin.AutoTLSManager.Cache = autocert.DirCache("/var/www/.cache") - - // Ping handler - r.GET("/ping", func(c *gin.Context) { - c.String(200, "pong") - }) - - // Listen and Server in 0.0.0.0:443 - r.RunAutoTLS("example1.com", "example2.com") -} diff --git a/gin1.7.go b/gin1.7.go deleted file mode 100644 index 919c80f3..00000000 --- a/gin1.7.go +++ /dev/null @@ -1,37 +0,0 @@ -// +build go1.7 - -package gin - -import ( - "crypto/tls" - "net/http" - - "golang.org/x/crypto/acme/autocert" -) - -// AutoTLSManager is a stateful certificate manager built on top of acme.Client. -var AutoTLSManager = autocert.Manager{ - Prompt: autocert.AcceptTOS, -} - -// RunAutoTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests. -// It obtains and refreshes certificates automatically, -// as well as providing them to a TLS server via tls.Config. -// only from Go version 1.7 onward -func (engine *Engine) RunAutoTLS(domain ...string) (err error) { - debugPrint("Listening and serving HTTPS on host name is %s\n", domain) - defer func() { debugPrintError(err) }() - - // HostPolicy controls which domains the Manager will attempt - if len(domain) != 0 { - AutoTLSManager.HostPolicy = autocert.HostWhitelist(domain...) - } - - s := &http.Server{ - Addr: ":https", - TLSConfig: &tls.Config{GetCertificate: AutoTLSManager.GetCertificate}, - Handler: engine, - } - err = s.ListenAndServeTLS("", "") - return -}