From bd08cb2f99c537aab74cacccd8c6be402b85af74 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Thu, 24 Oct 2024 12:11:13 +0300 Subject: [PATCH] feat: AcmeChallenge field for domains --- internal/db/domains.go | 42 +++++++++++++++++++++++++++++++++++---- internal/db/migrations.go | 2 ++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/internal/db/domains.go b/internal/db/domains.go index f5bc5b6..9aa0edf 100644 --- a/internal/db/domains.go +++ b/internal/db/domains.go @@ -22,10 +22,11 @@ type DomainOwner struct { // Domain contains a domains information. type Domain struct { - ID string - APIKey string - Domain string - A string + ID string + APIKey string + Domain string + A string + AcmeChallenge string Owner DomainOwner } @@ -173,3 +174,36 @@ func RefreshDomainAPIKey(id string, userID string) error { } return nil } + +// FetchDomainAcmeChallenge fetches the ACME challenge string of a domain. +func FetchDomainAcmeChallenge(ddnsDomain string) (string, error) { + var acmeChallengeString string + err := DBConn.QueryRow(`SELECT acme_challenge FROM domains WHERE ddns_domain = $1`, + ddnsDomain).Scan(&acmeChallengeString) + if err != nil { + return "", err + } + return acmeChallengeString, nil +} + +// UpdateDomainAcmeChallenge updates the ACME challenge string of a domain. +func UpdateDomainAcmeChallenge(ddnsDomain string, acmeChallengeString string) error { + _, err := DBConn.Exec(`UPDATE domains SET acme_challenge = $1 WHERE ddns_domain = $2`, + acmeChallengeString, ddnsDomain) + if err != nil { + return err + } + + return nil +} + +// DeleteDomainAcmeChallenge deletes the ACME challenge string of a domain. +func DeleteDomainAcmeChallenge(ddnsDomain string) error { + _, err := DBConn.Exec(`UPDATE domains SET acme_challenge = NULL WHERE ddns_domain = $1`, + ddnsDomain) + if err != nil { + return err + } + + return nil +} diff --git a/internal/db/migrations.go b/internal/db/migrations.go index b35ee6b..a106377 100644 --- a/internal/db/migrations.go +++ b/internal/db/migrations.go @@ -82,5 +82,7 @@ func migrations() []string { a_record TEXT, FOREIGN KEY(owner) REFERENCES users(id) )`), + fmt.Sprintf(`ALTER TABLE domains + ADD acme_challenge TEXT`), } } -- 2.44.1