/*
* 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 handlers
import (
"net/http"
"regexp"
"strings"
"git.src.quest/~skye/felu-ddns/internal/db"
"git.src.quest/~skye/felu-ddns/internal/util"
"github.com/gin-gonic/gin"
)
type postDomainData struct {
Domain string `form:"domain"`
ARecord string `form:"a_record"`
}
func PostDomain() gin.HandlerFunc {
return func(c *gin.Context) {
data := &postDomainData{}
if err := c.Bind(data); err != nil {
c.String(http.StatusBadRequest, "Could not bind domain data")
c.Abort()
return
}
if data.Domain == "" {
c.String(http.StatusBadRequest, "Domain can't be empty")
c.Abort()
return
}
if strings.Contains(data.Domain, ".") {
c.String(http.StatusBadRequest, "Domain can't contain full stops")
c.Abort()
return
}
// NOTE: I doubt doing a little regex here will matter, just the easiest for now.
if !regexp.MustCompile(`^[A-Za-z0-9]*$`).MatchString(data.Domain) {
c.String(http.StatusBadRequest, "Domain contains invalid chars")
c.Abort()
return
}
if err := util.CheckARecord(data.ARecord); err != nil {
c.String(http.StatusBadRequest, err.Error())
c.Abort()
return
}
userId, exists := c.Get("user_id")
if !exists {
c.String(http.StatusInternalServerError, "This should not be possible, but don't quote me on that")
c.Abort()
return
}
err := db.CreateDomain(data.Domain, data.ARecord, userId.(string))
if err != nil {
// FIXME: Handle better
c.String(http.StatusInternalServerError, "Something went wrong while creating a new domain")
c.Abort()
return
}
c.Header("HX-Trigger", "update-domain-list")
}
}
func PatchDomain() gin.HandlerFunc {
return func(c *gin.Context) {
userId, exists := c.Get("user_id")
if !exists {
c.String(http.StatusInternalServerError, "This should not be possible, but don't quote me on that")
c.Abort()
return
}
id := c.Param("id")
aRecord := c.PostForm("a_record")
if aRecord != "" {
if err := util.CheckARecord(aRecord); err != nil {
c.String(http.StatusBadRequest, err.Error())
c.Abort()
return
}
err := db.UpdateDomainARecordManual(id, userId.(string), aRecord)
if err != nil {
// FIXME: Handle better
c.String(http.StatusInternalServerError, "Something went wrong while updating the a record")
c.Abort()
return
}
}
c.Header("HX-Trigger", "update-domain-list")
}
}
func DeleteDomain() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
userId, exists := c.Get("user_id")
if !exists {
c.String(http.StatusInternalServerError, "This should not be possible, but don't quote me on that")
c.Abort()
return
}
err := db.DeleteDomain(id, userId.(string))
if err != nil {
// FIXME: Handle better
c.String(http.StatusInternalServerError, "Something went wrong while deleting the domain")
c.Abort()
return
}
c.Header("HX-Trigger", "update-domain-list")
}
}
func RefreshDomainApiKey() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
userId, exists := c.Get("user_id")
if !exists {
c.String(http.StatusInternalServerError, "This should not be possible, but don't quote me on that")
c.Abort()
return
}
err := db.RefreshDomainApiKey(id, userId.(string))
if err != nil {
// FIXME: Handle better
c.String(http.StatusInternalServerError, "Something went wrong while updating the api key")
c.Abort()
return
}
c.Header("HX-Trigger", "update-domain-list")
}
}