2024-04-03 09:05:12 +00:00
|
|
|
package server
|
|
|
|
|
2024-04-03 14:37:15 +00:00
|
|
|
import (
|
2024-04-09 08:28:18 +00:00
|
|
|
"fmt"
|
2024-04-03 14:37:15 +00:00
|
|
|
"net/http"
|
2024-04-09 08:28:18 +00:00
|
|
|
"reCoreD-UI/controllers"
|
2024-04-03 14:37:15 +00:00
|
|
|
"reCoreD-UI/models"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2024-04-13 02:14:45 +00:00
|
|
|
func validateRecord(r models.IRecord) error {
|
|
|
|
|
|
|
|
switch r.GetType() {
|
|
|
|
case models.RecordTypeA:
|
|
|
|
record := &models.Record[models.ARecord]{}
|
|
|
|
if err := record.FromEntity(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return record.Content.Validate()
|
|
|
|
case models.RecordTypeAAAA:
|
|
|
|
record := &models.Record[models.AAAARecord]{}
|
|
|
|
if err := record.FromEntity(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return record.Content.Validate()
|
|
|
|
case models.RecordTypeCNAME:
|
|
|
|
record := &models.Record[models.CNAMERecord]{}
|
|
|
|
if err := record.FromEntity(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return record.Content.Validate()
|
|
|
|
case models.RecordTypeCAA:
|
|
|
|
record := &models.Record[models.CAARecord]{}
|
|
|
|
if err := record.FromEntity(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return record.Content.Validate()
|
|
|
|
case models.RecordTypeMX:
|
|
|
|
record := &models.Record[models.MXRecord]{}
|
|
|
|
if err := record.FromEntity(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return record.Content.Validate()
|
|
|
|
case models.RecordTypeNS:
|
|
|
|
record := &models.Record[models.NSRecord]{}
|
|
|
|
if err := record.FromEntity(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return record.Content.Validate()
|
|
|
|
case models.RecordTypeSOA:
|
|
|
|
record := &models.Record[models.SOARecord]{}
|
|
|
|
if err := record.FromEntity(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return record.Content.Validate()
|
|
|
|
case models.RecordTypeSRV:
|
|
|
|
record := &models.Record[models.SRVRecord]{}
|
|
|
|
if err := record.FromEntity(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return record.Content.Validate()
|
|
|
|
case models.RecordTypeTXT:
|
|
|
|
record := &models.Record[models.TXTRecord]{}
|
|
|
|
if err := record.FromEntity(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return record.Content.Validate()
|
|
|
|
default:
|
|
|
|
return models.ErrInvalidType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
func getRecords(c *gin.Context) {
|
2024-04-13 01:22:27 +00:00
|
|
|
query := &models.Record[models.RecordContentDefault]{Content: make(models.RecordContentDefault)}
|
|
|
|
if err := c.BindQuery(query); err != nil {
|
2024-04-03 14:37:15 +00:00
|
|
|
c.JSON(http.StatusBadRequest, Response{
|
|
|
|
Succeed: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
domain := c.Param("domain")
|
2024-04-09 08:28:18 +00:00
|
|
|
query.Zone = fmt.Sprintf("%s.", domain)
|
2024-04-03 14:37:15 +00:00
|
|
|
|
2024-04-13 01:22:27 +00:00
|
|
|
records, err := controllers.GetRecords(query)
|
2024-04-03 14:37:15 +00:00
|
|
|
if err != nil {
|
|
|
|
errorHandler(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, Response{
|
|
|
|
Succeed: true,
|
|
|
|
Data: records,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
func createRecord(c *gin.Context) {
|
2024-04-13 01:22:27 +00:00
|
|
|
record := &models.Record[models.RecordContentDefault]{Content: make(models.RecordContentDefault)}
|
2024-04-03 14:37:15 +00:00
|
|
|
if err := c.BindJSON(record); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, Response{
|
|
|
|
Succeed: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := c.Param("domain")
|
2024-04-09 13:16:19 +00:00
|
|
|
if domain != record.WithOutDotTail() {
|
2024-04-03 14:37:15 +00:00
|
|
|
c.JSON(http.StatusBadRequest, Response{
|
|
|
|
Succeed: false,
|
|
|
|
Message: "request body doesn't match URI",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-13 02:14:45 +00:00
|
|
|
if err := validateRecord(record); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, Response{
|
|
|
|
Succeed: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
irecord, err := controllers.CreateRecord(record)
|
2024-04-09 08:28:18 +00:00
|
|
|
if err != nil {
|
2024-04-03 14:37:15 +00:00
|
|
|
errorHandler(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusCreated, Response{
|
|
|
|
Succeed: true,
|
2024-04-09 13:16:19 +00:00
|
|
|
Data: irecord,
|
2024-04-03 14:37:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
func createRecords(c *gin.Context) {
|
2024-04-13 01:22:27 +00:00
|
|
|
var records []models.Record[models.RecordContentDefault]
|
2024-04-03 14:37:15 +00:00
|
|
|
if err := c.BindJSON(&records); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, Response{
|
|
|
|
Succeed: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-13 01:22:27 +00:00
|
|
|
var iRecords []models.IRecord
|
|
|
|
for _, v := range records {
|
|
|
|
iRecords = append(iRecords, &v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := controllers.CreateRecords(iRecords); err != nil {
|
2024-04-03 14:37:15 +00:00
|
|
|
errorHandler(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusCreated, Response{
|
|
|
|
Succeed: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
func updateRecord(c *gin.Context) {
|
2024-04-13 01:22:27 +00:00
|
|
|
record := &models.Record[models.RecordContentDefault]{Content: make(models.RecordContentDefault)}
|
2024-04-03 14:37:15 +00:00
|
|
|
if err := c.BindJSON(record); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, Response{
|
|
|
|
Succeed: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-13 02:14:45 +00:00
|
|
|
if err := validateRecord(record); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, Response{
|
|
|
|
Succeed: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-03 14:37:15 +00:00
|
|
|
domain := c.Param("domain")
|
|
|
|
if domain != record.Zone {
|
|
|
|
c.JSON(http.StatusBadRequest, Response{
|
|
|
|
Succeed: false,
|
|
|
|
Message: "request body doesn't match URI",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-09 08:28:18 +00:00
|
|
|
if err := controllers.UpdateRecord(record); err != nil {
|
2024-04-03 14:37:15 +00:00
|
|
|
errorHandler(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, Response{
|
|
|
|
Succeed: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-09 13:16:19 +00:00
|
|
|
func deleteRecord(c *gin.Context) {
|
2024-04-03 14:37:15 +00:00
|
|
|
domain := c.Param("domain")
|
|
|
|
id := c.Param("id")
|
|
|
|
|
2024-04-09 08:28:18 +00:00
|
|
|
if err := controllers.DeleteRecord(domain, id); err != nil {
|
2024-04-03 14:37:15 +00:00
|
|
|
errorHandler(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusNoContent, Response{
|
|
|
|
Succeed: true,
|
|
|
|
})
|
|
|
|
}
|