From 21827d68b521e22ac6e38b40bca49bc2885b8212 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Mon, 3 Mar 2014 13:18:33 +0100 Subject: Template will now be loaded, even if not started from same directory --- main.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 952802a..edfe037 100644 --- a/main.go +++ b/main.go @@ -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 -- cgit v1.2.3-54-g00ecf