This commit is contained in:
Sense T 2024-04-11 11:41:33 +08:00
parent 7a5fcf1972
commit 58c66fc3a8
10 changed files with 68 additions and 36 deletions

View File

@ -26,6 +26,8 @@ func migrateDatabase(c *cli.Context) error {
if err := database.Connect(c.String("mysql-dsn")); err != nil { if err := database.Connect(c.String("mysql-dsn")); err != nil {
return err return err
} }
if c.Bool("debug") {
database.Client = database.Client.Debug()
}
return controllers.Migrate() return controllers.Migrate()
} }

View File

@ -26,6 +26,8 @@ func setDNS(c *cli.Context) error {
if err := database.Connect(c.String("mysql-dsn")); err != nil { if err := database.Connect(c.String("mysql-dsn")); err != nil {
return err return err
} }
if c.Bool("debug") {
database.Client = database.Client.Debug()
}
return controllers.SetupDNS(c.StringSlice("servers")...) return controllers.SetupDNS(c.StringSlice("servers")...)
} }

View File

@ -34,5 +34,8 @@ func setUser(c *cli.Context) error {
if err := database.Connect(c.String("mysql-dsn")); err != nil { if err := database.Connect(c.String("mysql-dsn")); err != nil {
return err return err
} }
if c.Bool("debug") {
database.Client = database.Client.Debug()
}
return controllers.SetupAdmin(c.String("username"), c.String("password")) return controllers.SetupAdmin(c.String("username"), c.String("password"))
} }

View File

@ -59,17 +59,25 @@ func CreateDomain(d *models.Domain) (*models.Domain, error) {
func GetDomains(domain string) ([]models.Domain, error) { func GetDomains(domain string) ([]models.Domain, error) {
if domain != "" { 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) n := make([]models.Domain, 0)
for _, e := range r { 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 return n, err
} else { } else {
r, err := (domainsDAO{}).GetAll(database.Client, models.Domain{}) r, err := (domainsDAO{}).GetAll(database.Client, &models.Domain{})
n := make([]models.Domain, 0) n := make([]models.Domain, 0)
for _, e := range r { 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 return n, err
} }
@ -117,13 +125,13 @@ func DeleteDomain(id string) error {
} }
tx := database.Client.Begin() 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 { if err != nil {
tx.Rollback() tx.Rollback()
return err 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() tx.Rollback()
return err return err
} }
@ -138,7 +146,7 @@ func DeleteDomain(id string) error {
// for metrics // for metrics
func getDomainCounts() (float64, error) { func getDomainCounts() (float64, error) {
c, err := (domainsDAO{}).GetAll(database.Client, models.Domain{}) c, err := (domainsDAO{}).GetAll(database.Client, &models.Domain{})
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -1,6 +1,7 @@
package controllers package controllers
import ( import (
"errors"
"reCoreD-UI/database" "reCoreD-UI/database"
"reCoreD-UI/models" "reCoreD-UI/models"
"strings" "strings"
@ -17,7 +18,7 @@ type settingsDAO struct {
func SetupDNS(dns ...string) error { func SetupDNS(dns ...string) error {
settings := models.Settings{Key: models.SettingsKeyDNSServer, Value: strings.Join(dns, dnsSep)} 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 return err
} }
@ -25,16 +26,20 @@ func SetupDNS(dns ...string) error {
} }
func GetDNS() ([]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 { if err != nil {
return nil, err 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 { func SetupAdmin(username, password string) error {
logrus.Debugf("got %s:%s", username, password) logrus.Debugf("got %s: %s", username, password)
settingUsername := models.Settings{ settingUsername := models.Settings{
Key: models.SettingsKeyAdminUsername, Key: models.SettingsKeyAdminUsername,
@ -46,12 +51,12 @@ func SetupAdmin(username, password string) error {
} }
tx := database.Client.Begin() 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() tx.Rollback()
return err 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() tx.Rollback()
return err return err
} }
@ -60,17 +65,25 @@ func SetupAdmin(username, password string) error {
} }
func GetAdmin() (string, 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 { if err != nil {
return "", "", err 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 { if err != nil {
return "", "", err 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 return username, password, nil
} }

View File

@ -3,7 +3,6 @@ package database
import ( import (
"errors" "errors"
"github.com/sirupsen/logrus"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -72,20 +71,22 @@ func (BaseDAO[T]) Update(db *gorm.DB, e T, cond ...T) (T, error) {
tx = tx.Where(c) 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 return e, err
} }
if result.RowsAffected == 0 {
return e, gorm.ErrRecordNotFound
}
return e, nil return e, nil
} }
func (b BaseDAO[T]) UpdateOrCreate(db *gorm.DB, e T, cond ...T) (T, error) { func (b BaseDAO[T]) UpdateOrCreate(db *gorm.DB, e T, cond ...T) (T, error) {
logrus.Debugf("got %v %v %v", db, e, cond) _, err := b.Update(db, e, cond...)
e, err := b.Update(db, e, cond...)
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
logrus.Debug("will create it") _, err = b.Create(db, e)
return b.Create(db, e)
} }
logrus.Debugf("return %v %v", e, err)
return e, err return e, err
} }

View File

@ -21,7 +21,7 @@ type Domain struct {
NegativeTtl uint32 `gorm:"not null;size:255,default:\"86400\"" json:"negative_ttl"` 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 := strings.Split(d.AdminEmail, "@")
s[0] = strings.Replace(s[0], ".", "\\", -1) s[0] = strings.Replace(s[0], ".", "\\", -1)
if !strings.HasSuffix(s[1], ".") { if !strings.HasSuffix(s[1], ".") {
@ -30,7 +30,7 @@ func (d Domain) EmailSOAForamt() string {
return strings.Join(s, ".") return strings.Join(s, ".")
} }
func (d Domain) WithDotEnd() string { func (d *Domain) WithDotEnd() string {
if strings.HasSuffix(d.DomainName, ".") { if strings.HasSuffix(d.DomainName, ".") {
return d.DomainName return d.DomainName
} else { } 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 var ns string
if !strings.HasSuffix(d.MainDNS, ".") { if !strings.HasSuffix(d.MainDNS, ".") {
ns = fmt.Sprintf("%s.", d.MainDNS) ns = fmt.Sprintf("%s.", d.MainDNS)

View File

@ -37,23 +37,23 @@ type Record[T recordContentTypes] struct {
RecordType string `gorm:"not null;size:255" json:"record_type"` RecordType string `gorm:"not null;size:255" json:"record_type"`
} }
func (Record[T]) TableName() string { func (*Record[T]) TableName() string {
return "coredns_record" return "coredns_record"
} }
func (r Record[T]) CheckZone() error { func (r *Record[T]) CheckZone() error {
if strings.HasSuffix(r.Zone, ".") { if strings.HasSuffix(r.Zone, ".") {
return ErrorZoneNotEndWithDot return ErrorZoneNotEndWithDot
} }
return nil return nil
} }
func (r Record[T]) WithOutDotTail() string { func (r *Record[T]) WithOutDotTail() string {
return strings.TrimRight(r.Zone, ".") return strings.TrimRight(r.Zone, ".")
} }
func (r Record[T]) ToEntity() IRecord { func (r *Record[T]) ToEntity() IRecord {
return &r return r
} }
func (r *Record[T]) FromEntity(entity any) error { 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) return json.Unmarshal(b, r)
} }
func (r Record[T]) GetType() string { func (r *Record[T]) GetType() string {
return r.RecordType return r.RecordType
} }

View File

@ -18,7 +18,7 @@ type Settings struct {
Value string `gorm:"not null;size:255"` 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) return fmt.Sprintf("%s: %s", s.Key, s.Value)
} }

View File

@ -19,6 +19,9 @@ func NewServer(c *cli.Context) (*Server, error) {
if err := database.Connect(c.String("mysql-dsn")); err != nil { if err := database.Connect(c.String("mysql-dsn")); err != nil {
return nil, err return nil, err
} }
if c.Bool("debug") {
database.Client = database.Client.Debug()
}
return &Server{ return &Server{
webServer: gin.New(), webServer: gin.New(),