fix: build only from Go version 1.7 onward

This commit is contained in:
Bo-Yi Wu 2017-04-03 22:51:56 +08:00
parent 46220b726d
commit 70d0a4c5ba
2 changed files with 40 additions and 31 deletions

31
gin.go
View File

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

40
gin1.7.go Normal file
View File

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