dev: automated commit - 2025-08-10 18:18:05
This commit is contained in:
parent
47fb2fb928
commit
81967b4d8e
13 changed files with 438 additions and 53 deletions
103
main.go
103
main.go
|
|
@ -231,6 +231,19 @@ func getDNSRecords(zoneID string) ([]templates.DNSRecord, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// Get static records from database
|
||||
staticRecords, err := queries.GetStaticRecords(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Error getting static records: %v", err)
|
||||
staticRecords = []db.GetStaticRecordsRow{} // Continue with empty list
|
||||
}
|
||||
|
||||
// Create a map for faster lookup
|
||||
staticMap := make(map[string]bool)
|
||||
for _, sr := range staticRecords {
|
||||
staticMap[sr.RecordID] = true
|
||||
}
|
||||
|
||||
var records []templates.DNSRecord
|
||||
for _, rec := range recs {
|
||||
records = append(records, templates.DNSRecord{
|
||||
|
|
@ -240,6 +253,7 @@ func getDNSRecords(zoneID string) ([]templates.DNSRecord, error) {
|
|||
Content: rec.Content,
|
||||
TTL: rec.TTL,
|
||||
Proxied: *rec.Proxied,
|
||||
IsStatic: staticMap[rec.ID],
|
||||
CreatedOn: rec.CreatedOn.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
|
@ -262,13 +276,32 @@ func updateAllRecordsWithCurrentIP(zoneID string) error {
|
|||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Get static records to exclude from updates
|
||||
staticRecords, err := queries.GetStaticRecords(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Error getting static records: %v", err)
|
||||
staticRecords = []db.GetStaticRecordsRow{} // Continue with empty list
|
||||
}
|
||||
|
||||
staticMap := make(map[string]bool)
|
||||
for _, sr := range staticRecords {
|
||||
staticMap[sr.RecordID] = true
|
||||
}
|
||||
|
||||
rc := cloudflare.ZoneIdentifier(zoneID)
|
||||
records, _, err := api.ListDNSRecords(ctx, rc, cloudflare.ListDNSRecordsParams{Type: "A"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updatedCount := 0
|
||||
for _, rec := range records {
|
||||
// Skip static records
|
||||
if staticMap[rec.ID] {
|
||||
log.Printf("Skipping static record %s", rec.Name)
|
||||
continue
|
||||
}
|
||||
|
||||
if rec.Content != currentIP {
|
||||
_, err := api.UpdateDNSRecord(ctx, rc, cloudflare.UpdateDNSRecordParams{
|
||||
ID: rec.ID,
|
||||
|
|
@ -280,10 +313,13 @@ func updateAllRecordsWithCurrentIP(zoneID string) error {
|
|||
})
|
||||
if err != nil {
|
||||
log.Printf("Failed to update record %s: %v", rec.Name, err)
|
||||
} else {
|
||||
updatedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Updated %d A records to current IP %s", updatedCount, currentIP)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -518,6 +554,9 @@ func main() {
|
|||
e.DELETE("/records/:id", func(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
|
||||
// Remove from static records if it exists
|
||||
queries.RemoveStaticRecord(context.Background(), id)
|
||||
|
||||
if err := deleteDNSRecord(config.ZoneID, id); err != nil {
|
||||
return errorResponse(c, "Failed to delete record")
|
||||
}
|
||||
|
|
@ -551,6 +590,68 @@ func main() {
|
|||
return templates.Render(c.Response(), templates.RecordForm("Edit DNS Record", id, config.Domain, record))
|
||||
})
|
||||
|
||||
// New route to toggle static status
|
||||
e.POST("/records/:id/toggle-static", func(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
|
||||
// Check if record is currently static
|
||||
isStatic, err := queries.IsStaticRecord(context.Background(), id)
|
||||
if err != nil {
|
||||
return errorResponse(c, "Failed to check record status")
|
||||
}
|
||||
|
||||
// Get record details for the name
|
||||
records, err := getDNSRecords(config.ZoneID)
|
||||
if err != nil {
|
||||
return errorResponse(c, "Failed to load records")
|
||||
}
|
||||
|
||||
var recordName string
|
||||
for _, r := range records {
|
||||
if r.ID == id {
|
||||
recordName = r.Name
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if recordName == "" {
|
||||
return errorResponse(c, "Record not found")
|
||||
}
|
||||
|
||||
// Toggle static status
|
||||
if isStatic {
|
||||
// Remove from static records
|
||||
err = queries.RemoveStaticRecord(context.Background(), id)
|
||||
if err != nil {
|
||||
return errorResponse(c, "Failed to remove static status")
|
||||
}
|
||||
} else {
|
||||
// Add to static records
|
||||
err = queries.AddStaticRecord(context.Background(), db.AddStaticRecordParams{
|
||||
RecordID: id,
|
||||
RecordName: recordName,
|
||||
})
|
||||
if err != nil {
|
||||
return errorResponse(c, "Failed to add static status")
|
||||
}
|
||||
}
|
||||
|
||||
// Return updated table
|
||||
records, _ = getDNSRecords(config.ZoneID)
|
||||
currentIP, _ := getCurrentIP()
|
||||
|
||||
var message string
|
||||
if isStatic {
|
||||
message = "Record is now dynamic (will auto-update)"
|
||||
} else {
|
||||
message = "Record is now static (won't auto-update)"
|
||||
}
|
||||
|
||||
notification := templates.SuccessNotification(message)
|
||||
table := templates.DNSRecordsTable(records, currentIP)
|
||||
return templates.RenderMultiple(c.Response().Writer, notification, table)
|
||||
})
|
||||
|
||||
e.POST("/update-all-records", func(c echo.Context) error {
|
||||
if err := updateAllRecordsWithCurrentIP(config.ZoneID); err != nil {
|
||||
return errorResponse(c, "Failed to update records")
|
||||
|
|
@ -558,7 +659,7 @@ func main() {
|
|||
|
||||
records, _ := getDNSRecords(config.ZoneID)
|
||||
currentIP, _ := getCurrentIP()
|
||||
notification := templates.SuccessNotification("All A records updated")
|
||||
notification := templates.SuccessNotification("All non-static A records updated")
|
||||
table := templates.DNSRecordsTable(records, currentIP)
|
||||
return templates.RenderMultiple(c.Response().Writer, notification, table)
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue