diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 68 |
1 files changed, 60 insertions, 8 deletions
@@ -10,7 +10,7 @@ import ( type Song struct { Yid string - Name string + Title string User string } type Playlist struct { @@ -20,22 +20,19 @@ type Playlist struct { var templates map[string]*template.Template const debug = true +var db *mysql.Client 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` = ?;") + query, err := db.Prepare("SELECT `yid`,`title`,`user` FROM `playlist` JOIN `song` WHERE `id` = ?;") if err != nil { http.Error(w, err.String(), http.StatusInternalServerError) return @@ -54,7 +51,7 @@ func playlist(w http.ResponseWriter, r *http.Request) { playlist := Playlist{Id: id, Songs: make([]*Song, 0, 2)} for { song := new(Song) - query.BindResult(&song.Yid, &song.Name, &song.User) + query.BindResult(&song.Yid, &song.Title, &song.User) eof, err := query.Fetch() if err != nil { http.Error(w, err.String(), http.StatusInternalServerError) @@ -65,6 +62,11 @@ func playlist(w http.ResponseWriter, r *http.Request) { } playlist.Songs = append(playlist.Songs, song) } + err = query.FreeResult() + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } if debug { t, err := template.ParseFile("templates/p.html") @@ -86,6 +88,48 @@ func playlist(w http.ResponseWriter, r *http.Request) { } } +func add(w http.ResponseWriter, r *http.Request) { + query, err := db.Prepare("SELECT `pid` FROM `playlist` WHERE `id` = ?;") + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + q := r.URL.Query() + err = query.BindParams(q.Get("pid")) + 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 + } + var pid int + query.BindResult(&pid) + _, err = query.Fetch() + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + err = query.FreeResult() + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } + + sql :="INSERT INTO `song` (`pid`,`yid`,`title`,`user`) VALUES(%d,'%s','%s','%s')" + sql = fmt.Sprintf(sql, pid, + db.Escape(q.Get("yid")), + db.Escape(q.Get("title")), + db.Escape(q.Get("user"))) + err = db.Query(sql) + if err != nil { + http.Error(w, err.String(), http.StatusInternalServerError) + return + } +} + func main() { templates = make(map[string]*template.Template) for _, path := range []string{"p"} { @@ -97,7 +141,15 @@ func main() { templates[path] = t } + var err os.Error + db, err = mysql.DialTCP("raylu.net", "audio", "audio", "audio") + if err != nil { + fmt.Println(err) + os.Exit(1) + } + http.HandleFunc("/", home) http.HandleFunc("/p/", playlist) + http.HandleFunc("/add/", add) http.ListenAndServe(":8000", nil) } |