Added command line flag for domain

This commit is contained in:
Philipp Böhm 2014-07-15 21:48:15 +02:00
parent 0b1e3055d5
commit b3d993b0eb
1 changed files with 26 additions and 27 deletions

53
ddns.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"flag"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pboehm/ddns/connection" "github.com/pboehm/ddns/connection"
@ -58,7 +59,6 @@ func RunBackend() {
fmt.Printf("END\n") fmt.Printf("END\n")
} }
} }
func RunWebService() { func RunWebService() {
@ -76,16 +76,16 @@ func RunWebService() {
r.HTMLTemplates = html r.HTMLTemplates = html
r.GET("/", func(g *gin.Context) { r.GET("/", func(g *gin.Context) {
g.HTML(200, "index.html", gin.H{ "domain": DdnsDomain }) g.HTML(200, "index.html", gin.H{"domain": DdnsDomain})
}) })
r.GET("/available/:hostname", func(c *gin.Context) { r.GET("/available/:hostname", func(c *gin.Context) {
hostname := c.Params.ByName("hostname") hostname := c.Params.ByName("hostname")
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"available": ! conn.HostExist(hostname), "available": !conn.HostExist(hostname),
}) })
}) })
r.GET("/new/:hostname", func(c *gin.Context) { r.GET("/new/:hostname", func(c *gin.Context) {
hostname := c.Params.ByName("hostname") hostname := c.Params.ByName("hostname")
@ -100,11 +100,11 @@ func RunWebService() {
conn.SaveHost(host) conn.SaveHost(host)
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"hostname": host.Hostname, "hostname": host.Hostname,
"token": host.Token, "token": host.Token,
"update_link": fmt.Sprintf("/update/%s/%s", host.Hostname, host.Token), "update_link": fmt.Sprintf("/update/%s/%s", host.Hostname, host.Token),
}) })
}) })
r.GET("/update/:hostname/:token", func(c *gin.Context) { r.GET("/update/:hostname/:token", func(c *gin.Context) {
@ -153,29 +153,28 @@ func GetRemoteAddr(req *http.Request) (string, error) {
} }
} }
func ExtractConfigVariables() { func ValidateCommandArgs() {
if DdnsDomain == "" {
// get the domain in the right format log.Fatal("You have to supply the domain via --domain=DOMAIN")
DdnsDomain = os.Getenv("DDNS_DOMAIN") } else if !strings.HasPrefix(DdnsDomain, ".") {
if DdnsDomain == "" { // get the domain in the right format
log.Fatal( DdnsDomain = "." + DdnsDomain
"You have to set your Subdomain through the DDNS_DOMAIN env variable") }
} }
if ! strings.HasPrefix(DdnsDomain, ".") {
DdnsDomain = "." + DdnsDomain
}
func init() {
flag.StringVar(&DdnsDomain, "domain", "",
"The subdomain which should be handled by DDNS")
} }
func main() { func main() {
flag.Parse()
ValidateCommandArgs()
if len(os.Args) < 2 { if len(flag.Args()) != 1 {
usage() usage()
} }
cmd := flag.Args()[0]
ExtractConfigVariables()
cmd := os.Args[1]
switch cmd { switch cmd {
case "backend": case "backend":