首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用gorilla mux进行自动认证

使用gorilla mux进行自动认证
EN

Stack Overflow用户
提问于 2018-05-13 07:37:42
回答 1查看 1.1K关注 0票数 3

我想使用autocert和gorila mux生成证书,我的实际代码是:

代码语言:javascript
复制
func main() {
    certManager := autocert.Manager{
        Prompt:     autocert.AcceptTOS,
        //HostPolicy: autocert.HostWhitelist("example.com"),
        Cache:      autocert.DirCache("./certs"),            //Folder for storing certificates
    }

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello world"))
    })

    server := &http.Server{
        Addr:      ":https",
        TLSConfig: &tls.Config{
            GetCertificate: certManager.GetCertificate,
        },
    }

    go http.ListenAndServe(":http", certManager.HTTPHandler(nil))

    log.Fatal(server.ListenAndServeTLS("", "")) //Key and cert are coming from Let's Encrypt
}

我的路由器是:

代码语言:javascript
复制
r := mux.NewRouter()
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
r.HandleFunc("/login.html",  LoginHtml)

如何在我的实际代码中集成gurilla mux?

EN

回答 1

Stack Overflow用户

发布于 2019-02-26 17:25:40

集成将如下所示:

代码语言:javascript
复制
func main() {
    certManager := autocert.Manager{
        Prompt:     autocert.AcceptTOS,
        //HostPolicy: autocert.HostWhitelist("example.com"),
        Cache:      autocert.DirCache("./certs"),            //Folder for storing certificates
    }

    r := mux.NewRouter()
    r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
    r.HandleFunc("/login.html",  LoginHtml)

    server := &http.Server{
        Addr:      ":https",
        Handler:   r,
        TLSConfig: &tls.Config{
            GetCertificate: certManager.GetCertificate,
        },
    }

    go http.ListenAndServe(":http", certManager.HTTPHandler(nil))

    log.Fatal(server.ListenAndServeTLS("", "")) //Key and cert are coming from Let's Encrypt
}

我建议向服务器添加一些超时。我通常去读,写和闲置。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50311532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档