Skip to content
This repository has been archived by the owner on Nov 22, 2018. It is now read-only.

Commit

Permalink
go-tour: render tour just once at startup
Browse files Browse the repository at this point in the history
R=golang-dev, campoy
CC=golang-dev
https://codereview.appspot.com/7331046
  • Loading branch information
adg committed Feb 15, 2013
1 parent ff1825b commit 4c02382
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
5 changes: 4 additions & 1 deletion gotour/appengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
30 changes: 17 additions & 13 deletions gotour/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,30 @@ 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)
http.Handle("/talks/", fs)

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
Expand All @@ -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) {
Expand Down
24 changes: 20 additions & 4 deletions gotour/tour.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 4c02382

Please sign in to comment.