• 支持 Let's Encrypt

    支持 Let's Encrypt

    一行代码支持 LetsEncrypt HTTPS servers 示例。

    1. package main
    2. import (
    3. "log"
    4. "github.com/gin-gonic/autotls"
    5. "github.com/gin-gonic/gin"
    6. )
    7. func main() {
    8. r := gin.Default()
    9. // Ping handler
    10. r.GET("/ping", func(c *gin.Context) {
    11. c.String(200, "pong")
    12. })
    13. log.Fatal(autotls.Run(r, "example1.com", "example2.com"))
    14. }

    自定义 autocert manager 示例。

    1. package main
    2. import (
    3. "log"
    4. "github.com/gin-gonic/autotls"
    5. "github.com/gin-gonic/gin"
    6. "golang.org/x/crypto/acme/autocert"
    7. )
    8. func main() {
    9. r := gin.Default()
    10. // Ping handler
    11. r.GET("/ping", func(c *gin.Context) {
    12. c.String(200, "pong")
    13. })
    14. m := autocert.Manager{
    15. Prompt: autocert.AcceptTOS,
    16. HostPolicy: autocert.HostWhitelist("example1.com", "example2.com"),
    17. Cache: autocert.DirCache("/var/www/.cache"),
    18. }
    19. log.Fatal(autotls.RunWithManager(r, &m))
    20. }