/*
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
package api
import (
"net/http"
"git.src.quest/~skye/felu-ddns/internal/db"
"git.src.quest/~skye/felu-ddns/internal/util"
"github.com/gin-gonic/gin"
)
func UpdateA() gin.HandlerFunc {
return func(c *gin.Context) {
domain := c.Query("domain")
if domain == "" {
c.JSON(http.StatusBadRequest, gin.H{
"status": "error",
"error": "no domain was provided",
})
c.Abort()
return
}
apiKey := c.Query("api_key")
if apiKey == "" {
c.JSON(http.StatusBadRequest, gin.H{
"status": "error",
"error": "no api key was provided",
})
c.Abort()
return
}
aRecord := c.Query("record")
if aRecord == "" {
aRecord = c.ClientIP()
}
if err := util.CheckARecord(aRecord); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": "error",
"error": err.Error(),
})
c.Abort()
return
}
err := db.UpdateDomainARecord(domain, apiKey, aRecord)
if err != nil {
// FIXME: Handle better, "bad api key" is just the most likely scenario
c.JSON(http.StatusBadRequest, gin.H{
"status": "error",
"error": "bad api key",
})
c.Abort()
return
}
c.JSON(http.StatusOK, gin.H{
"status": "success",
"a_record": aRecord,
})
}
}