/*
* Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
// Package api implements API route handlers.
package api
import (
"net/http"
"git.src.quest/~liljamo/felu/internal/db"
"git.src.quest/~liljamo/felu/internal/util"
"github.com/gin-gonic/gin"
)
// UpdateA updates an A record based on query params.
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,
})
}
}