diff --git a/gotour/appengine.go b/gotour/appengine.go index cb46850..a410989 100644 --- a/gotour/appengine.go +++ b/gotour/appengine.go @@ -26,11 +26,14 @@ func init() { if err != nil { panic(err) } + if err := initTour("."); err != nil { + panic(err) + } } func rootHandler(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) - if err := renderTour(w, "."); err != nil { + if err := renderTour(w); err != nil { c.Criticalf("template render: %v", err) } } diff --git a/gotour/local.go b/gotour/local.go index 326a2b3..96a760a 100644 --- a/gotour/local.go +++ b/gotour/local.go @@ -89,6 +89,22 @@ func main() { log.Println("Serving content from", root) + host, port, err := net.SplitHostPort(*httpListen) + if err != nil { + log.Fatal(err) + } + if host == "" { + host = "localhost" + } + if host != "127.0.0.1" && host != "localhost" { + log.Print(localhostWarning) + } + httpAddr = host + ":" + port + + if err := initTour(root); err != nil { + log.Fatal(err) + } + fs := http.FileServer(http.Dir(root)) http.Handle("/favicon.ico", fs) http.Handle("/static/", fs) @@ -96,7 +112,7 @@ func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { - if err := renderTour(w, root); err != nil { + if err := renderTour(w); err != nil { log.Println(err) } return @@ -111,18 +127,6 @@ func main() { log.Fatal(err) } - host, port, err := net.SplitHostPort(*httpListen) - if err != nil { - log.Fatal(err) - } - if host == "" { - host = "localhost" - } - if host != "127.0.0.1" && host != "localhost" { - log.Print(localhostWarning) - } - - httpAddr = host + ":" + port go func() { url := "http://" + httpAddr if waitServer(url) && *openBrowser && startBrowser(url) { diff --git a/gotour/tour.go b/gotour/tour.go index 47a947f..180c193 100644 --- a/gotour/tour.go +++ b/gotour/tour.go @@ -22,9 +22,11 @@ func init() { present.PlayEnabled = true } -// renderTour loads tour.article and the relevant HTML templates from the given -// tour root, and renders the template to the provided writer. -func renderTour(w io.Writer, root string) error { +var tourContent []byte + +// initTour loads tour.article and the relevant HTML templates from the given +// tour root, and renders the template to the tourContent global variable. +func initTour(root string) error { // Open and parse source file. source := filepath.Join(root, "tour.article") f, err := os.Open(source) @@ -47,7 +49,21 @@ func renderTour(w io.Writer, root string) error { } // Render. - return doc.Render(w, t) + buf := new(bytes.Buffer) + if err := doc.Render(buf, t); err != nil { + return err + } + tourContent = buf.Bytes() + return nil +} + +// renderTour writes the tour content to the provided Writer. +func renderTour(w io.Writer) error { + if tourContent == nil { + panic("renderTour called before successful initTour") + } + _, err := w.Write(tourContent) + return err } // nocode returns true if the provided Section contains