|
@@ -10,7 +10,7 @@ import (
|
|
|
|
|
|
|
|
type Song struct {
|
|
type Song struct {
|
|
|
Yid string
|
|
Yid string
|
|
|
- Name string
|
|
|
|
|
|
|
+ Title string
|
|
|
User string
|
|
User string
|
|
|
}
|
|
}
|
|
|
type Playlist struct {
|
|
type Playlist struct {
|
|
@@ -20,22 +20,19 @@ type Playlist struct {
|
|
|
|
|
|
|
|
var templates map[string]*template.Template
|
|
var templates map[string]*template.Template
|
|
|
const debug = true
|
|
const debug = true
|
|
|
|
|
+var db *mysql.Client
|
|
|
|
|
|
|
|
func home(w http.ResponseWriter, r *http.Request) {
|
|
func home(w http.ResponseWriter, r *http.Request) {
|
|
|
fmt.Fprintf(w, "path is %s", r.URL.Path[1:])
|
|
fmt.Fprintf(w, "path is %s", r.URL.Path[1:])
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
func playlist(w http.ResponseWriter, r *http.Request) {
|
|
func playlist(w http.ResponseWriter, r *http.Request) {
|
|
|
id := r.URL.Path[len("/p/"):]
|
|
id := r.URL.Path[len("/p/"):]
|
|
|
if len(id) != 8 {
|
|
if len(id) != 8 {
|
|
|
http.Redirect(w, r, "/", 303)
|
|
http.Redirect(w, r, "/", 303)
|
|
|
return
|
|
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 {
|
|
if err != nil {
|
|
|
http.Error(w, err.String(), http.StatusInternalServerError)
|
|
http.Error(w, err.String(), http.StatusInternalServerError)
|
|
|
return
|
|
return
|
|
@@ -54,7 +51,7 @@ func playlist(w http.ResponseWriter, r *http.Request) {
|
|
|
playlist := Playlist{Id: id, Songs: make([]*Song, 0, 2)}
|
|
playlist := Playlist{Id: id, Songs: make([]*Song, 0, 2)}
|
|
|
for {
|
|
for {
|
|
|
song := new(Song)
|
|
song := new(Song)
|
|
|
- query.BindResult(&song.Yid, &song.Name, &song.User)
|
|
|
|
|
|
|
+ query.BindResult(&song.Yid, &song.Title, &song.User)
|
|
|
eof, err := query.Fetch()
|
|
eof, err := query.Fetch()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
http.Error(w, err.String(), http.StatusInternalServerError)
|
|
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)
|
|
playlist.Songs = append(playlist.Songs, song)
|
|
|
}
|
|
}
|
|
|
|
|
+ err = query.FreeResult()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ http.Error(w, err.String(), http.StatusInternalServerError)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
if debug {
|
|
if debug {
|
|
|
t, err := template.ParseFile("templates/p.html")
|
|
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() {
|
|
func main() {
|
|
|
templates = make(map[string]*template.Template)
|
|
templates = make(map[string]*template.Template)
|
|
|
for _, path := range []string{"p"} {
|
|
for _, path := range []string{"p"} {
|
|
@@ -97,7 +141,15 @@ func main() {
|
|
|
templates[path] = t
|
|
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("/", home)
|
|
|
http.HandleFunc("/p/", playlist)
|
|
http.HandleFunc("/p/", playlist)
|
|
|
|
|
+ http.HandleFunc("/add/", add)
|
|
|
http.ListenAndServe(":8000", nil)
|
|
http.ListenAndServe(":8000", nil)
|
|
|
}
|
|
}
|