stage 1
This commit is contained in:
parent
7a5fcf1972
commit
58c66fc3a8
@ -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()
|
||||
}
|
||||
|
@ -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")...)
|
||||
}
|
||||
|
@ -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"))
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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(),
|
||||
|
Loading…
Reference in New Issue
Block a user