Skip to content

Commit

Permalink
feat: Add support for Cloudflare API token authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
mac-zhou authored and we11adam committed Jun 13, 2024
1 parent c72d2a3 commit 0b944b9
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions updater/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package cloudflare

import (
"context"
"log/slog"
"strings"

"github.com/cloudflare/cloudflare-go"
"github.com/spf13/viper"
"github.com/we11adam/uddns/updater"
"log/slog"
"strings"
)

type Config struct {
Email string `mapstructure:"email"`
APIKey string `mapstructure:"apikey"`
Domain string `mapstructure:"domain"`
Email string `mapstructure:"email"`
APIKey string `mapstructure:"apikey"`
APIToken string `mapstructure:"apitoken"`
Domain string `mapstructure:"domain"`
}

type Cloudflare struct {
Expand All @@ -32,7 +34,20 @@ func init() {
}

func New(config *Config) (updater.Updater, error) {
api, err := cloudflare.New(config.APIKey, config.Email)
var (
api *cloudflare.API
err error
)

// If APIToken is provided, use it to create the API client
if config.APIToken != "" {
api, err = cloudflare.NewWithAPIToken(config.APIToken)
} else {
// Otherwise, use APIKey and Email to create the API client
api, err = cloudflare.New(config.APIKey, config.Email)
}

// Check if there was an error creating the API client
if err != nil {
slog.Debug("[CloudFlare] failed to create API client:", "error", err)
return nil, err
Expand Down

0 comments on commit 0b944b9

Please sign in to comment.