From 3015b873cb45893f0b931123e58dc5819714b0dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20B=C3=B6hm?= Date: Tue, 15 Jul 2014 23:41:54 +0200 Subject: [PATCH] refactored web component --- backend.go | 50 +++++++++++++++++++++++++------------------------- ddns.go | 8 ++++++-- web.go | 47 ++++++++++++++++++++++++++++++----------------- 3 files changed, 61 insertions(+), 44 deletions(-) diff --git a/backend.go b/backend.go index 8e91a2b..b94522b 100644 --- a/backend.go +++ b/backend.go @@ -12,7 +12,7 @@ import ( // the response data it possible func RunBackend(conn *connection.RedisConnection) { - // handshake with PowerDNS + // handshake with PowerDNS fmt.Printf("OK\tDDNS Go Backend\n") bio := bufio.NewReader(os.Stdin) @@ -28,34 +28,34 @@ func RunBackend(conn *connection.RedisConnection) { func HandleRequest(line string, conn *connection.RedisConnection) { defer fmt.Printf("END\n") - parts := strings.Split(line, "\t") - if len(parts) != 6 { - return - } + parts := strings.Split(line, "\t") + if len(parts) != 6 { + return + } - query_name := parts[1] - query_class := parts[2] - // query_type := parts[3] // TODO Handle SOA Requests - query_id := parts[4] + query_name := parts[1] + query_class := parts[2] + // query_type := parts[3] // TODO Handle SOA Requests + query_id := parts[4] - // get the host part of the fqdn - // pi.d.example.org -> pi - hostname := "" - if strings.HasSuffix(query_name, DdnsDomain) { - hostname = query_name[:len(query_name)-len(DdnsDomain)] - } + // get the host part of the fqdn + // pi.d.example.org -> pi + hostname := "" + if strings.HasSuffix(query_name, DdnsDomain) { + hostname = query_name[:len(query_name)-len(DdnsDomain)] + } - if hostname == "" || ! conn.HostExist(hostname) { - return - } + if hostname == "" || !conn.HostExist(hostname) { + return + } - host := conn.GetHost(hostname) + host := conn.GetHost(hostname) - record := "A" - if !host.IsIPv4() { - record = "AAAA" - } + record := "A" + if !host.IsIPv4() { + record = "AAAA" + } - fmt.Printf("DATA\t%s\t%s\t%s\t10\t%s\t%s\n", - query_name, query_class, record, query_id, host.Ip) + fmt.Printf("DATA\t%s\t%s\t%s\t10\t%s\t%s\n", + query_name, query_class, record, query_id, host.Ip) } diff --git a/ddns.go b/ddns.go index ec0173c..91dc59f 100644 --- a/ddns.go +++ b/ddns.go @@ -14,12 +14,16 @@ func HandleErr(err error) { } var ( - DdnsDomain string + DdnsDomain string + DdnsWebListenSocket string ) func init() { flag.StringVar(&DdnsDomain, "domain", "", "The subdomain which should be handled by DDNS") + + flag.StringVar(&DdnsWebListenSocket, "listen", ":8080", + "Which socket should the web service use to bind itself") } func ValidateCommandArgs() { @@ -44,7 +48,7 @@ func PrepareForExecution() string { } func main() { - cmd := PrepareForExecution() + cmd := PrepareForExecution() conn := connection.OpenConnection() defer conn.Close() diff --git a/web.go b/web.go index f8f0cfb..2fdae83 100644 --- a/web.go +++ b/web.go @@ -10,16 +10,8 @@ import ( ) func RunWebService(conn *connection.RedisConnection) { - - // Add index template from bindata - index_content, err := Asset("templates/index.html") - HandleErr(err) - - html, err := template.New("index.html").Parse(string(index_content)) - HandleErr(err) - r := gin.Default() - r.HTMLTemplates = html + r.HTMLTemplates = BuildTemplate() r.GET("/", func(g *gin.Context) { g.HTML(200, "index.html", gin.H{"domain": DdnsDomain}) @@ -37,7 +29,9 @@ func RunWebService(conn *connection.RedisConnection) { hostname := c.Params.ByName("hostname") if conn.HostExist(hostname) { - c.String(403, "This hostname has already been registered.") + c.JSON(403, gin.H{ + "error": "This hostname has already been registered.", + }) return } @@ -58,31 +52,39 @@ func RunWebService(conn *connection.RedisConnection) { token := c.Params.ByName("token") if !conn.HostExist(hostname) { - c.String(404, - "This hostname has not been registered or is expired.") + c.JSON(404, gin.H{ + "error": "This hostname has not been registered or is expired.", + }) return } host := conn.GetHost(hostname) if host.Token != token { - c.String(403, - "You have supplied the wrong token to manipulate this host") + c.JSON(403, gin.H{ + "error": "You have supplied the wrong token to manipulate this host", + }) return } ip, err := GetRemoteAddr(c.Req) if err != nil { - c.String(500, "Your sender IP address is not in the right format") + c.JSON(400, gin.H{ + "error": "Your sender IP address is not in the right format", + }) + return } host.Ip = ip conn.SaveHost(host) - c.String(200, fmt.Sprintf("Your current IP is %s", ip)) + c.JSON(200, gin.H{ + "current_ip": ip, + "status": "Successfuly updated", + }) }) - r.Run(":8080") + r.Run(DdnsWebListenSocket) } // Get the Remote Address of the client. At First we try to get the @@ -98,3 +100,14 @@ func GetRemoteAddr(req *http.Request) (string, error) { return ip, err } } + +// Get index template from bindata +func BuildTemplate() *template.Template { + index_content, err := Asset("templates/index.html") + HandleErr(err) + + html, err := template.New("index.html").Parse(string(index_content)) + HandleErr(err) + + return html +}