forked from mirror/gin
fix: remove hard code for cache.
This commit is contained in:
parent
70d0a4c5ba
commit
d17e0a31b8
|
@ -2,16 +2,19 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"golang.org/x/crypto/acme/autocert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
|
gin.AutoTLSManager.Cache = autocert.DirCache("/var/www/.cache")
|
||||||
|
|
||||||
// Ping handler
|
// Ping handler
|
||||||
r.GET("/ping", func(c *gin.Context) {
|
r.GET("/ping", func(c *gin.Context) {
|
||||||
c.String(200, "pong")
|
c.String(200, "pong")
|
||||||
})
|
})
|
||||||
|
|
||||||
// Listen and Server in 0.0.0.0:443
|
// Listen and Server in 0.0.0.0:443
|
||||||
r.RunAutoTLS(":443", "/var/www/.cache", "example.com")
|
r.RunAutoTLS("example.com")
|
||||||
}
|
}
|
||||||
|
|
22
gin1.7.go
22
gin1.7.go
|
@ -9,30 +9,26 @@ import (
|
||||||
"golang.org/x/crypto/acme/autocert"
|
"golang.org/x/crypto/acme/autocert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var AutoTLSManager = autocert.Manager{
|
||||||
|
Prompt: autocert.AcceptTOS,
|
||||||
|
}
|
||||||
|
|
||||||
// RunAutoTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
|
// RunAutoTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
|
||||||
// It obtains and refreshes certificates automatically,
|
// It obtains and refreshes certificates automatically,
|
||||||
// as well as providing them to a TLS server via tls.Config.
|
// as well as providing them to a TLS server via tls.Config.
|
||||||
// only from Go version 1.7 onward
|
// only from Go version 1.7 onward
|
||||||
func (engine *Engine) RunAutoTLS(addr string, cache string, domain ...string) (err error) {
|
func (engine *Engine) RunAutoTLS(domain ...string) (err error) {
|
||||||
debugPrint("Listening and serving HTTPS on %s and host name is %s\n", addr, domain)
|
debugPrint("Listening and serving HTTPS on host name is %s\n", domain)
|
||||||
defer func() { debugPrintError(err) }()
|
defer func() { debugPrintError(err) }()
|
||||||
m := autocert.Manager{
|
|
||||||
Prompt: autocert.AcceptTOS,
|
|
||||||
}
|
|
||||||
|
|
||||||
//your domain here
|
//your domain here
|
||||||
if len(domain) != 0 {
|
if len(domain) != 0 {
|
||||||
m.HostPolicy = autocert.HostWhitelist(domain...)
|
AutoTLSManager.HostPolicy = autocert.HostWhitelist(domain...)
|
||||||
}
|
|
||||||
|
|
||||||
// folder for storing certificates
|
|
||||||
if cache != "" {
|
|
||||||
m.Cache = autocert.DirCache(cache)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Addr: addr,
|
Addr: ":443",
|
||||||
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
|
TLSConfig: &tls.Config{GetCertificate: AutoTLSManager.GetCertificate},
|
||||||
Handler: engine,
|
Handler: engine,
|
||||||
}
|
}
|
||||||
err = s.ListenAndServeTLS("", "")
|
err = s.ListenAndServeTLS("", "")
|
||||||
|
|
Loading…
Reference in New Issue