Просмотр исходного кода

feat: support Let's Encrypt tls.

Bo-Yi Wu 8 лет назад
Родитель
Сommit
46220b726d
4 измененных файлов с 71 добавлено и 5 удалено
  1. 2 2
      .gitignore
  2. 17 0
      examples/auto-tls/main.go
  3. 31 0
      gin.go
  4. 21 3
      vendor/vendor.json

+ 2 - 2
.gitignore

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

+ 17 - 0
examples/auto-tls/main.go

@@ -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 - 0
gin.go

@@ -5,6 +5,7 @@
 package gin
 
 import (
+	"crypto/tls"
 	"html/template"
 	"net"
 	"net/http"
@@ -12,6 +13,7 @@ import (
 	"sync"
 
 	"github.com/gin-gonic/gin/render"
+	"golang.org/x/crypto/acme/autocert"
 )
 
 // Version is Framework's version
@@ -255,6 +257,35 @@ 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.

+ 21 - 3
vendor/vendor.json

@@ -54,11 +54,29 @@
 			"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",
 			"path": "golang.org/x/net/context",
-			"revision": "d4c55e66d8c3a2f3382d264b08e3e3454a66355a",
-			"revisionTime": "2016-10-18T08:54:36Z"
+			"revision": "ffcf1bedda3b04ebb15a168a59800a73d6dc0f4d",
+			"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=",