From 8a05f63c5c9d52c0c6165d9785498a13e9306ced Mon Sep 17 00:00:00 2001 From: raylu Date: Tue, 9 Aug 2011 22:33:17 -0700 Subject: add homepage template --- main.go | 86 +++++++++++++++++++++++++++++------------------------------------ 1 file changed, 38 insertions(+), 48 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 761a895..3571e42 100644 --- a/main.go +++ b/main.go @@ -19,41 +19,57 @@ type Playlist struct { Id string } -var templates map[string]*template.Template +var templates *template.Set const debug = true -func home(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Welcome to Audio Axis!") -} +func main() { + templates = template.SetMust(template.ParseTemplateFiles("templates/*.html")) -func playlist(w http.ResponseWriter, r *http.Request) { - id := r.URL.Path[len("/p/"):] - if len(id) != 8 { - http.Redirect(w, r, "/", 303) - return + initDb() + + http.HandleFunc("/", home) + http.HandleFunc("/p/", playlist) + http.HandleFunc("/add/", add) + http.HandleFunc("/remove/", remove) + http.HandleFunc("/move/", move) + http.HandleFunc("/poll/", poll) + err := http.ListenAndServe("localhost:8000", nil) + if err != nil { + fmt.Println(err) + os.Exit(1) } - playlist := Playlist{Id: id} +} +func renderPage(w http.ResponseWriter, page string, data interface{}) { if debug { - t, err := template.ParseFile("templates/p.html") - if err != nil { - http.Error(w, err.String(), http.StatusInternalServerError) - return - } - err = t.Execute(w, playlist) + var err os.Error + templates, err = template.ParseTemplateFiles("templates/*.html") if err != nil { - w.Write([]byte(err.String())) fmt.Fprintln(os.Stderr, err.String()) + http.Error(w, err.String(), http.StatusInternalServerError) return } - } else { - err := templates["p"].Execute(w, playlist) - if err != nil { - fmt.Fprintln(os.Stderr, err.String()) - } + } + err := templates.Execute(w, page + ".html", data) + if err != nil { + fmt.Fprintln(os.Stderr, err.String()) } } +func home(w http.ResponseWriter, r *http.Request) { + renderPage(w, "home", nil) +} + +func playlist(w http.ResponseWriter, r *http.Request) { + id := r.URL.Path[len("/p/"):] + if len(id) != 8 { + http.Redirect(w, r, "/", 303) + return + } + p := Playlist{Id: id} + renderPage(w, "p", p) +} + func add(w http.ResponseWriter, r *http.Request) { q := r.URL.Query() db := <-dbPool @@ -283,29 +299,3 @@ func poll(w http.ResponseWriter, r *http.Request) { w.Write([]byte("[]")) } } - -func main() { - templates = make(map[string]*template.Template) - for _, path := range []string{"p"} { - t, err := template.ParseFile("templates/" + path + ".html") - if err != nil { - fmt.Println(err) - return - } - templates[path] = t - } - - initDb() - - http.HandleFunc("/", home) - http.HandleFunc("/p/", playlist) - http.HandleFunc("/add/", add) - http.HandleFunc("/remove/", remove) - http.HandleFunc("/move/", move) - http.HandleFunc("/poll/", poll) - err := http.ListenAndServe("localhost:8000", nil) - if err != nil { - fmt.Println(err) - os.Exit(1) - } -} -- cgit v1.2.3