diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 7deb41e..509f655 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -16,7 +16,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.21 + go-version: 1.23 - name: Build run: make build diff --git a/cmd/ddns/main.go b/cmd/ddns/main.go index be32a66..a098eda 100644 --- a/cmd/ddns/main.go +++ b/cmd/ddns/main.go @@ -2,142 +2,21 @@ package main import ( "context" - "fmt" "log/slog" "os" - "strings" - "time" - "github.com/libdns/libdns" - "github.com/orvice/ddns/dns" - "github.com/orvice/ddns/internal/config" - "github.com/orvice/ddns/internal/ip" - "github.com/orvice/ddns/notify" -) - -var ( - dnsProvider dns.LibDNS - ipGetter ip.Getter - DNSMode string + "github.com/orvice/ddns/internal/wire" ) var ( IPNotifyFormat = "[%s] ip changed, old IP: %s new IP: %s" ) -func Init() error { - logger := slog.Default() - var err error - err = config.Init() - if err != nil { - return err - } - ipGetter = ip.NewIfconfigCo() - - notify.Init() - - notifier, err := notify.NewTelegramNotifier(config.GetConfig().TelegramToken, config.GetConfig().TelegramChatID) - if err != nil { - logger.Error("notify init error", "error", err) - } else { - notify.AddNotifier(notifier) - } - - dnsProvider = dns.New(config.GetConfig()) - - return nil -} - func main() { - logger := slog.Default() - err := Init() + app, err := wire.NewApp() if err != nil { - logger.Error("init error", "error", err) - os.Exit(0) - } - - ctx := context.Background() - - for { - select { - case <-ctx.Done(): - return - default: - err := updateIP(ctx) - if err != nil { - logger.Error("update ip error", "error", err.Error()) - os.Exit(1) - } - time.Sleep(time.Minute * 3) - } - } -} - -func updateIP(ctx context.Context) error { - logger := slog.Default() - ip, err := ipGetter.GetIP() - if err != nil { - logger.Error("Get ip error", "error", err) - return err - } - - name, zone := zoneFromDomain(config.GetConfig().Domain) - logger.Info("zone from domain", - "name", name, - "zone", zone) - - currentIP, err := dnsProvider.GetRecords(ctx, zone) - if err != nil { - logger.Error("Get records error", "error", err) - return err - } - - var found bool - var record *libdns.Record - for _, r := range currentIP { - if r.Name == name { - found = true - record = &r - break - } - } - if found { - if record.Value == ip { - logger.Info("ip is same, skip update", "ip", ip) - return nil - } - oldIP := record.Value - record.Value = ip - _, err = dnsProvider.SetRecords(ctx, zone, []libdns.Record{ - *record, - }) - if err != nil { - logger.Error("Set records error", "error", err) - return err - } - notify.Notify(ctx, fmt.Sprintf(IPNotifyFormat, config.GetConfig().Domain, oldIP, ip)) - } else { - _, err = dnsProvider.AppendRecords(ctx, zone, []libdns.Record{ - { - Name: name, - Value: ip, - Type: "A", - }, - }) - if err != nil { - logger.Error("Append records error", "error", err) - return err - } - } - - return nil -} - -// zoneFromDomain return zone and domain -func zoneFromDomain(domain string) (string, string) { - arr := strings.SplitN(domain, ".", 2) - if len(arr) == 1 { - return "", "" + slog.Error("init app error", "error", err) + os.Exit(1) } - return arr[0], arr[1] + app.Run(context.Background()) } diff --git a/dns/dns.go b/dns/dns.go index 6d9cb0d..cee8bc1 100644 --- a/dns/dns.go +++ b/dns/dns.go @@ -16,24 +16,24 @@ type LibDNS interface { func New(conf *config.Config) LibDNS { switch conf.DNSProvider { case "cloudflare": - return NewCloudFlare() + return NewCloudFlare(conf) case "aliyun": - return NewAliyun() + return NewAliyun(conf) } return nil } // cloudflare -func NewCloudFlare() LibDNS { - provider := cloudflare.Provider{APIToken: config.GetConfig().CFToken} +func NewCloudFlare(conf *config.Config) LibDNS { + provider := cloudflare.Provider{APIToken: conf.CFToken} return &provider } // aliyun -func NewAliyun() LibDNS { +func NewAliyun(conf *config.Config) LibDNS { provider := alidns.Provider{ - AccKeyID: config.GetConfig().AliyunAccessKeyID, - AccKeySecret: config.GetConfig().AliyunAccessKeySecret, + AccKeyID: conf.AliyunAccessKeyID, + AccKeySecret: conf.AliyunAccessKeySecret, } return &provider } diff --git a/go.mod b/go.mod index 16a82d9..0a56e20 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,12 @@ go 1.23 require ( github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 + github.com/google/wire v0.6.0 github.com/hashicorp/go-retryablehttp v0.7.7 github.com/libdns/alidns v1.0.3 github.com/libdns/cloudflare v0.1.1 github.com/libdns/libdns v0.2.2 + github.com/lmittmann/tint v1.0.5 github.com/spf13/viper v1.18.0 github.com/stretchr/testify v1.9.0 github.com/weppos/publicsuffix-go v0.40.2 diff --git a/go.sum b/go.sum index 589f4a3..0508482 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,7 @@ github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2 github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= @@ -25,6 +26,9 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-github/v50 v50.2.0/go.mod h1:VBY8FB6yPIjrtKhozXv4FQupxKLS6H4m6xFZlT43q8Q= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= +github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= +github.com/google/wire v0.6.0 h1:HBkoIh4BdSxoyo9PveV8giw7ZsaBOvzWKfcg/6MrVwI= +github.com/google/wire v0.6.0/go.mod h1:F4QhpQ9EDIdJ1Mbop/NZBRB+5yrR6qg3BnctaoUk6NA= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k= @@ -44,6 +48,8 @@ github.com/libdns/cloudflare v0.1.1/go.mod h1:9VK91idpOjg6v7/WbjkEW49bSCxj00ALes github.com/libdns/libdns v0.2.0/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40= github.com/libdns/libdns v0.2.2 h1:O6ws7bAfRPaBsgAYt8MDe2HcNBGC29hkZ9MX2eUSX3s= github.com/libdns/libdns v0.2.2/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ= +github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw= +github.com/lmittmann/tint v1.0.5/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -96,6 +102,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= @@ -104,6 +111,7 @@ golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqR golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= @@ -114,6 +122,7 @@ golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= @@ -135,6 +144,7 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= @@ -146,6 +156,7 @@ golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= +golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= @@ -166,6 +177,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/internal/app/app.go b/internal/app/app.go new file mode 100644 index 0000000..265d201 --- /dev/null +++ b/internal/app/app.go @@ -0,0 +1,122 @@ +package app + +import ( + "context" + "fmt" + "log/slog" + "os" + "strings" + "time" + + "github.com/libdns/libdns" + "github.com/orvice/ddns/dns" + "github.com/orvice/ddns/internal/config" + "github.com/orvice/ddns/internal/ip" + "github.com/orvice/ddns/notify" +) + +var ( + IPNotifyFormat = "[%s] ip changed, old IP: %s new IP: %s" +) + +type App struct { + logger *slog.Logger + config *config.Config + dnsProvider dns.LibDNS + ipGetter ip.Getter + notifier notify.Notifier +} + +func New(logger *slog.Logger, config *config.Config, dnsProvider dns.LibDNS, ipGetter ip.Getter, notifier notify.Notifier) *App { + return &App{ + logger: logger, + config: config, + dnsProvider: dnsProvider, + ipGetter: ipGetter, + notifier: notifier, + } +} + +func (a *App) Run(ctx context.Context) { + for { + select { + case <-ctx.Done(): + return + default: + err := a.updateIP(ctx) + if err != nil { + a.logger.Error("update ip error", "error", err.Error()) + os.Exit(1) + } + time.Sleep(time.Minute * 3) + } + } +} + +func (a *App) updateIP(ctx context.Context) error { + ip, err := a.ipGetter.GetIP() + if err != nil { + a.logger.Error("Get ip error", "error", err) + return err + } + + name, zone := zoneFromDomain(a.config.Domain) + a.logger.Info("zone from domain", + "name", name, + "zone", zone) + + currentIP, err := a.dnsProvider.GetRecords(ctx, zone) + if err != nil { + a.logger.Error("Get records error", "error", err) + return err + } + + var found bool + var record *libdns.Record + for _, r := range currentIP { + if r.Name == name { + found = true + record = &r + break + } + } + if found { + if record.Value == ip { + a.logger.Info("ip is same, skip update", "ip", ip) + return nil + } + oldIP := record.Value + record.Value = ip + _, err = a.dnsProvider.SetRecords(ctx, zone, []libdns.Record{ + *record, + }) + if err != nil { + a.logger.Error("Set records error", "error", err) + return err + } + _ = a.notifier.Send(ctx, fmt.Sprintf(IPNotifyFormat, a.config.Domain, oldIP, ip)) + } else { + _, err = a.dnsProvider.AppendRecords(ctx, zone, []libdns.Record{ + { + Name: name, + Value: ip, + Type: "A", + }, + }) + if err != nil { + a.logger.Error("Append records error", "error", err) + return err + } + } + + return nil +} + +// zoneFromDomain return zone and domain +func zoneFromDomain(domain string) (string, string) { + arr := strings.SplitN(domain, ".", 2) + if len(arr) == 1 { + return "", "" + } + return arr[0], arr[1] +} diff --git a/internal/config/config.go b/internal/config/config.go index 5c9eca7..0cb676e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -16,29 +16,15 @@ type Config struct { AliyunAccessKeySecret string `mapstructure:"ALIYUN_ACCESS_KEY_SECRET"` } -var ( - config = &Config{} -) - -func Init() (err error) { - err = LoadConfig(".") - if err != nil { - return - } - return -} - -func GetConfig() *Config { - return config -} - -func LoadConfig(path string) (err error) { +func New() (*Config, error) { + path := "." viper.AddConfigPath(path) viper.SetConfigName("app") viper.SetConfigType("env") viper.AutomaticEnv() // viper.ReadInConfig() - err = viper.Unmarshal(config) - return + var config Config + err := viper.Unmarshal(&config) + return &config, err } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 4f6eb58..b5c1d7f 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -10,10 +10,10 @@ import ( func TestLoadConfig(t *testing.T) { os.Setenv("DOMAIN", "example.com") os.Setenv("TELEGRAM_CHATID", "1234567890") - err := LoadConfig(".") + conf, err := New() assert.Nil(t, err) - t.Log(GetConfig()) - assert.Equal(t, "example.com", GetConfig().Domain) - assert.Equal(t, int64(1234567890), GetConfig().TelegramChatID) + t.Log(conf) + assert.Equal(t, "example.com", conf.Domain) + assert.Equal(t, int64(1234567890), conf.TelegramChatID) } diff --git a/internal/ip/ip.go b/internal/ip/ip.go index c0891be..3205d93 100644 --- a/internal/ip/ip.go +++ b/internal/ip/ip.go @@ -23,7 +23,7 @@ type Getter interface { type IfconfigCo struct { } -func NewIfconfigCo() *IfconfigCo { +func NewIfGetter() Getter { return new(IfconfigCo) } diff --git a/internal/ip/ip_test.go b/internal/ip/ip_test.go index f7fcec4..20b7563 100644 --- a/internal/ip/ip_test.go +++ b/internal/ip/ip_test.go @@ -5,6 +5,6 @@ import ( ) func TestGetIP(t *testing.T) { - ip, err := NewIfconfigCo().GetIP() + ip, err := NewIfGetter().GetIP() t.Log(ip, err) } diff --git a/internal/wire/wire.go b/internal/wire/wire.go new file mode 100644 index 0000000..5eb4186 --- /dev/null +++ b/internal/wire/wire.go @@ -0,0 +1,19 @@ +//go:build wireinject +// +build wireinject + +package wire + +import ( + "github.com/google/wire" + "github.com/orvice/ddns/dns" + "github.com/orvice/ddns/internal/app" + "github.com/orvice/ddns/internal/config" + "github.com/orvice/ddns/internal/ip" + "github.com/orvice/ddns/notify" + "github.com/orvice/ddns/utils" +) + +func NewApp() (*app.App, error) { + wire.Build(app.New, config.New, dns.New, ip.NewIfGetter, utils.NewLogger, notify.NewTelegramNotifier) + return &app.App{}, nil +} diff --git a/internal/wire/wire_gen.go b/internal/wire/wire_gen.go new file mode 100644 index 0000000..20e642e --- /dev/null +++ b/internal/wire/wire_gen.go @@ -0,0 +1,34 @@ +// Code generated by Wire. DO NOT EDIT. + +//go:generate go run github.com/google/wire/cmd/wire +//go:build !wireinject +// +build !wireinject + +package wire + +import ( + "github.com/orvice/ddns/dns" + "github.com/orvice/ddns/internal/app" + "github.com/orvice/ddns/internal/config" + "github.com/orvice/ddns/internal/ip" + "github.com/orvice/ddns/notify" + "github.com/orvice/ddns/utils" +) + +// Injectors from wire.go: + +func NewApp() (*app.App, error) { + logger := utils.NewLogger() + configConfig, err := config.New() + if err != nil { + return nil, err + } + libDNS := dns.New(configConfig) + getter := ip.NewIfGetter() + notifier, err := notify.NewTelegramNotifier(configConfig) + if err != nil { + return nil, err + } + appApp := app.New(logger, configConfig, libDNS, getter, notifier) + return appApp, nil +} diff --git a/notify/notify.go b/notify/notify.go index e0481f8..e3e70b1 100644 --- a/notify/notify.go +++ b/notify/notify.go @@ -5,6 +5,7 @@ import ( "log/slog" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "github.com/orvice/ddns/internal/config" ) var notifiers []Notifier @@ -21,15 +22,15 @@ type TelegramNotifier struct { chatID int64 } -func NewTelegramNotifier(token string, chatID int64) (*TelegramNotifier, error) { - bot, err := tgbotapi.NewBotAPI(token) +func NewTelegramNotifier(conf *config.Config) (Notifier, error) { + bot, err := tgbotapi.NewBotAPI(conf.TelegramToken) if err != nil { return nil, err } return &TelegramNotifier{ bot: bot, - token: token, - chatID: chatID, + token: conf.TelegramToken, + chatID: conf.TelegramChatID, }, nil } diff --git a/utils/domain.go b/utils/domain.go deleted file mode 100644 index 1789f8d..0000000 --- a/utils/domain.go +++ /dev/null @@ -1,10 +0,0 @@ -package utils - -import ( - "github.com/weppos/publicsuffix-go/publicsuffix" -) - -func GetDomainSuffix(domain string) string { - s, _ := publicsuffix.Domain(domain) - return s -} diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..e9a2643 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,29 @@ +package utils + +import ( + "log/slog" + "os" + + "github.com/lmittmann/tint" + "github.com/weppos/publicsuffix-go/publicsuffix" +) + +func GetDomainSuffix(domain string) string { + s, _ := publicsuffix.Domain(domain) + return s +} + +func NewLogger() *slog.Logger { + w := os.Stderr + logger := slog.New( + tint.NewHandler(w, &tint.Options{ + ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { + if a.Key == slog.TimeKey && len(groups) == 0 { + return slog.Attr{} + } + return a + }, + }), + ) + return logger +}