diff options
author | Kevin Chabowski <kevin@kch42.de> | 2014-03-03 13:18:33 +0100 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2014-03-03 13:18:33 +0100 |
commit | 21827d68b521e22ac6e38b40bca49bc2885b8212 (patch) | |
tree | 27eebcf945b82b9a68ec632072d33a7e3b56f222 | |
parent | d25cb0b579122362a6759e981d1cf9f6458e9959 (diff) | |
download | startpage-21827d68b521e22ac6e38b40bca49bc2885b8212.tar.gz startpage-21827d68b521e22ac6e38b40bca49bc2885b8212.tar.bz2 startpage-21827d68b521e22ac6e38b40bca49bc2885b8212.zip |
Template will now be loaded, even if not started from same directory
-rw-r--r-- | main.go | 23 |
1 files changed, 21 insertions, 2 deletions
@@ -1,10 +1,14 @@ package main import ( + "errors" "flag" "html/template" "log" "net/http" + "os" + "path" + "strings" "time" ) @@ -71,10 +75,27 @@ func intervalUpdates(d time.Duration, stopch <-chan bool, chans ...chan<- bool) } } +var tpl *template.Template + +func loadTemplate() { + gopaths := strings.Split(os.Getenv("GOPATH"), ":") + for _, p := range gopaths { + var err error + tpl, err = template.ParseFiles(path.Join(p, "src", "github.com", "kch42", "startpage", "template.html")) + if err == nil { + return + } + } + + panic(errors.New("could not find template in $GOPATH/src/github.com/kch42/startpage")) +} + func main() { laddr := flag.String("laddr", ":25145", "Listen on this port") flag.Parse() + loadTemplate() + pornch := make(chan bool) weatherch := make(chan bool) stopch := make(chan bool) @@ -91,8 +112,6 @@ func main() { log.Fatal(http.ListenAndServe(*laddr, nil)) } -var tpl = template.Must(template.ParseFiles("template.html")) - type TplData struct { Porn *EarthPorn Weather *Weather |