DEVELOPMENT ENVIRONMENT

~liljamo/felu

bd08cb2f99c537aab74cacccd8c6be402b85af74 — Jonni Liljamo 19 days ago 827c9e5
feat: AcmeChallenge field for domains
2 files changed, 40 insertions(+), 4 deletions(-)

M internal/db/domains.go
M internal/db/migrations.go
M internal/db/domains.go => internal/db/domains.go +38 -4
@@ 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
}

M internal/db/migrations.go => internal/db/migrations.go +2 -0
@@ 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`),
	}
}