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 +++++++++++++++++++++++---------------------------- templates/footer.html | 3 ++ templates/header.html | 13 ++++++++ templates/home.html | 10 ++++++ templates/p.html | 74 +++++++++++++++++++------------------------- 5 files changed, 95 insertions(+), 91 deletions(-) create mode 100644 templates/footer.html create mode 100644 templates/header.html create mode 100644 templates/home.html 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) - } -} diff --git a/templates/footer.html b/templates/footer.html new file mode 100644 index 0000000..15b9011 --- /dev/null +++ b/templates/footer.html @@ -0,0 +1,3 @@ + + + diff --git a/templates/header.html b/templates/header.html new file mode 100644 index 0000000..8b65ad6 --- /dev/null +++ b/templates/header.html @@ -0,0 +1,13 @@ + + + + + Audio Axis + + + + + + diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..7e7e9e3 --- /dev/null +++ b/templates/home.html @@ -0,0 +1,10 @@ +{{template "header.html"}} + +
+ Audio Axis +
+
+ Welcome to Audio Axis! +
+ +{{template "footer.html"}} diff --git a/templates/p.html b/templates/p.html index 79971c1..d434052 100644 --- a/templates/p.html +++ b/templates/p.html @@ -1,45 +1,33 @@ - - - - - Audio Axis - - - - +{{template "header.html"}} - -
- Audio Axis -
- - -
- - - -
-
    -
-
-
-
- {{$first := "82KEVKGRAhI"}} - - - - - - - - - - +
+ Audio Axis +
+ + +
+ + + +
+
    +
+
+
+
+{{$first := "82KEVKGRAhI"}} + + + + + + + + + - +{{template "footer.html"}} -- cgit v1.2.3