all api done

This commit is contained in:
Sense T 2024-04-03 22:37:15 +08:00
parent 8a8ea59b71
commit bdd4866c10
6 changed files with 172 additions and 43 deletions

View File

@ -1,6 +1,7 @@
package controllers package controllers
import ( import (
"fmt"
"reCoreD-UI/models" "reCoreD-UI/models"
"strconv" "strconv"
@ -10,6 +11,11 @@ import (
) )
func (c *Controller) CreateDomain(d *models.Domain) error { func (c *Controller) CreateDomain(d *models.Domain) error {
nss, err := c.GetDNS()
if err != nil {
return err
}
return c.DB.Transaction(func(tx *gorm.DB) error { return c.DB.Transaction(func(tx *gorm.DB) error {
if err := tx.Create(d).Error; err != nil { if err := tx.Create(d).Error; err != nil {
return err return err
@ -30,6 +36,18 @@ func (c *Controller) CreateDomain(d *models.Domain) error {
return err return err
} }
for i, ns := range nss {
record := &models.RecordWithType[dns.NSRecord]{}
record.Zone = d.DomainName
record.RecordType = models.RecordTypeNS
record.Content.Host = ns
record.Name = fmt.Sprintf("ns%d", i+1)
if err := tx.Create(record.ToRecord()).Error; err != nil {
return err
}
}
return nil return nil
}) })
} }

View File

@ -4,34 +4,9 @@ import (
"fmt" "fmt"
"reCoreD-UI/models" "reCoreD-UI/models"
dns "github.com/cloud66-oss/coredns_mysql"
"gorm.io/gorm" "gorm.io/gorm"
) )
func (c *Controller) SetupNSRecord(domain *models.Domain) error {
nss, err := c.GetDNS()
if err != nil {
return err
}
return c.DB.Transaction(func(tx *gorm.DB) error {
for i, ns := range nss {
record := &models.RecordWithType[dns.NSRecord]{}
record.Zone = domain.DomainName
record.RecordType = models.RecordTypeNS
record.Content.Host = ns
record.Name = fmt.Sprintf("ns%d", i+1)
if err := tx.Create(record.ToRecord()).Error; err != nil {
return err
}
}
return nil
})
}
func (c *Controller) CreateRecord(r *models.Record) error { func (c *Controller) CreateRecord(r *models.Record) error {
if r.RecordType != models.RecordTypeSOA { if r.RecordType != models.RecordTypeSOA {
domains, err := c.GetDomains(r.Zone) domains, err := c.GetDomains(r.Zone)
@ -60,7 +35,7 @@ func (c *Controller) CreateRecords(rs []*models.Record) error {
}) })
} }
func (c *Controller) GetRecords(cond map[string]any) ([]models.Record, error) { func (c *Controller) GetRecords(cond map[string]string) ([]models.Record, error) {
var records []models.Record var records []models.Record
if err := c.DB.Where(cond).Find(&records).Error; err != nil { if err := c.DB.Where(cond).Find(&records).Error; err != nil {
@ -76,8 +51,11 @@ func (c *Controller) UpdateRecord(r *models.Record) error {
}) })
} }
func (c *Controller) DeleteRecord(id string) error { func (c *Controller) DeleteRecord(domain, id string) error {
return c.DB.Transaction(func(tx *gorm.DB) error { return c.DB.Transaction(func(tx *gorm.DB) error {
return tx.Where("record_type != ?", models.RecordTypeSOA).Where("id = ?", id).Delete(&models.Record{}).Error return tx.Where("record_type != ?", models.RecordTypeSOA).
Where("id = ?", id).
Where("zone = ?", domain).
Delete(&models.Record{}).Error
}) })
} }

View File

@ -8,20 +8,6 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type Response struct {
Succeed bool `json:"succeed"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func errorHandler(c *gin.Context, err error) {
c.JSON(http.StatusInternalServerError, Response{
Succeed: false,
Message: err.Error(),
Data: nil,
})
}
func (s *Server) getDomains(c *gin.Context) { func (s *Server) getDomains(c *gin.Context) {
domains, err := s.controller.GetDomains("") domains, err := s.controller.GetDomains("")
if err != nil { if err != nil {

View File

@ -1,2 +1,124 @@
package server package server
import (
"net/http"
"reCoreD-UI/models"
"github.com/gin-gonic/gin"
)
func (s *Server) getRecords(c *gin.Context) {
query := make(map[string]string)
if err := c.BindQuery(&query); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: err.Error(),
})
return
}
domain := c.Param("domain")
query["zone"] = domain
records, err := s.controller.GetRecords(query)
if err != nil {
errorHandler(c, err)
return
}
c.JSON(http.StatusOK, Response{
Succeed: true,
Data: records,
})
}
func (s *Server) createRecord(c *gin.Context) {
record := &models.Record{}
if err := c.BindJSON(record); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: err.Error(),
})
return
}
domain := c.Param("domain")
if domain != record.Zone {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: "request body doesn't match URI",
})
return
}
if err := s.controller.CreateRecord(record); err != nil {
errorHandler(c, err)
return
}
c.JSON(http.StatusCreated, Response{
Succeed: true,
})
}
func (s *Server) createRecords(c *gin.Context) {
var records []*models.Record
if err := c.BindJSON(&records); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: err.Error(),
})
return
}
if err := s.controller.CreateRecords(records); err != nil {
errorHandler(c, err)
return
}
c.JSON(http.StatusCreated, Response{
Succeed: true,
})
}
func (s *Server) updateRecord(c *gin.Context) {
record := &models.Record{}
if err := c.BindJSON(record); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: err.Error(),
})
return
}
domain := c.Param("domain")
if domain != record.Zone {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: "request body doesn't match URI",
})
return
}
if err := s.controller.UpdateRecord(record); err != nil {
errorHandler(c, err)
return
}
c.JSON(http.StatusOK, Response{
Succeed: true,
})
}
func (s *Server) deleteRecord(c *gin.Context) {
domain := c.Param("domain")
id := c.Param("id")
if err := s.controller.DeleteRecord(domain, id); err != nil {
errorHandler(c, err)
return
}
c.JSON(http.StatusNoContent, Response{
Succeed: true,
})
}

21
server/response.go Normal file
View File

@ -0,0 +1,21 @@
package server
import (
"net/http"
"github.com/gin-gonic/gin"
)
type Response struct {
Succeed bool `json:"succeed"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func errorHandler(c *gin.Context, err error) {
c.JSON(http.StatusInternalServerError, Response{
Succeed: false,
Message: err.Error(),
Data: nil,
})
}

View File

@ -30,7 +30,11 @@ func (s *Server) setupRoute() {
records := groupV1.Group("/records") records := groupV1.Group("/records")
records. records.
GET("/") GET("/:domain", s.getRecords).
POST("/:domain", s.createRecord).
POST("/:domain/bulk", s.createRecords).
PUT("/:domain", s.updateRecord).
DELETE("/:domain/:id", s.deleteRecord)
server.Use(func(ctx *gin.Context) { server.Use(func(ctx *gin.Context) {
uri := ctx.Request.RequestURI uri := ctx.Request.RequestURI