feat: support Let's Encrypt tls.

This commit is contained in:
Bo-Yi Wu 2017-04-03 21:23:45 +08:00
parent 41316b9ca9
commit 46220b726d
4 changed files with 71 additions and 5 deletions

4
.gitignore vendored
View File

@ -1,4 +1,4 @@
Godeps/* vendor/*
!Godeps/Godeps.json !vendor/vendor.json
coverage.out coverage.out
count.out count.out

17
examples/auto-tls/main.go Normal file
View File

@ -0,0 +1,17 @@
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
// Ping handler
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
// Listen and Server in 0.0.0.0:443
r.RunAutoTLS(":443", "/var/www/.cache", "example.com")
}

31
gin.go
View File

@ -5,6 +5,7 @@
package gin package gin
import ( import (
"crypto/tls"
"html/template" "html/template"
"net" "net"
"net/http" "net/http"
@ -12,6 +13,7 @@ import (
"sync" "sync"
"github.com/gin-gonic/gin/render" "github.com/gin-gonic/gin/render"
"golang.org/x/crypto/acme/autocert"
) )
// Version is Framework's version // Version is Framework's version
@ -255,6 +257,35 @@ func (engine *Engine) RunTLS(addr string, certFile string, keyFile string) (err
return return
} }
// 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.
func (engine *Engine) RunAutoTLS(addr string, cache string, domain ...string) (err error) {
debugPrint("Listening and serving HTTPS on %s and host name is %s\n", addr, domain)
defer func() { debugPrintError(err) }()
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
}
//your domain here
if len(domain) != 0 {
m.HostPolicy = autocert.HostWhitelist(domain...)
}
// folder for storing certificates
if cache != "" {
m.Cache = autocert.DirCache(cache)
}
s := &http.Server{
Addr: addr,
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
Handler: engine,
}
err = s.ListenAndServeTLS("", "")
return
}
// RunUnix attaches the router to a http.Server and starts listening and serving HTTP requests // RunUnix attaches the router to a http.Server and starts listening and serving HTTP requests
// through the specified unix socket (ie. a file). // through the specified unix socket (ie. a file).
// Note: this method will block the calling goroutine indefinitely unless an error happens. // Note: this method will block the calling goroutine indefinitely unless an error happens.

24
vendor/vendor.json vendored
View File

@ -54,11 +54,29 @@
"revisionTime": "2017-02-15T20:11:44Z" "revisionTime": "2017-02-15T20:11:44Z"
}, },
{ {
"checksumSHA1": "9jjO5GjLa0XF/nfWihF02RoH4qc=", "checksumSHA1": "didOyrMN69DzlBd+BPSC28G2YG0=",
"path": "golang.org/x/crypto/acme",
"revision": "88915ccf7aeb91e9324fe7cf3eddd1531ced61ea",
"revisionTime": "2017-04-02T20:18:05Z"
},
{
"checksumSHA1": "yfiamzDHcZXb6irWt7DfHVxCs44=",
"path": "golang.org/x/crypto/acme/autocert",
"revision": "88915ccf7aeb91e9324fe7cf3eddd1531ced61ea",
"revisionTime": "2017-04-02T20:18:05Z"
},
{
"checksumSHA1": "Y+HGqEkYM15ir+J93MEaHdyFy0c=",
"comment": "release-branch.go1.7", "comment": "release-branch.go1.7",
"path": "golang.org/x/net/context", "path": "golang.org/x/net/context",
"revision": "d4c55e66d8c3a2f3382d264b08e3e3454a66355a", "revision": "ffcf1bedda3b04ebb15a168a59800a73d6dc0f4d",
"revisionTime": "2016-10-18T08:54:36Z" "revisionTime": "2017-03-29T01:43:45Z"
},
{
"checksumSHA1": "WHc3uByvGaMcnSoI21fhzYgbOgg=",
"path": "golang.org/x/net/context/ctxhttp",
"revision": "ffcf1bedda3b04ebb15a168a59800a73d6dc0f4d",
"revisionTime": "2017-03-29T01:43:45Z"
}, },
{ {
"checksumSHA1": "/oZpHfYc+ZgOwYAhlvcMhmETYpw=", "checksumSHA1": "/oZpHfYc+ZgOwYAhlvcMhmETYpw=",