diff options
| -rw-r--r-- | data.go | 22 | ||||
| -rw-r--r-- | main.go | 11 |
2 files changed, 11 insertions, 22 deletions
@@ -34,8 +34,8 @@ type Session struct { type DataStore interface { PutSession(*Session) - GetSession(key string) (*Session, bool) - GetPassword(userName string) (string, bool) + GetSession(key string) (*Session, error) + GetPassword(userName string) (string, error) PutScrobbles([]Scrobble) Api } @@ -71,28 +71,18 @@ func (store *SqlStore) PutSession(s *Session) { s.User, s.Key, s.Client, s.Protocol, s.Created) } -func (store *SqlStore) GetSession(key string) (*Session, bool) { +func (store *SqlStore) GetSession(key string) (*Session, error) { s := &Session{} row := store.QueryRow("SELECT * FROM sessions WHERE key = ?", key) err := row.Scan(&s.User, &s.Key, &s.Client, &s.Protocol, &s.Created) - if err == sql.ErrNoRows { - return s, false - } else if err != nil { - fmt.Println(err) - } - return s, true + return s, err } -func (store *SqlStore) GetPassword(name string) (string, bool) { +func (store *SqlStore) GetPassword(name string) (string, error) { var password string row := store.QueryRow("SELECT password FROM users WHERE name = ?", name) err := row.Scan(&password) - if err == sql.ErrNoRows { - return password, false - } else if err != nil { - fmt.Println(err) - } - return password, true + return password, err } func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) { @@ -36,9 +36,8 @@ func mainHandler(ds DataStore, w http.ResponseWriter, r *http.Request) { user := r.FormValue("u") auth := r.FormValue("a") - password, ok := ds.GetPassword(user) - - if (md5hex(password+timestamp) != auth) || !ok { + password, err := ds.GetPassword(user) + if (md5hex(password+timestamp) != auth) || err != nil { fmt.Fprintf(w, "BADAUTH\n") return } @@ -123,7 +122,7 @@ func parseScrobbles(values url.Values, session *Session) []Scrobble { } func scrobbleHandler(ds DataStore, w http.ResponseWriter, r *http.Request) { - if session, ok := ds.GetSession(r.FormValue("s")); ok { + if session, err := ds.GetSession(r.FormValue("s")); err != nil { scrobbles := parseScrobbles(r.Form, session) ds.PutScrobbles(scrobbles) fmt.Fprintf(w, "OK\n") @@ -133,8 +132,8 @@ func scrobbleHandler(ds DataStore, w http.ResponseWriter, r *http.Request) { } func nowPlayingHandler(ds DataStore, w http.ResponseWriter, r *http.Request) { - if _, ok := ds.GetSession(r.FormValue("s")); ok { - fmt.Fprintf(w, "OK\n") + if _, err := ds.GetSession(r.FormValue("s")); err != nil { + fmt.Fprintln(w, "OK") } else { fmt.Fprintf(w, "BADSESSION\n") } |
