Added settings through environment vars

This commit is contained in:
Philipp Böhm 2014-07-15 19:20:11 +02:00
parent 947a556871
commit 0b1e3055d5
2 changed files with 34 additions and 6 deletions

38
ddns.go
View File

@ -13,6 +13,8 @@ import (
"strings" "strings"
) )
var DdnsDomain string
func HandleErr(err error) { func HandleErr(err error) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -74,7 +76,15 @@ 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", nil) g.HTML(200, "index.html", gin.H{ "domain": DdnsDomain })
})
r.GET("/available/:hostname", func(c *gin.Context) {
hostname := c.Params.ByName("hostname")
c.JSON(200, gin.H{
"available": ! conn.HostExist(hostname),
})
}) })
r.GET("/new/:hostname", func(c *gin.Context) { r.GET("/new/:hostname", func(c *gin.Context) {
@ -90,9 +100,11 @@ func RunWebService() {
conn.SaveHost(host) conn.SaveHost(host)
c.String(200, fmt.Sprintf( c.JSON(200, gin.H{
"Go to /update/%s/%s for updating your IP address", "hostname": host.Hostname,
host.Hostname, host.Token)) "token": 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) {
@ -115,7 +127,7 @@ func RunWebService() {
ip, err := GetRemoteAddr(c.Req) ip, err := GetRemoteAddr(c.Req)
if err != nil { if err != nil {
c.String(500, "You sender IP address is not in the right format") c.String(500, "Your sender IP address is not in the right format")
} }
host.Ip = ip host.Ip = ip
@ -141,12 +153,28 @@ func GetRemoteAddr(req *http.Request) (string, error) {
} }
} }
func ExtractConfigVariables() {
// get the domain in the right format
DdnsDomain = os.Getenv("DDNS_DOMAIN")
if DdnsDomain == "" {
log.Fatal(
"You have to set your Subdomain through the DDNS_DOMAIN env variable")
}
if ! strings.HasPrefix(DdnsDomain, ".") {
DdnsDomain = "." + DdnsDomain
}
}
func main() { func main() {
if len(os.Args) < 2 { if len(os.Args) < 2 {
usage() usage()
} }
ExtractConfigVariables()
cmd := os.Args[1] cmd := os.Args[1]
switch cmd { switch cmd {

View File

@ -124,7 +124,7 @@
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">
<input class="form-control input-lg" type="text" placeholder="my-own-hostname"> <input class="form-control input-lg" type="text" placeholder="my-own-hostname">
<div class="input-group-addon input-lg">.d.pboehm.de</div> <div class="input-group-addon input-lg">{{.domain}}</div>
</div> </div>
</div> </div>
</form> </form>