Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-04-17 13:41:47 +08:00
parent 2da17294c9
commit f8520b83f9
5 changed files with 104 additions and 58 deletions

View File

@ -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?

View File

@ -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"))
}

View File

@ -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))
}

View File

@ -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")
}

View File

@ -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
}