|
|
@@ -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)
|
|
|
- }
|
|
|
-}
|