🌱🏠 a cloud service to enable your own server (owned by you and running on your computer) to be accessible on the internet in seconds, no credit card required https://greenhouse.server.garden/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

94 lines
2.1 KiB

package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
)
type GandiService struct {
BaseHTTPService
APIUrl string
APIKey string
}
type GandiDomainRecords struct {
RemoveApexNS bool `json:"remove_apex_ns"`
Items []GandiDomainRecord `json:"items"`
}
type GandiDomainRecord struct {
Name string `json:"rrset_name"`
Type string `json:"rrset_type"`
Values []string `json:"rrset_values"`
TTL int `json:"rrset_ttl"`
}
func NewGandiService(config *Config) *GandiService {
toReturn := &GandiService{
APIUrl: "https://api.gandi.net/v5",
APIKey: config.GandiAPIKey,
}
toReturn.ClientFactory = func() (*http.Client, *time.Time, error) {
return &http.Client{Timeout: 300 * time.Second}, nil, nil
}
return toReturn
}
func (service *GandiService) UpdateFreeSubdomains(freeSubdomains map[string][]string) error {
requestBody := GandiDomainRecords{
Items: []GandiDomainRecord{},
}
for subdomain, ips := range freeSubdomains {
requestBody.Items = append(
requestBody.Items,
GandiDomainRecord{
Name: subdomain,
Type: "A",
Values: ips,
TTL: 300,
},
GandiDomainRecord{
Name: fmt.Sprintf("*.%s", subdomain),
Type: "A",
Values: ips,
TTL: 300,
},
)
}
requestBodyBytes, err := json.Marshal(requestBody)
if err != nil {
return err
}
endpoint := fmt.Sprintf("/livedns/domains/%s/records", freeSubdomainDomain)
responseBytes, err := service.GandiHTTP("PUT", endpoint, bytes.NewBuffer(requestBodyBytes))
if err != nil {
log.Printf("Failed to update free subdomains on gandi: %s \n\nResponse was: %s\n\n", err, string(responseBytes))
return err
}
return nil
}
func (service *GandiService) GandiHTTP(method string, path string, body io.Reader) ([]byte, error) {
return service.MyHTTP200(
method,
fmt.Sprintf("%s%s", service.APIUrl, path),
body,
func(request *http.Request) {
request.Header.Add("Authorization", fmt.Sprintf("Apikey %s", service.APIKey))
if body != nil {
request.Header.Add("Content-Type", "application/json")
}
},
)
}