diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | main.go | 98 | ||||
-rw-r--r-- | templates/p.html | 8 |
4 files changed, 115 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..351c99d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +aa +*.6 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..47cbb29 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +all: aa + +main.6: main.go + 6g main.go + +aa: main.6 + 6l -o aa main.6 @@ -0,0 +1,98 @@ +package main + +import ( + "fmt" + "http" + "template" + mysql "github.com/Philio/GoMySQL" + "os" +) + +type Song struct { + Yid string + Name string + User string +} +type Playlist struct { + Id string + Songs []*Song +} + +var templates map[string]*template.Template +const debug = true + +func home(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "path is %s", r.URL.Path[1:]) +} +func playlist(w http.ResponseWriter, r *http.Request) { + id := r.URL.Path[len("/p/"):] + if len(id) != 8 { + http.Redirect(w, r, "/", 303) + return + } + db, err := mysql.DialTCP("raylu.net", "audio", "audio", "audio") + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + query, err := db.Prepare("SELECT `yid`,`name`,`user` FROM `playlist` JOIN `song` WHERE `id` = ?;") + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + err = query.BindParams(id) + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + err = query.Execute() + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + + playlist := Playlist{Id: id, Songs: make([]*Song, 0, 2)} + for { + song := new(Song) + query.BindResult(&song.Yid, &song.Name, &song.User) + eof, err := query.Fetch() + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + if eof { + break + } + playlist.Songs = append(playlist.Songs, song) + } + + if debug { + t, err := template.ParseFile("templates/p.html", nil) + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + err = t.Execute(w, playlist) + if err != nil { + w.Write([]byte(err.String())) + fmt.Fprintln(os.Stderr, err.String()) + return + } + } else { + err = templates["p"].Execute(w, playlist) + if err != nil { + fmt.Fprintln(os.Stderr, err.String()) + } + } +} + +func main() { + templates = make(map[string]*template.Template) + for _, t:= range []string{"p"} { + templates[t] = template.MustParseFile("templates/" + t + ".html", nil) + } + + http.HandleFunc("/", home) + http.HandleFunc("/p/", playlist) + http.ListenAndServe(":8000", nil) +} diff --git a/templates/p.html b/templates/p.html new file mode 100644 index 0000000..14b367c --- /dev/null +++ b/templates/p.html @@ -0,0 +1,8 @@ +Playlist: {Id} +<br /> + +{.repeated section Songs} + http://www.youtube.com/watch?v={Yid} +{.alternates with} + <br /> +{.end} |