From 4871211f93e8d48cffb4bd56289ad474253b7e05 Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Sat, 26 Apr 2025 18:28:21 +0800 Subject: [PATCH] chore: cleanup some code (#1069) * chore * modernize loop * ddns: simpify Provider struct --- go.mod | 2 +- model/common.go | 2 +- model/ddns.go | 2 +- pkg/ddns/ddns.go | 29 ++++++++-------------------- pkg/ddns/dummy/dummy.go | 3 +-- pkg/utils/gjson.go | 10 ---------- service/singleton/alertsentinel.go | 1 - service/singleton/servicesentinel.go | 1 - 8 files changed, 12 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index cd83ac5..9743055 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,7 @@ require ( github.com/robfig/cron/v3 v3.0.1 github.com/swaggo/files v1.0.1 github.com/swaggo/gin-swagger v1.6.0 + github.com/swaggo/swag v1.16.4 github.com/tidwall/gjson v1.18.0 golang.org/x/crypto v0.36.0 golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 @@ -74,7 +75,6 @@ require ( github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect - github.com/swaggo/swag v1.16.4 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/tidwall/sjson v1.2.5 // indirect diff --git a/model/common.go b/model/common.go index 65f316f..12c3052 100644 --- a/model/common.go +++ b/model/common.go @@ -116,7 +116,7 @@ func searchByIDPri[S ~[]E, E CommonInterface](seq iter.Seq[string], x S) S { var class E split, ok := any(class).(splitter[S, E]) if !ok { - return nil + return x } plist, list2 := split.SplitList(x) diff --git a/model/ddns.go b/model/ddns.go index 0e1bf04..254ca0a 100644 --- a/model/ddns.go +++ b/model/ddns.go @@ -35,7 +35,7 @@ type DDNSProfile struct { DomainsRaw string `json:"-"` } -func (d DDNSProfile) TableName() string { +func (d *DDNSProfile) TableName() string { return "ddns" } diff --git a/pkg/ddns/ddns.go b/pkg/ddns/ddns.go index 3df190e..950e1a5 100644 --- a/pkg/ddns/ddns.go +++ b/pkg/ddns/ddns.go @@ -20,10 +20,8 @@ const ( ) type Provider struct { - ipAddr string - recordType string - prefix string - zone string + prefix string + zone string DDNSProfile *model.DDNSProfile IPAddrs *model.IP @@ -36,7 +34,7 @@ func (provider *Provider) GetProfileID() uint64 { func (provider *Provider) UpdateDomain(ctx context.Context, overrideDomains ...string) { for _, domain := range utils.IfOr(len(overrideDomains) > 0, overrideDomains, provider.DDNSProfile.Domains) { - for retries := 0; retries < int(provider.DDNSProfile.MaxRetries); retries++ { + for retries := range int(provider.DDNSProfile.MaxRetries) { log.Printf("NEZHA>> Updating DNS Record of domain %s: %d/%d", domain, retries+1, provider.DDNSProfile.MaxRetries) if err := provider.updateDomain(ctx, domain); err != nil { log.Printf("NEZHA>> Failed to update DNS record of domain %s: %v", domain, err) @@ -57,17 +55,13 @@ func (provider *Provider) updateDomain(ctx context.Context, domain string) error // 当IPv4和IPv6同时成功才算作成功 if *provider.DDNSProfile.EnableIPv4 { - provider.recordType = getRecordString(true) - provider.ipAddr = provider.IPAddrs.IPv4Addr - if err = provider.addDomainRecord(ctx); err != nil { + if err = provider.addDomainRecord(ctx, "A", provider.IPAddrs.IPv4Addr); err != nil { return err } } if *provider.DDNSProfile.EnableIPv6 { - provider.recordType = getRecordString(false) - provider.ipAddr = provider.IPAddrs.IPv6Addr - if err = provider.addDomainRecord(ctx); err != nil { + if err = provider.addDomainRecord(ctx, "AAAA", provider.IPAddrs.IPv6Addr); err != nil { return err } } @@ -75,13 +69,13 @@ func (provider *Provider) updateDomain(ctx context.Context, domain string) error return nil } -func (provider *Provider) addDomainRecord(ctx context.Context) error { +func (provider *Provider) addDomainRecord(ctx context.Context, recType, addr string) error { _, err := provider.Setter.SetRecords(ctx, provider.zone, []libdns.Record{ { - Type: provider.recordType, + Type: recType, Name: provider.prefix, - Value: provider.ipAddr, + Value: addr, TTL: time.Minute, }, }) @@ -123,10 +117,3 @@ func (provider *Provider) splitDomainSOA(ctx context.Context, domain string) (pr return "", "", fmt.Errorf("SOA record not found for domain: %s", domain) } - -func getRecordString(isIpv4 bool) string { - if isIpv4 { - return "A" - } - return "AAAA" -} diff --git a/pkg/ddns/dummy/dummy.go b/pkg/ddns/dummy/dummy.go index 310ffd8..2cb5a32 100644 --- a/pkg/ddns/dummy/dummy.go +++ b/pkg/ddns/dummy/dummy.go @@ -7,8 +7,7 @@ import ( ) // Internal use -type Provider struct { -} +type Provider struct{} func (provider *Provider) SetRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) { diff --git a/pkg/utils/gjson.go b/pkg/utils/gjson.go index 15df2eb..c4a9662 100644 --- a/pkg/utils/gjson.go +++ b/pkg/utils/gjson.go @@ -8,21 +8,11 @@ import ( ) var ( - ErrGjsonNotFound = errors.New("specified path does not exist") ErrGjsonWrongType = errors.New("wrong type") ) var emptyIterator = func(yield func(string, string) bool) {} -func GjsonGet(json []byte, path string) (gjson.Result, error) { - result := gjson.GetBytes(json, path) - if !result.Exists() { - return result, ErrGjsonNotFound - } - - return result, nil -} - func GjsonIter(json string) (iter.Seq2[string, string], error) { if json == "" { return emptyIterator, nil diff --git a/service/singleton/alertsentinel.go b/service/singleton/alertsentinel.go index 6e923f0..36d3ba9 100644 --- a/service/singleton/alertsentinel.go +++ b/service/singleton/alertsentinel.go @@ -187,7 +187,6 @@ func checkStatus() { // 清理旧数据 if max > 0 && max < len(alertsStore[alert.ID][server.ID]) { index := len(alertsStore[alert.ID][server.ID]) - max - clear(alertsStore[alert.ID][server.ID][:index]) // for GC alertsStore[alert.ID][server.ID] = alertsStore[alert.ID][server.ID][index:] } } diff --git a/service/singleton/servicesentinel.go b/service/singleton/servicesentinel.go index c44056f..310c49a 100644 --- a/service/singleton/servicesentinel.go +++ b/service/singleton/servicesentinel.go @@ -496,7 +496,6 @@ func (ss *ServiceSentinel) worker() { log.Printf("NEZHA>> Failed to save service monitor metrics: %v", err) } - clear(ss.serviceCurrentStatusData[mh.GetId()].result) ss.serviceCurrentStatusData[mh.GetId()].result = ss.serviceCurrentStatusData[mh.GetId()].result[:0] }