From 58c66fc3a8bbb9b2635e7af16974bc7d0878b26d Mon Sep 17 00:00:00 2001 From: Sense T Date: Thu, 11 Apr 2024 11:41:33 +0800 Subject: [PATCH] stage 1 --- cmd/config/database.go | 4 +++- cmd/config/dns.go | 4 +++- cmd/config/user.go | 3 +++ controllers/domain.go | 22 +++++++++++++++------- controllers/settings.go | 33 +++++++++++++++++++++++---------- database/basedao.go | 15 ++++++++------- models/domain.go | 6 +++--- models/record.go | 12 ++++++------ models/settings.go | 2 +- server/server.go | 3 +++ 10 files changed, 68 insertions(+), 36 deletions(-) diff --git a/cmd/config/database.go b/cmd/config/database.go index 5f354b3..accd76a 100644 --- a/cmd/config/database.go +++ b/cmd/config/database.go @@ -26,6 +26,8 @@ func migrateDatabase(c *cli.Context) error { if err := database.Connect(c.String("mysql-dsn")); err != nil { return err } - + if c.Bool("debug") { + database.Client = database.Client.Debug() + } return controllers.Migrate() } diff --git a/cmd/config/dns.go b/cmd/config/dns.go index 727a6ba..65e724e 100644 --- a/cmd/config/dns.go +++ b/cmd/config/dns.go @@ -26,6 +26,8 @@ func setDNS(c *cli.Context) error { if err := database.Connect(c.String("mysql-dsn")); err != nil { return err } - + if c.Bool("debug") { + database.Client = database.Client.Debug() + } return controllers.SetupDNS(c.StringSlice("servers")...) } diff --git a/cmd/config/user.go b/cmd/config/user.go index f33bdc7..35c7d0d 100644 --- a/cmd/config/user.go +++ b/cmd/config/user.go @@ -34,5 +34,8 @@ func setUser(c *cli.Context) error { if err := database.Connect(c.String("mysql-dsn")); err != nil { return err } + if c.Bool("debug") { + database.Client = database.Client.Debug() + } return controllers.SetupAdmin(c.String("username"), c.String("password")) } diff --git a/controllers/domain.go b/controllers/domain.go index bd89b9d..8a76147 100644 --- a/controllers/domain.go +++ b/controllers/domain.go @@ -59,17 +59,25 @@ func CreateDomain(d *models.Domain) (*models.Domain, error) { func GetDomains(domain string) ([]models.Domain, error) { if domain != "" { - r, err := (domainsDAO{}).GetAll(database.Client, models.Domain{DomainName: domain}) + r, err := (domainsDAO{}).GetAll(database.Client, &models.Domain{DomainName: domain}) n := make([]models.Domain, 0) for _, e := range r { - n = append(n, e.(models.Domain)) + i, ok := e.(*models.Domain) + if !ok { + continue + } + n = append(n, *i) } return n, err } else { - r, err := (domainsDAO{}).GetAll(database.Client, models.Domain{}) + r, err := (domainsDAO{}).GetAll(database.Client, &models.Domain{}) n := make([]models.Domain, 0) for _, e := range r { - n = append(n, e.(models.Domain)) + i, ok := e.(*models.Domain) + if !ok { + continue + } + n = append(n, *i) } return n, err } @@ -117,13 +125,13 @@ func DeleteDomain(id string) error { } tx := database.Client.Begin() - domain, err := (domainsDAO{}).GetOne(tx, models.Domain{ID: ID}) + domain, err := (domainsDAO{}).GetOne(tx, &models.Domain{ID: ID}) if err != nil { tx.Rollback() return err } - if err := (domainsDAO{}).Delete(tx, models.Domain{ID: ID}); err != nil { + if err := (domainsDAO{}).Delete(tx, &models.Domain{ID: ID}); err != nil { tx.Rollback() return err } @@ -138,7 +146,7 @@ func DeleteDomain(id string) error { // for metrics func getDomainCounts() (float64, error) { - c, err := (domainsDAO{}).GetAll(database.Client, models.Domain{}) + c, err := (domainsDAO{}).GetAll(database.Client, &models.Domain{}) if err != nil { return 0, err } diff --git a/controllers/settings.go b/controllers/settings.go index e04ab65..f9dc40a 100644 --- a/controllers/settings.go +++ b/controllers/settings.go @@ -1,6 +1,7 @@ package controllers import ( + "errors" "reCoreD-UI/database" "reCoreD-UI/models" "strings" @@ -17,7 +18,7 @@ type settingsDAO struct { func SetupDNS(dns ...string) error { settings := models.Settings{Key: models.SettingsKeyDNSServer, Value: strings.Join(dns, dnsSep)} - if _, err := (settingsDAO{}).UpdateOrCreate(database.Client, &settings, models.Settings{Key: models.SettingsKeyDNSServer}); err != nil { + if _, err := (settingsDAO{}).UpdateOrCreate(database.Client, &settings, &models.Settings{Key: models.SettingsKeyDNSServer}); err != nil { return err } @@ -25,16 +26,20 @@ func SetupDNS(dns ...string) error { } func GetDNS() ([]string, error) { - settings, err := (settingsDAO{}).GetOne(database.Client, models.Settings{Key: models.SettingsKeyDNSServer}) + settings, err := (settingsDAO{}).GetOne(database.Client, &models.Settings{Key: models.SettingsKeyDNSServer}) if err != nil { return nil, err } + d, ok := settings.(*models.Settings) + if !ok { + return nil, errors.New("cannot get dns config") + } - return strings.Split(settings.(models.Settings).Value, dnsSep), nil + return strings.Split(d.Value, dnsSep), nil } func SetupAdmin(username, password string) error { - logrus.Debugf("got %s:%s", username, password) + logrus.Debugf("got %s: %s", username, password) settingUsername := models.Settings{ Key: models.SettingsKeyAdminUsername, @@ -46,12 +51,12 @@ func SetupAdmin(username, password string) error { } tx := database.Client.Begin() - if _, err := (settingsDAO{}).UpdateOrCreate(tx, &settingUsername, models.Settings{Key: models.SettingsKeyAdminUsername}); err != nil { + if _, err := (settingsDAO{}).UpdateOrCreate(tx, &settingUsername, &models.Settings{Key: models.SettingsKeyAdminUsername}); err != nil { tx.Rollback() return err } - if _, err := (settingsDAO{}).UpdateOrCreate(tx, &settingPassword, models.Settings{Key: models.SettingsKeyAdminPassword}); err != nil { + if _, err := (settingsDAO{}).UpdateOrCreate(tx, &settingPassword, &models.Settings{Key: models.SettingsKeyAdminPassword}); err != nil { tx.Rollback() return err } @@ -60,17 +65,25 @@ func SetupAdmin(username, password string) error { } func GetAdmin() (string, string, error) { - settings, err := (settingsDAO{}).GetOne(database.Client, models.Settings{Key: models.SettingsKeyAdminUsername}) + settings, err := (settingsDAO{}).GetOne(database.Client, &models.Settings{Key: models.SettingsKeyAdminUsername}) if err != nil { return "", "", err } - username := settings.(models.Settings).Value + u, ok := settings.(*models.Settings) + if !ok { + return "", "", errors.New("cannot get admin username") + } + username := u.Value - settings, err = (settingsDAO{}).GetOne(database.Client, models.Settings{Key: models.SettingsKeyAdminPassword}) + settings, err = (settingsDAO{}).GetOne(database.Client, &models.Settings{Key: models.SettingsKeyAdminPassword}) if err != nil { return "", "", err } - password := settings.(models.Settings).Value + p, ok := settings.(*models.Settings) + password := p.Value + if !ok { + return "", "", errors.New("cannot get admin password") + } return username, password, nil } diff --git a/database/basedao.go b/database/basedao.go index 63d908d..f711915 100644 --- a/database/basedao.go +++ b/database/basedao.go @@ -3,7 +3,6 @@ package database import ( "errors" - "github.com/sirupsen/logrus" "gorm.io/gorm" ) @@ -72,20 +71,22 @@ func (BaseDAO[T]) Update(db *gorm.DB, e T, cond ...T) (T, error) { tx = tx.Where(c) } - if err := tx.Updates(&e).Error; err != nil { + result := tx.Updates(e) + + if err := result.Error; err != nil { return e, err } + if result.RowsAffected == 0 { + return e, gorm.ErrRecordNotFound + } return e, nil } func (b BaseDAO[T]) UpdateOrCreate(db *gorm.DB, e T, cond ...T) (T, error) { - logrus.Debugf("got %v %v %v", db, e, cond) - e, err := b.Update(db, e, cond...) + _, err := b.Update(db, e, cond...) if errors.Is(err, gorm.ErrRecordNotFound) { - logrus.Debug("will create it") - return b.Create(db, e) + _, err = b.Create(db, e) } - logrus.Debugf("return %v %v", e, err) return e, err } diff --git a/models/domain.go b/models/domain.go index 05aec83..78a5784 100644 --- a/models/domain.go +++ b/models/domain.go @@ -21,7 +21,7 @@ type Domain struct { NegativeTtl uint32 `gorm:"not null;size:255,default:\"86400\"" json:"negative_ttl"` } -func (d Domain) EmailSOAForamt() string { +func (d *Domain) EmailSOAForamt() string { s := strings.Split(d.AdminEmail, "@") s[0] = strings.Replace(s[0], ".", "\\", -1) if !strings.HasSuffix(s[1], ".") { @@ -30,7 +30,7 @@ func (d Domain) EmailSOAForamt() string { return strings.Join(s, ".") } -func (d Domain) WithDotEnd() string { +func (d *Domain) WithDotEnd() string { if strings.HasSuffix(d.DomainName, ".") { return d.DomainName } else { @@ -38,7 +38,7 @@ func (d Domain) WithDotEnd() string { } } -func (d Domain) GenerateSOA() dns.SOARecord { +func (d *Domain) GenerateSOA() dns.SOARecord { var ns string if !strings.HasSuffix(d.MainDNS, ".") { ns = fmt.Sprintf("%s.", d.MainDNS) diff --git a/models/record.go b/models/record.go index b972e59..df4c4de 100644 --- a/models/record.go +++ b/models/record.go @@ -37,23 +37,23 @@ type Record[T recordContentTypes] struct { RecordType string `gorm:"not null;size:255" json:"record_type"` } -func (Record[T]) TableName() string { +func (*Record[T]) TableName() string { return "coredns_record" } -func (r Record[T]) CheckZone() error { +func (r *Record[T]) CheckZone() error { if strings.HasSuffix(r.Zone, ".") { return ErrorZoneNotEndWithDot } return nil } -func (r Record[T]) WithOutDotTail() string { +func (r *Record[T]) WithOutDotTail() string { return strings.TrimRight(r.Zone, ".") } -func (r Record[T]) ToEntity() IRecord { - return &r +func (r *Record[T]) ToEntity() IRecord { + return r } func (r *Record[T]) FromEntity(entity any) error { @@ -65,7 +65,7 @@ func (r *Record[T]) FromEntity(entity any) error { return json.Unmarshal(b, r) } -func (r Record[T]) GetType() string { +func (r *Record[T]) GetType() string { return r.RecordType } diff --git a/models/settings.go b/models/settings.go index b7cd999..f55285a 100644 --- a/models/settings.go +++ b/models/settings.go @@ -18,7 +18,7 @@ type Settings struct { Value string `gorm:"not null;size:255"` } -func (s Settings) String() string { +func (s *Settings) String() string { return fmt.Sprintf("%s: %s", s.Key, s.Value) } diff --git a/server/server.go b/server/server.go index b0209df..9945838 100644 --- a/server/server.go +++ b/server/server.go @@ -19,6 +19,9 @@ func NewServer(c *cli.Context) (*Server, error) { if err := database.Connect(c.String("mysql-dsn")); err != nil { return nil, err } + if c.Bool("debug") { + database.Client = database.Client.Debug() + } return &Server{ webServer: gin.New(),