This commit is contained in:
Sense T 2024-04-09 16:28:18 +08:00
parent 2369734230
commit 7dd3af3707
20 changed files with 419 additions and 392 deletions

View File

@ -2,6 +2,7 @@ package config
import ( import (
"reCoreD-UI/controllers" "reCoreD-UI/controllers"
"reCoreD-UI/database"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -26,11 +27,9 @@ func init() {
} }
func migrateDatabase(c *cli.Context) error { func migrateDatabase(c *cli.Context) error {
controller, err := controllers.NewController(c.String("mysql-dsn")) if err := database.Connect(c.String("mysql-dsn")); err != nil {
if err != nil {
return err return err
} }
defer controller.Close()
return controller.Migrate() return controllers.Migrate()
} }

View File

@ -2,6 +2,7 @@ package config
import ( import (
"reCoreD-UI/controllers" "reCoreD-UI/controllers"
"reCoreD-UI/database"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -25,11 +26,9 @@ func init() {
} }
func setDNS(c *cli.Context) error { func setDNS(c *cli.Context) error {
controller, err := controllers.NewController(c.String("mysql-dsn")) if err := database.Connect(c.String("mysql-dsn")); err != nil {
if err != nil {
return err return err
} }
defer controller.Close()
return controller.SetupDNS(c.StringSlice("servers")...) return controllers.SetupDNS(c.StringSlice("servers")...)
} }

View File

@ -2,6 +2,7 @@ package config
import ( import (
"reCoreD-UI/controllers" "reCoreD-UI/controllers"
"reCoreD-UI/database"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -33,10 +34,8 @@ func init() {
} }
func setUser(c *cli.Context) error { func setUser(c *cli.Context) error {
controller, err := controllers.NewController(c.String("mysql-dsn")) if err := database.Connect(c.String("mysql-dsn")); err != nil {
if err != nil {
return err return err
} }
defer controller.Close() return controllers.SetupAdmin(c.String("username"), c.String("password"))
return controller.SetupAdmin(c.String("username"), c.String("password"))
} }

View File

@ -1,29 +0,0 @@
package controllers
import (
"reCoreD-UI/models"
"strings"
"gorm.io/gorm"
)
const dnsSep = ","
func (c *Controller) SetupDNS(dns ...string) error {
return c.DB.Transaction(func(tx *gorm.DB) error {
settings := &models.Settings{}
return tx.Where(&models.Settings{Key: models.SettingsKeyDNSServer}).
Attrs(&models.Settings{Value: strings.Join(dns, dnsSep)}).
FirstOrCreate(&settings).Error
})
}
func (c *Controller) GetDNS() ([]string, error) {
settings := &models.Settings{}
if err := c.DB.Where(&models.Settings{Key: models.SettingsKeyDNSServer}).Find(&settings).Error; err != nil {
return nil, err
}
return strings.Split(settings.Value, dnsSep), nil
}

View File

@ -2,23 +2,27 @@ package controllers
import ( import (
"fmt" "fmt"
"reCoreD-UI/database"
"reCoreD-UI/models" "reCoreD-UI/models"
"strconv" "strconv"
dns "github.com/cloud66-oss/coredns_mysql" dns "github.com/cloud66-oss/coredns_mysql"
"gorm.io/gorm"
) )
func (c *Controller) CreateDomain(d *models.Domain) (*models.Domain, error) { type domainsDAO struct {
nss, err := c.GetDNS() database.BaseDAO[models.Domain]
}
func CreateDomain(d *models.Domain) (*models.Domain, error) {
nss, err := GetDNS()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := c.DB.Transaction(func(tx *gorm.DB) error { tx := database.Client.Begin()
if err := tx.Create(d).Error; err != nil { if _, err := (domainsDAO{}).Create(tx, *d); err != nil {
return err tx.Rollback()
return nil, err
} }
r := &models.RecordWithType[dns.SOARecord]{} r := &models.RecordWithType[dns.SOARecord]{}
@ -32,62 +36,58 @@ func (c *Controller) CreateDomain(d *models.Domain) (*models.Domain, error) {
r.Content.Expire = d.ExpiryPeriod r.Content.Expire = d.ExpiryPeriod
r.Content.MinTtl = d.NegativeTtl r.Content.MinTtl = d.NegativeTtl
if err := r.CheckZone(); err != nil { if err := r.CheckZone(); err != nil {
return err tx.Rollback()
return nil, err
} }
if err := tx.Create(r.ToRecord()).Error; err != nil { if _, err := (recordsDAO{}).Create(tx, *r.ToRecord()); err != nil {
return err tx.Rollback()
return nil, err
} }
for i, ns := range nss { for i, ns := range nss {
record := &models.RecordWithType[dns.NSRecord]{} record := &models.RecordWithType[dns.NSRecord]{}
record.Zone = d.DomainName record.Zone = d.WithDotEnd()
record.RecordType = models.RecordTypeNS record.RecordType = models.RecordTypeNS
record.Content.Host = ns record.Content.Host = ns
record.Name = fmt.Sprintf("ns%d", i+1) record.Name = fmt.Sprintf("ns%d", i+1)
if err := tx.Create(record.ToRecord()).Error; err != nil { if _, err := (recordsDAO{}).Create(tx, *record.ToRecord()); err != nil {
return err tx.Rollback()
}
}
return nil
}); err != nil {
return nil, err return nil, err
} }
}
tx.Commit()
return d, err return d, err
} }
func (c *Controller) GetDomains(domain string) ([]models.Domain, error) { func GetDomains(domain string) ([]models.Domain, error) {
var domains []models.Domain
tx := c.DB
if domain != "" { if domain != "" {
tx = tx.Where(&models.Domain{DomainName: domain}) return (domainsDAO{}).GetAll(database.Client, models.Domain{DomainName: domain})
} else {
return (domainsDAO{}).GetAll(database.Client, models.Domain{})
}
} }
if err := tx.Find(&domains).Error; err != nil { func UpdateDomain(d *models.Domain) error {
return nil, err tx := database.Client.Begin()
} if _, err := (domainsDAO{}).Update(tx, *d); err != nil {
tx.Rollback()
return domains, nil
}
func (c *Controller) UpdateDomain(d *models.Domain) error {
return c.DB.Transaction(func(tx *gorm.DB) error {
if err := tx.Model(d).Updates(d).Error; err != nil {
return err return err
} }
record := &models.Record{} soa, err := (recordsDAO{}).GetOne(tx, models.Record{
if err := tx.Where("record_type = ?", models.RecordTypeSOA).Where("zone = ?", d.DomainName).First(record).Error; err != nil { RecordType: models.RecordTypeSOA, Zone: d.WithDotEnd(),
})
if err != nil {
tx.Rollback()
return err return err
} }
r := &models.RecordWithType[dns.SOARecord]{} r := &models.RecordWithType[dns.SOARecord]{}
if err := r.FromRecord(record); err != nil { if err := r.FromRecord(&soa); err != nil {
tx.Rollback()
return err return err
} }
@ -97,49 +97,53 @@ func (c *Controller) UpdateDomain(d *models.Domain) error {
r.Content.Retry = d.RetryInterval r.Content.Retry = d.RetryInterval
r.Content.Expire = d.ExpiryPeriod r.Content.Expire = d.ExpiryPeriod
r.Content.MinTtl = d.NegativeTtl r.Content.MinTtl = d.NegativeTtl
if err := r.CheckZone(); err != nil { if err := r.CheckZone(); err != nil {
tx.Rollback()
return err return err
} }
if err := tx.Where("record_type = ?", models.RecordTypeSOA).Where("zone = ?", d.DomainName).Save(r.ToRecord()).Error; err != nil { if _, err := (recordsDAO{}).Update(tx, *r.ToRecord()); err != nil {
tx.Rollback()
return err return err
} }
tx.Commit()
return nil return nil
})
} }
func (c *Controller) DeleteDomain(id string) error { func DeleteDomain(id string) error {
ID, err := strconv.Atoi(id) ID, err := strconv.Atoi(id)
if err != nil { if err != nil {
return err return err
} }
return c.DB.Transaction(func(tx *gorm.DB) error { tx := database.Client.Begin()
domain := &models.Domain{ domain, err := (domainsDAO{}).GetOne(tx, models.Domain{ID: ID})
ID: ID, if err != nil {
} tx.Rollback()
if err := tx.First(&domain).Error; err != nil {
return err return err
} }
if err := tx.Where("zone = ?", domain.DomainName).Delete(&models.Record{}).Error; err != nil { if err := (domainsDAO{}).Delete(tx, models.Domain{ID: ID}); err != nil {
tx.Rollback()
return err return err
} }
if err := tx.Delete(&domain).Error; err != nil { if err := (recordsDAO{}).Delete(tx, models.Record{Zone: domain.WithDotEnd()}); err != nil {
tx.Rollback()
return err return err
} }
tx.Commit()
return nil return nil
})
} }
func (c *Controller) getDomainCounts() (float64, error) { // for metrics
var count int64 func getDomainCounts() (float64, error) {
if err := c.DB.Model(models.Domain{}).Count(&count).Error; err != nil { c, err := (domainsDAO{}).GetAll(database.Client, models.Domain{})
if err != nil {
return 0, err return 0, err
} }
return float64(count), nil return float64(len(c)), nil
} }

View File

@ -1,27 +0,0 @@
package controllers
import (
"reCoreD-UI/database"
"gorm.io/gorm"
)
type Controller struct {
DB *gorm.DB
}
func NewController(DSN string) (*Controller, error) {
db, err := database.Connect(DSN)
return &Controller{
DB: db,
}, err
}
func (c *Controller) Close() error {
d, err := c.DB.DB()
if err != nil {
return err
}
return d.Close()
}

View File

@ -22,8 +22,7 @@ var (
}, []string{"domain"}) }, []string{"domain"})
) )
func (c *Controller) RegisterMetrics() { func RegisterMetrics() {
prometheus.MustRegister(GaugeDomainCounts, GaugeRecordCounts) prometheus.MustRegister(GaugeDomainCounts, GaugeRecordCounts)
GormMetrics := ormMetric.New(ormMetric.Config{ GormMetrics := ormMetric.New(ormMetric.Config{
@ -42,14 +41,14 @@ func (c *Controller) RegisterMetrics() {
} }
} }
func (c *Controller) RefreshMetrics() error { func RefreshMetrics() error {
domainCounts, err := c.getDomainCounts() domainCounts, err := getDomainCounts()
if err != nil { if err != nil {
return err return err
} }
GaugeDomainCounts.Set(domainCounts) GaugeDomainCounts.Set(domainCounts)
recordCounts, err := c.getRecordCounts() recordCounts, err := getRecordCounts()
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,11 +1,22 @@
package controllers package controllers
import "reCoreD-UI/models" import (
"reCoreD-UI/database"
func (c *Controller) Migrate() error { "reCoreD-UI/models"
return c.DB.Set("gorm:table_options", "CHARSET=utf8mb4").AutoMigrate(
&models.Domain{},
&models.Record{},
&models.Settings{},
) )
func Migrate() error {
if err := (domainsDAO{}).Migrate(database.Client, models.Domain{}); err != nil {
return err
}
if err := (recordsDAO{}).Migrate(database.Client, models.Record{}); err != nil {
return err
}
if err := (settingsDAO{}).Migrate(database.Client, models.Settings{}); err != nil {
return err
}
return nil
} }

View File

@ -2,20 +2,22 @@ package controllers
import ( import (
"fmt" "fmt"
"reCoreD-UI/database"
"reCoreD-UI/models" "reCoreD-UI/models"
"strconv"
"gorm.io/gorm" "gorm.io/gorm"
) )
func (c *Controller) CreateRecord(r *models.Record) (*models.Record, error) { type recordsDAO struct {
if r.RecordType != models.RecordTypeSOA { database.BaseDAO[models.Record]
domains, err := c.GetDomains(r.Zone)
if err != nil {
return nil, err
} }
if len(domains) == 0 || domains[0].DomainName == r.Zone { func CreateRecord(r *models.Record) (*models.Record, error) {
return nil, fmt.Errorf("no such domain") if r.RecordType != models.RecordTypeSOA {
_, err := GetDomains(r.WithOutDotTail())
if err != nil {
return nil, err
} }
} }
@ -23,75 +25,79 @@ func (c *Controller) CreateRecord(r *models.Record) (*models.Record, error) {
return nil, err return nil, err
} }
if err := c.DB.Transaction(func(tx *gorm.DB) error { res, err := (recordsDAO{}).Create(database.Client, *r)
return tx.Create(r).Error return &res, err
}); err != nil {
return nil, err
} }
return r, nil func CreateRecords(rs []*models.Record) error {
} tx := database.Client.Begin()
func (c *Controller) CreateRecords(rs []*models.Record) error {
return c.DB.Transaction(func(tx *gorm.DB) error {
for _, r := range rs { for _, r := range rs {
if err := r.CheckZone(); err != nil { if err := r.CheckZone(); err != nil {
tx.Rollback()
return err return err
} }
if err := tx.Create(r).Error; err != nil { if _, err := (recordsDAO{}).Create(tx, *r); err != nil {
tx.Rollback()
return err return err
} }
} }
tx.Commit()
return nil return nil
})
} }
func (c *Controller) GetRecords(cond map[string]string) ([]models.Record, error) { func GetRecords(cond models.Record) ([]models.Record, error) {
var records []models.Record return (recordsDAO{}).GetAll(database.Client, cond)
if err := c.DB.Where(cond).Find(&records).Error; err != nil {
return nil, err
} }
return records, nil func UpdateRecord(r *models.Record) error {
}
func (c *Controller) UpdateRecord(r *models.Record) error {
if err := r.CheckZone(); err != nil { if err := r.CheckZone(); err != nil {
return err return err
} }
return c.DB.Transaction(func(tx *gorm.DB) error { if _, err := (recordsDAO{}).Update(database.Client, *r); err != nil {
return tx.Model(r).Updates(r).Error return err
}) }
return nil
} }
func (c *Controller) DeleteRecord(domain, id string) error { func DeleteRecord(domain, id string) error {
return c.DB.Transaction(func(tx *gorm.DB) error { ID, err := strconv.Atoi(id)
return tx.Where("record_type != ?", models.RecordTypeSOA). if err != nil {
Where("id = ?", id). return err
Where("zone = ?", domain).
Delete(&models.Record{}).Error
})
} }
func (c *Controller) getRecordCounts() (map[string]float64, error) { tx := database.Client.Begin()
rows, err := c.DB.Model(models.Record{}).Select("zone", "count(*) as count").Group("zone").Rows() record, err := (recordsDAO{}).GetOne(tx, models.Record{ID: ID, Zone: fmt.Sprintf("%s.", domain)})
if err != nil {
tx.Rollback()
return err
}
if record.RecordType == models.RecordTypeSOA {
tx.Rollback()
return gorm.ErrRecordNotFound
}
if err := (recordsDAO{}).Delete(tx, record); err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
// for metrics
func getRecordCounts() (map[string]float64, error) {
rows, err := (recordsDAO{}).GetAll(database.Client, models.Record{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close()
result := make(map[string]float64) result := make(map[string]float64)
for rows.Next() { for _, row := range rows {
var domain string result[row.Zone] += 1
var count int64
if err := rows.Scan(&domain, &count); err != nil {
return nil, err
} }
result[domain] = float64(count)
}
return result, nil return result, nil
} }

73
controllers/settings.go Normal file
View File

@ -0,0 +1,73 @@
package controllers
import (
"reCoreD-UI/database"
"reCoreD-UI/models"
"strings"
)
const dnsSep = ","
type settingsDAO struct {
database.BaseDAO[models.Settings]
}
func SetupDNS(dns ...string) error {
settings := models.Settings{Key: models.SettingsKeyDNSServer, Value: strings.Join(dns, dnsSep)}
if _, err := (settingsDAO{}).UpdateOrCreate(database.Client, settings); err != nil {
return err
}
return nil
}
func GetDNS() ([]string, error) {
settings, err := (settingsDAO{}).GetOne(database.Client, models.Settings{Key: models.SettingsKeyDNSServer})
if err != nil {
return nil, err
}
return strings.Split(settings.Value, dnsSep), nil
}
func SetupAdmin(username, password string) error {
settingUsername := models.Settings{
Key: models.SettingsKeyAdminUsername,
Value: username,
}
settingPassword := models.Settings{
Key: models.SettingsKeyAdminPassword,
Value: password,
}
tx := database.Client.Begin()
if _, err := (settingsDAO{}).UpdateOrCreate(tx, settingUsername); err != nil {
tx.Rollback()
return err
}
if _, err := (settingsDAO{}).UpdateOrCreate(tx, settingPassword); err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func GetAdmin() (string, string, error) {
settings, err := (settingsDAO{}).GetOne(database.Client, models.Settings{Key: models.SettingsKeyAdminUsername})
if err != nil {
return "", "", err
}
username := settings.Value
settings, err = (settingsDAO{}).GetOne(database.Client, models.Settings{Key: models.SettingsKeyAdminPassword})
if err != nil {
return "", "", err
}
password := settings.Value
return username, password, nil
}

View File

@ -1,41 +0,0 @@
package controllers
import (
"reCoreD-UI/models"
"gorm.io/gorm"
)
func (c *Controller) SetupAdmin(username, password string) error {
return c.DB.Transaction(func(tx *gorm.DB) error {
settings := &models.Settings{}
if err := tx.Where(&models.Settings{Key: models.SettingsKeyAdminUsername}).
Attrs(&models.Settings{Value: username}).
FirstOrCreate(settings).Error; err != nil {
return err
}
if err := tx.Where(&models.Settings{Key: models.SettingsKeyAdminPassword}).
Attrs(&models.Settings{Value: password}).
FirstOrCreate(settings).Error; err != nil {
return err
}
return nil
})
}
func (c *Controller) GetAdmin() (string, string, error) {
settings := &models.Settings{}
if err := c.DB.Where(&models.Settings{Key: models.SettingsKeyAdminUsername}).First(settings).Error; err != nil {
return "", "", err
}
username := settings.Value
if err := c.DB.Where(&models.Settings{Key: models.SettingsKeyAdminPassword}).First(settings).Error; err != nil {
return "", "", err
}
password := settings.Value
return username, password, nil
}

89
database/basedao.go Normal file
View File

@ -0,0 +1,89 @@
package database
import (
"errors"
"gorm.io/gorm"
)
type BaseDAO[T any] struct{}
func (b BaseDAO[T]) Migrate(db *gorm.DB, e T) error {
return db.Set("gorm:table_options", "CHARSET=utf8mb4").AutoMigrate(e)
}
func (BaseDAO[T]) GetAll(db *gorm.DB, e T) ([]T, error) {
var r []T
if err := db.Find(&r, e).Error; err != nil {
return nil, err
}
return r, nil
}
func (BaseDAO[T]) GetOne(db *gorm.DB, e T) (T, error) {
var r T
if err := db.First(&r, e).Error; err != nil {
return r, err
}
return r, nil
}
func (BaseDAO[T]) GetSome(db *gorm.DB, e T, limit, offset int) ([]T, error) {
var r []T
if err := db.Find(&r, e).Limit(limit).Offset(offset).Error; err != nil {
return nil, err
}
return r, nil
}
func (BaseDAO[T]) Create(db *gorm.DB, e T) (T, error) {
if err := db.Create(&e).Error; err != nil {
return e, err
}
return e, nil
}
func (BaseDAO[T]) FirstOrCreate(db *gorm.DB, e T) (T, error) {
if err := db.FirstOrCreate(&e).Error; err != nil {
return e, err
}
return e, nil
}
func (BaseDAO[T]) Update(db *gorm.DB, e T) (T, error) {
if err := db.Updates(&e).Error; err != nil {
return e, err
}
return e, nil
}
func (b BaseDAO[T]) UpdateOrCreate(db *gorm.DB, e T) (T, error) {
e, err := b.Update(db, e)
if errors.Is(err, gorm.ErrRecordNotFound) {
return b.Create(db, e)
}
return e, err
}
func (BaseDAO[T]) Delete(db *gorm.DB, e T) error {
if err := db.Delete(e).Error; err != nil {
return err
}
return nil
}
type IBaseDAO interface {
Migrate()
GetAll()
GetOne()
GetSome()
Create()
FirstOrCreate()
Update()
UpdateOrCreate()
Delete()
}

View File

@ -1,76 +0,0 @@
package database
import (
"errors"
"gorm.io/gorm"
)
type BaseDAO[T any] struct {
db *gorm.DB
}
func NewDAO[T any](db *gorm.DB) *BaseDAO[T] {
return &BaseDAO[T]{
db: db,
}
}
/* Single Table Operations */
func (b *BaseDAO[T]) GetAll(e T) ([]T, error) {
var r []T
if err := b.db.Find(&r, e).Error; err != nil {
return nil, err
}
return r, nil
}
func (b *BaseDAO[T]) GetOne(e T) (T, error) {
var r T
if err := b.db.First(&r, e).Error; err != nil {
return r, err
}
return r, nil
}
func (b *BaseDAO[T]) GetSome(e T, limit, offset int) ([]T, error) {
var r []T
if err := b.db.Find(&r, e).Limit(limit).Offset(offset).Error; err != nil {
return nil, err
}
return r, nil
}
func (b *BaseDAO[T]) Create(e T) (T, error) {
if err := b.db.Create(&e).Error; err != nil {
return e, err
}
return e, nil
}
func (b *BaseDAO[T]) Update(e T) (T, error) {
if err := b.db.Updates(&e).Error; err != nil {
return e, err
}
return e, nil
}
func (b *BaseDAO[T]) UpdateOrCreate(e T) (T, error) {
var r T
err := b.db.First(&r, e).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return b.Create(e)
}
return e, err
}
return b.Update(e)
}
func (b *BaseDAO[T]) Delete(e T) error {
if err := b.db.Delete(e).Error; err != nil {
return err
}
return nil
}

View File

@ -5,6 +5,13 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func Connect(DSN string) (*gorm.DB, error) { var Client *gorm.DB
return gorm.Open(mysql.Open(DSN), &gorm.Config{})
func Connect(DSN string) error {
var err error
Client, err = gorm.Open(mysql.Open(DSN), &gorm.Config{})
if err != nil {
return err
}
return nil
} }

View File

@ -32,13 +32,17 @@ func (Record) TableName() string {
return "coredns_record" return "coredns_record"
} }
func (r *Record) CheckZone() error { func (r Record) CheckZone() error {
if strings.HasSuffix(r.Zone, ".") { if strings.HasSuffix(r.Zone, ".") {
return fmt.Errorf("zone should end with '.'") return fmt.Errorf("zone should end with '.'")
} }
return nil return nil
} }
func (r Record) WithOutDotTail() string {
return strings.TrimRight(r.Zone, ".")
}
type RecordContentTypes interface { type RecordContentTypes interface {
dns.ARecord | dns.AAAARecord | dns.CNAMERecord | dns.CAARecord | dns.NSRecord | dns.MXRecord | dns.SOARecord | dns.SRVRecord | dns.TXTRecord dns.ARecord | dns.AAAARecord | dns.CNAMERecord | dns.CAARecord | dns.NSRecord | dns.MXRecord | dns.SOARecord | dns.SRVRecord | dns.TXTRecord
} }

View File

@ -2,13 +2,14 @@ package server
import ( import (
"net/http" "net/http"
"reCoreD-UI/controllers"
"reCoreD-UI/models" "reCoreD-UI/models"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func (s *Server) getDomains(c *gin.Context) { func (s *Server) getDomains(c *gin.Context) {
domains, err := s.controller.GetDomains("") domains, err := controllers.GetDomains("")
if err != nil { if err != nil {
errorHandler(c, err) errorHandler(c, err)
return return
@ -31,7 +32,7 @@ func (s *Server) createDomain(c *gin.Context) {
return return
} }
domain, err := s.controller.CreateDomain(domain) domain, err := controllers.CreateDomain(domain)
if err != nil { if err != nil {
errorHandler(c, err) errorHandler(c, err)
return return
@ -54,7 +55,7 @@ func (s *Server) updateDomain(c *gin.Context) {
return return
} }
if err := s.controller.UpdateDomain(domain); err != nil { if err := controllers.UpdateDomain(domain); err != nil {
errorHandler(c, err) errorHandler(c, err)
return return
} }
@ -66,7 +67,7 @@ func (s *Server) updateDomain(c *gin.Context) {
func (s *Server) deleteDomain(c *gin.Context) { func (s *Server) deleteDomain(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
if err := s.controller.DeleteDomain(id); err != nil { if err := controllers.DeleteDomain(id); err != nil {
errorHandler(c, err) errorHandler(c, err)
return return
} }

View File

@ -1,14 +1,16 @@
package server package server
import ( import (
"fmt"
"net/http" "net/http"
"reCoreD-UI/controllers"
"reCoreD-UI/models" "reCoreD-UI/models"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func (s *Server) getRecords(c *gin.Context) { func (s *Server) getRecords(c *gin.Context) {
query := make(map[string]string) query := models.Record{}
if err := c.BindQuery(&query); err != nil { if err := c.BindQuery(&query); err != nil {
c.JSON(http.StatusBadRequest, Response{ c.JSON(http.StatusBadRequest, Response{
Succeed: false, Succeed: false,
@ -17,9 +19,9 @@ func (s *Server) getRecords(c *gin.Context) {
return return
} }
domain := c.Param("domain") domain := c.Param("domain")
query["zone"] = domain query.Zone = fmt.Sprintf("%s.", domain)
records, err := s.controller.GetRecords(query) records, err := controllers.GetRecords(query)
if err != nil { if err != nil {
errorHandler(c, err) errorHandler(c, err)
return return
@ -50,7 +52,7 @@ func (s *Server) createRecord(c *gin.Context) {
return return
} }
record, err := s.controller.CreateRecord(record) record, err := controllers.CreateRecord(record)
if err != nil { if err != nil {
errorHandler(c, err) errorHandler(c, err)
return return
@ -72,7 +74,7 @@ func (s *Server) createRecords(c *gin.Context) {
return return
} }
if err := s.controller.CreateRecords(records); err != nil { if err := controllers.CreateRecords(records); err != nil {
errorHandler(c, err) errorHandler(c, err)
return return
} }
@ -101,7 +103,7 @@ func (s *Server) updateRecord(c *gin.Context) {
return return
} }
if err := s.controller.UpdateRecord(record); err != nil { if err := controllers.UpdateRecord(record); err != nil {
errorHandler(c, err) errorHandler(c, err)
return return
} }
@ -115,7 +117,7 @@ func (s *Server) deleteRecord(c *gin.Context) {
domain := c.Param("domain") domain := c.Param("domain")
id := c.Param("id") id := c.Param("id")
if err := s.controller.DeleteRecord(domain, id); err != nil { if err := controllers.DeleteRecord(domain, id); err != nil {
errorHandler(c, err) errorHandler(c, err)
return return
} }

View File

@ -1,10 +1,12 @@
package server package server
import ( import (
"errors"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"gorm.io/gorm"
) )
type Response struct { type Response struct {
@ -15,9 +17,16 @@ type Response struct {
func errorHandler(c *gin.Context, err error) { func errorHandler(c *gin.Context, err error) {
logrus.Error(err) logrus.Error(err)
if errors.Is(err, gorm.ErrRecordNotFound) {
c.JSON(http.StatusNotFound, Response{
Succeed: false,
Message: err.Error(),
})
} else {
c.JSON(http.StatusInternalServerError, Response{ c.JSON(http.StatusInternalServerError, Response{
Succeed: false, Succeed: false,
Message: err.Error(), Message: err.Error(),
Data: nil, Data: nil,
}) })
} }
}

View File

@ -2,6 +2,7 @@ package server
import ( import (
"path" "path"
"reCoreD-UI/controllers"
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -15,14 +16,14 @@ const (
) )
func (s *Server) setupRoute() { func (s *Server) setupRoute() {
username, password, err := s.controller.GetAdmin() username, password, err := controllers.GetAdmin()
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
metricHandler := gin.New() metricHandler := gin.New()
metricHandler.GET(metricPrefix, func(ctx *gin.Context) { metricHandler.GET(metricPrefix, func(ctx *gin.Context) {
if err := s.controller.RefreshMetrics(); err != nil { if err := controllers.RefreshMetrics(); err != nil {
logrus.Error(err) logrus.Error(err)
} }
promhttp.Handler().ServeHTTP(ctx.Writer, ctx.Request) promhttp.Handler().ServeHTTP(ctx.Writer, ctx.Request)

View File

@ -2,7 +2,7 @@ package server
import ( import (
"net" "net"
"reCoreD-UI/controllers" "reCoreD-UI/database"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -10,20 +10,17 @@ import (
) )
type Server struct { type Server struct {
controller *controllers.Controller
webServer *gin.Engine webServer *gin.Engine
listen string listen string
prefix string prefix string
} }
func NewServer(c *cli.Context) (*Server, error) { func NewServer(c *cli.Context) (*Server, error) {
controller, err := controllers.NewController(c.String("mysql-dsn")) if err := database.Connect(c.String("mysql-dsn")); err != nil {
if err != nil {
return nil, err return nil, err
} }
return &Server{ return &Server{
controller: controller,
webServer: gin.New(), webServer: gin.New(),
listen: net.JoinHostPort( listen: net.JoinHostPort(
c.String("listen"), c.String("listen"),