-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
43 lines (35 loc) · 1.03 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"dnsresolver/dnsrecords"
"fmt"
"log"
"github.com/gin-gonic/gin"
)
// Wrapper for existing addRecord function
func addRecordGin(c *gin.Context) {
// Read command from JSON request body
var request []string
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(400, gin.H{"error": "Invalid input"})
return
}
dnsrecords.Add(request, gDNSRecords) // Call the existing addRecord function with parsed input
c.JSON(201, gin.H{"status": "Record added"})
}
// Wrapper for existing listRecords function
func listRecordsGin(c *gin.Context) {
// Call the existing listRecords function
dnsrecords.List(gDNSRecords)
c.JSON(200, gin.H{"status": "Listed"})
}
func startGinAPI(apiport string) {
// Create a Gin router
r := gin.Default()
// Add routes for the API
r.GET("/dns/records", listRecordsGin) // List all DNS records
r.POST("/dns/records", addRecordGin) // Add a new DNS record
// Start the server
if err := r.Run(fmt.Sprintf(":%s", apiport)); err != nil {
log.Fatal("Error starting API:", err)
}
}