我正在查看一些示例代码,用于在go web服务器上使用autocert。
是否可以通过动态方式实现hostPolicy,即从数据库中读取while列出的主机(因为它们会不断变化)。
m := autocert.Manager{
Cache: certcache,
Prompt: autocert.AcceptTOS,
HostPolicy: hostPolicy,
}对于自定义的hostPolicy实现,框架结构看起来像什么?
https://github.com/golang/crypto/blob/master/acme/autocert/autocert.go#L60
必须返回一个函数吗?
发布于 2018-09-02 04:12:58
是否必须返回函数?
是的,这是autocert.Manager结构签名的一部分。
Manager.HostPolicy字段的类型是autocert.HostPolicy,这实际上是一个func(ctx context.Context, host string) error类型的函数。
自定义hostPolicy实现的框架结构应该是什么样子的?
您只需为HostPolicy字段设置一个自定义函数,以实现查询数据库的逻辑。
m := autocert.Manager{
// ... more fields here
HostPolicy: func(ctx context.Context, host string) error{
// implement database calls here
return nil
},
}根据文档,您应该返回一个错误来拒绝主机。
https://stackoverflow.com/questions/52129908
复制相似问题