feat: create DNSProvider using independent config instead of envvar

This commit is contained in:
Fu Diwei
2024-12-23 19:58:51 +08:00
parent 9ef16ebcf9
commit 586c7fa927
14 changed files with 249 additions and 216 deletions

View File

@@ -2,35 +2,37 @@ package applicant
import (
"encoding/json"
"fmt"
"os"
"time"
cf "github.com/go-acme/lego/v4/providers/dns/cloudflare"
"github.com/go-acme/lego/v4/providers/dns/cloudflare"
"github.com/usual2970/certimate/internal/domain"
)
type cloudflare struct {
type cloudflareApplicant struct {
option *ApplyOption
}
func NewCloudflare(option *ApplyOption) Applicant {
return &cloudflare{
func NewCloudflareApplicant(option *ApplyOption) Applicant {
return &cloudflareApplicant{
option: option,
}
}
func (c *cloudflare) Apply() (*Certificate, error) {
func (a *cloudflareApplicant) Apply() (*Certificate, error) {
access := &domain.CloudflareAccess{}
json.Unmarshal([]byte(c.option.Access), access)
json.Unmarshal([]byte(a.option.Access), access)
os.Setenv("CLOUDFLARE_DNS_API_TOKEN", access.DnsApiToken)
os.Setenv("CLOUDFLARE_PROPAGATION_TIMEOUT", fmt.Sprintf("%d", c.option.Timeout))
config := cloudflare.NewDefaultConfig()
config.AuthToken = access.DnsApiToken
if a.option.Timeout != 0 {
config.PropagationTimeout = time.Duration(a.option.Timeout) * time.Second
}
provider, err := cf.NewDNSProvider()
provider, err := cloudflare.NewDNSProviderConfig(config)
if err != nil {
return nil, err
}
return apply(c.option, provider)
return apply(a.option, provider)
}