Commenting for godoc
This commit is contained in:
parent
75b0734bf8
commit
e79e7e33f2
5 changed files with 37 additions and 8 deletions
19
api/api.go
19
api/api.go
|
@ -1,3 +1,5 @@
|
||||||
|
// Package api provides a basic interface for dealing
|
||||||
|
// with Name.com DNS API's.
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -15,12 +17,14 @@ const (
|
||||||
devUrl = "https://api.dev.name.com/"
|
devUrl = "https://api.dev.name.com/"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Contains details required to access the Name.com API.
|
||||||
type API struct {
|
type API struct {
|
||||||
baseUrl string
|
baseUrl string
|
||||||
username string
|
username string
|
||||||
token string
|
token string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A Name.com DNS record.
|
||||||
type DNSRecord struct {
|
type DNSRecord struct {
|
||||||
RecordId string `json:"record_id"`
|
RecordId string `json:"record_id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
@ -35,10 +39,8 @@ type resultResponse struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructs a new Name.com API
|
// Constructs a new Name.com API. If dev is true, then
|
||||||
//
|
// the API uses the development API, instead of the production API.
|
||||||
// If dev = true, then the API uses the development
|
|
||||||
// API, instead of the production API.
|
|
||||||
func NewNameAPI(username, token string, dev bool) API {
|
func NewNameAPI(username, token string, dev bool) API {
|
||||||
a := API{username: username, token: token}
|
a := API{username: username, token: token}
|
||||||
|
|
||||||
|
@ -51,7 +53,7 @@ func NewNameAPI(username, token string, dev bool) API {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructs a new Name.com API from a configuration
|
// Constructs a new Name.com API from a configuration.
|
||||||
func NewAPIFromConfig(c Config) API {
|
func NewAPIFromConfig(c Config) API {
|
||||||
return NewNameAPI(c.Username, c.Token, c.Dev)
|
return NewNameAPI(c.Username, c.Token, c.Dev)
|
||||||
}
|
}
|
||||||
|
@ -75,6 +77,9 @@ func (api API) performRequest(method, url string, body io.Reader) (response []by
|
||||||
return ioutil.ReadAll(resp.Body)
|
return ioutil.ReadAll(resp.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a DNS record for a given domain. The hostname is
|
||||||
|
// specified in the DNSRecord under name, and should not
|
||||||
|
// include the domain.
|
||||||
func (api API) CreateDNSRecord(domain string, record DNSRecord) error {
|
func (api API) CreateDNSRecord(domain string, record DNSRecord) error {
|
||||||
b, err := json.Marshal(record)
|
b, err := json.Marshal(record)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -105,6 +110,8 @@ func (api API) CreateDNSRecord(domain string, record DNSRecord) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deletes a DNS record for a given domain. The recordId can
|
||||||
|
// be retreived from GetDNSRecords.
|
||||||
func (api API) DeleteDNSRecord(domain, recordId string) error {
|
func (api API) DeleteDNSRecord(domain, recordId string) error {
|
||||||
var body struct {
|
var body struct {
|
||||||
RecordId string `json:"record_id"`
|
RecordId string `json:"record_id"`
|
||||||
|
@ -141,7 +148,7 @@ func (api API) DeleteDNSRecord(domain, recordId string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a slice of DNS records associated with a given domain.
|
// Returns a slice of DNS records associated with a given domain.
|
||||||
func (api API) GetRecords(domain string) (records []DNSRecord, err error) {
|
func (api API) GetDNSRecords(domain string) (records []DNSRecord, err error) {
|
||||||
resp, err := api.performRequest(
|
resp, err := api.performRequest(
|
||||||
"GET",
|
"GET",
|
||||||
fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/list/", domain),
|
fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/list/", domain),
|
||||||
|
|
|
@ -5,6 +5,14 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Each configuration should represent a specifc domain.
|
||||||
|
// A domain can have multiple hostnames, and the hostnames
|
||||||
|
// that should be automaticall updated are in Hostnames.
|
||||||
|
// If the root domain is to be managed, an empty hostname
|
||||||
|
// should be added to the Hostnames slice.
|
||||||
|
//
|
||||||
|
// The interval is the polling time (in seconds) for
|
||||||
|
// daemon mode.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Dev bool `json:"dev"`
|
Dev bool `json:"dev"`
|
||||||
Domain string `json:"domain"`
|
Domain string `json:"domain"`
|
||||||
|
@ -14,6 +22,8 @@ type Config struct {
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Loads configurations from a file. The configuration
|
||||||
|
// is stored as an array of JSON serialized Config structs.
|
||||||
func LoadConfigs(path string) ([]Config, error) {
|
func LoadConfigs(path string) ([]Config, error) {
|
||||||
var configs struct {
|
var configs struct {
|
||||||
Configs []Config
|
Configs []Config
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
// Package dyndns provides a tool for running a
|
||||||
|
// dynamic dns updating service.
|
||||||
package dyndns
|
package dyndns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -42,7 +44,7 @@ func runConfig(c api.Config, daemon bool) {
|
||||||
// 1 per hostname with the associated domain.
|
// 1 per hostname with the associated domain.
|
||||||
// If the content is not the current IP, then
|
// If the content is not the current IP, then
|
||||||
// update it.
|
// update it.
|
||||||
records, err := a.GetRecords(c.Domain)
|
records, err := a.GetDNSRecords(c.Domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Logger.Printf("Failed to retreive records for%s\n", c.Domain)
|
log.Logger.Printf("Failed to retreive records for%s\n", c.Domain)
|
||||||
if daemon {
|
if daemon {
|
||||||
|
|
|
@ -6,6 +6,9 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Urls contains a set of mirrors in which a
|
||||||
|
// raw IP string can be retreived. It is exported
|
||||||
|
// for the intent of modification.
|
||||||
var (
|
var (
|
||||||
Urls = []string{"http://myexternalip.com/raw"}
|
Urls = []string{"http://myexternalip.com/raw"}
|
||||||
)
|
)
|
||||||
|
@ -25,7 +28,10 @@ func tryMirror(url string) (string, error) {
|
||||||
return string(contents), nil
|
return string(contents), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieves the external facing IP Address
|
// Retrieves the external facing IP Address.
|
||||||
|
// If multiple mirrors are provided in Urls,
|
||||||
|
// it will try each one (in order), should
|
||||||
|
// preceding mirrors fail.
|
||||||
func GetExternalIP() (string, error) {
|
func GetExternalIP() (string, error) {
|
||||||
for _, url := range Urls {
|
for _, url := range Urls {
|
||||||
resp, err := tryMirror(url)
|
resp, err := tryMirror(url)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package log provides a basic global logger for name-dyndns.
|
||||||
package log
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -5,8 +6,11 @@ import (
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Global logger.
|
||||||
var Logger *log.Logger
|
var Logger *log.Logger
|
||||||
|
|
||||||
|
// Initialize the logger with a specific io.Writer.
|
||||||
|
// This function is generally called near startup.
|
||||||
func Init(writer io.Writer) {
|
func Init(writer io.Writer) {
|
||||||
Logger = log.New(writer, "", log.Ldate|log.Ltime)
|
Logger = log.New(writer, "", log.Ldate|log.Ltime)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue