diff --git a/.gitignore b/.gitignore index 9f48f142..f3b636df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -Godeps/* -!Godeps/Godeps.json +vendor/* +!vendor/vendor.json coverage.out count.out diff --git a/examples/auto-tls/main.go b/examples/auto-tls/main.go new file mode 100644 index 00000000..2c0d89d9 --- /dev/null +++ b/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") +} diff --git a/gin.go b/gin.go index 61ac5c00..4a3a8332 100644 --- a/gin.go +++ b/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. diff --git a/vendor/vendor.json b/vendor/vendor.json index 2bc5e194..e3b3e91b 100644 --- a/vendor/vendor.json +++ b/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=",