登录
首页 >  Golang >  Go问答

可用 autocert 创制动态主机政策吗?

来源:stackoverflow

时间:2024-03-24 14:00:46 259浏览 收藏

在 Go web 服务器上使用 autocert 时,是否可以动态实现 hostpolicy,即从数据库中读取主机列表?autocert.Manager 结构的 HostPolicy 字段类型为 func(ctx context.Context, host string) error 的函数。您可以设置一个自定义函数来实现从数据库查询的逻辑,如果需要拒绝主机,请返回错误。

问题内容

我正在查看一些在 go web 服务器上使用 autocert 的示例代码。

hostpolicy 实现是否可以动态实现,即从数据库中读取同时列出的主机(因为它们会不断变化)。

m := autocert.Manager{
        Cache:      certcache,
        Prompt:     autocert.AcceptTOS,
        HostPolicy: hostPolicy,
    }

自定义 hostpolicy 实现的骨架结构会是什么样子?

https://github.com/golang/crypto/blob/master/acme/autocert/autocert.go#l60

是否必须返回一个函数?


解决方案


是的,这是 autocert.Manager 结构体签名的一部分。

Manager.HostPolicy 字段的类型为 autocert.HostPolicy,这实际上是 func(ctx context.context, host string) error 类型的函数。

您只需为 hostpolicy 字段设置自定义函数即可实现查询数据库的逻辑。

m := autocert.Manager{
    // ... more fields here
    HostPolicy: func(ctx context.Context, host string) error{
        // implement database calls here
        return nil 
    },
}

根据文档,您应该返回错误来拒绝主机。

今天关于《可用 autocert 创制动态主机政策吗?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>