2017-04-03 17:51:56 +03:00
|
|
|
// +build go1.7
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/acme/autocert"
|
|
|
|
)
|
|
|
|
|
2017-04-03 18:30:46 +03:00
|
|
|
var AutoTLSManager = autocert.Manager{
|
|
|
|
Prompt: autocert.AcceptTOS,
|
|
|
|
}
|
|
|
|
|
2017-04-03 17:51:56 +03:00
|
|
|
// 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
|
2017-04-03 18:30:46 +03:00
|
|
|
func (engine *Engine) RunAutoTLS(domain ...string) (err error) {
|
|
|
|
debugPrint("Listening and serving HTTPS on host name is %s\n", domain)
|
2017-04-03 17:51:56 +03:00
|
|
|
defer func() { debugPrintError(err) }()
|
|
|
|
|
|
|
|
//your domain here
|
|
|
|
if len(domain) != 0 {
|
2017-04-03 18:30:46 +03:00
|
|
|
AutoTLSManager.HostPolicy = autocert.HostWhitelist(domain...)
|
2017-04-03 17:51:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s := &http.Server{
|
2017-04-03 18:30:46 +03:00
|
|
|
Addr: ":443",
|
|
|
|
TLSConfig: &tls.Config{GetCertificate: AutoTLSManager.GetCertificate},
|
2017-04-03 17:51:56 +03:00
|
|
|
Handler: engine,
|
|
|
|
}
|
|
|
|
err = s.ListenAndServeTLS("", "")
|
|
|
|
return
|
|
|
|
}
|