Skip to content
Snippets Groups Projects
Commit 4631be1b authored by Erick Hitter's avatar Erick Hitter
Browse files

Proper logging

parent 6bde3ce6
No related branches found
No related tags found
No related merge requests found
...@@ -4,20 +4,30 @@ import ( ...@@ -4,20 +4,30 @@ import (
"fmt" "fmt"
"github.com/joshbetz/config" "github.com/joshbetz/config"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"net/url" "net/url"
"os"
) )
var ( var (
cfg *config.Config cfg *config.Config
ipv4Endpoint string ipv4Endpoint string
ipv6Endpoint string ipv6Endpoint string
logger *log.Logger
) )
// Parse config // Parse config
func init() { func init() {
cfg = config.New("config.json") // Logging
logOpts := log.Ldate | log.Ltime | log.LUTC | log.Lshortfile
logger = log.New(os.Stdout, "", logOpts)
logger.Println("PINGING dyndnsd ENDPOINT")
// Configuration
cfg = config.New("config.json")
cfg.Get("ipv4_endpoint", &ipv4Endpoint) cfg.Get("ipv4_endpoint", &ipv4Endpoint)
cfg.Get("ipv6_endpoint", &ipv6Endpoint) cfg.Get("ipv6_endpoint", &ipv6Endpoint)
} }
...@@ -27,6 +37,8 @@ func main() { ...@@ -27,6 +37,8 @@ func main() {
// Base URL // Base URL
endpoint, err := buildEndpointUrl() endpoint, err := buildEndpointUrl()
if err != nil { if err != nil {
logger.Println("Couldn't build endpoint URL")
logger.Printf("%s", err)
return return
} }
...@@ -37,27 +49,32 @@ func main() { ...@@ -37,27 +49,32 @@ func main() {
query.Set("myip", ipv4) query.Set("myip", ipv4)
endpoint.RawQuery = query.Encode() endpoint.RawQuery = query.Encode()
} else { } else {
logger.Println("Couldn't retrieve IPv4 address")
logger.Printf("%s", err)
return return
} }
fmt.Println(endpoint)
// IPv6 is optional // IPv6 is optional
ipv6, err := getUrl(ipv6Endpoint) ipv6, err := getUrl(ipv6Endpoint)
if err == nil { if err == nil {
query := endpoint.Query() query := endpoint.Query()
query.Set("myip6", ipv6) query.Set("myip6", ipv6)
endpoint.RawQuery = query.Encode() endpoint.RawQuery = query.Encode()
} else {
logger.Println("Couldn't retrieve IPv6 address")
logger.Printf("%s", err)
} }
// Send the update // Send the update
dyndns, err := getUrl(endpoint.String()) dyndns, err := getUrl(endpoint.String())
if err != nil { if err != nil {
logger.Println("Couldn't update dyndnsd endpoint")
logger.Printf("%s", err)
return return
} }
// TODO: better formatting logger.Println("SUCCESS! Ping sent.")
fmt.Println(dyndns) logger.Printf("%s", dyndns)
} }
// Build endpoint URL from configuration // Build endpoint URL from configuration
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment