summaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorraylu <raylu@mixpanel.com>2011-08-09 22:33:17 -0700
committerraylu <raylu@mixpanel.com>2011-08-09 22:33:17 -0700
commit8a05f63c5c9d52c0c6165d9785498a13e9306ced (patch)
treef80dc1293d63c01bfc8264019e1e0323d1c15818 /main.go
parentb5f572860a2a8d94103d2b27a7f170243f885e97 (diff)
downloadaudioaxis-8a05f63c5c9d52c0c6165d9785498a13e9306ced.tar.xz
add homepage template
Diffstat (limited to 'main.go')
-rw-r--r--main.go86
1 files changed, 38 insertions, 48 deletions
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)
- }
-}