gin1.7.go 911 B

123456789101112131415161718192021222324252627282930313233343536
  1. // +build go1.7
  2. package gin
  3. import (
  4. "crypto/tls"
  5. "net/http"
  6. "golang.org/x/crypto/acme/autocert"
  7. )
  8. var AutoTLSManager = autocert.Manager{
  9. Prompt: autocert.AcceptTOS,
  10. }
  11. // RunAutoTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
  12. // It obtains and refreshes certificates automatically,
  13. // as well as providing them to a TLS server via tls.Config.
  14. // only from Go version 1.7 onward
  15. func (engine *Engine) RunAutoTLS(domain ...string) (err error) {
  16. debugPrint("Listening and serving HTTPS on host name is %s\n", domain)
  17. defer func() { debugPrintError(err) }()
  18. //your domain here
  19. if len(domain) != 0 {
  20. AutoTLSManager.HostPolicy = autocert.HostWhitelist(domain...)
  21. }
  22. s := &http.Server{
  23. Addr: ":443",
  24. TLSConfig: &tls.Config{GetCertificate: AutoTLSManager.GetCertificate},
  25. Handler: engine,
  26. }
  27. err = s.ListenAndServeTLS("", "")
  28. return
  29. }