diff --git a/gin.go b/gin.go index 4a3a833..61ac5c0 100644 --- a/gin.go +++ b/gin.go @@ -5,7 +5,6 @@ package gin import ( - "crypto/tls" "html/template" "net" "net/http" @@ -13,7 +12,6 @@ import ( "sync" "github.com/gin-gonic/gin/render" - "golang.org/x/crypto/acme/autocert" ) // Version is Framework's version @@ -257,35 +255,6 @@ func (engine *Engine) RunTLS(addr string, certFile string, keyFile string) (err 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 // through the specified unix socket (ie. a file). // Note: this method will block the calling goroutine indefinitely unless an error happens. diff --git a/gin1.7.go b/gin1.7.go new file mode 100644 index 0000000..dd8a65a --- /dev/null +++ b/gin1.7.go @@ -0,0 +1,40 @@ +// +build go1.7 + +package gin + +import ( + "crypto/tls" + "net/http" + + "golang.org/x/crypto/acme/autocert" +) + +// 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(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 +}