Skip to content

Commit

Permalink
Split library & file list handlers for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
zefer committed Mar 20, 2015
1 parent 89640b6 commit c51345f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 75 deletions.
85 changes: 85 additions & 0 deletions handlers/file_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package handlers

import (
"encoding/json"
"fmt"
"net/http"
"path"
"sort"

"github.com/golang/glog"
"github.com/zefer/gompd/mpd"
)

type FileLister interface {
ListInfo(string) ([]mpd.Attrs, error)
}

type FileListEntry struct {
Path string `json:"path"`
Type string `json:"type"`
Base string `json:"base"`
LastModified string `json:"lastModified"`
}

type ByDate []*FileListEntry
type ByName []*FileListEntry

func (a ByDate) Len() int { return len(a) }
func (a ByDate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDate) Less(i, j int) bool { return a[i].LastModified < a[j].LastModified }

func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool { return a[i].Path < a[j].Path }

func FileListHandler(c FileLister) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
}
data, err := c.ListInfo(r.FormValue("uri"))
if err != nil {
glog.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
out := make([]*FileListEntry, len(data))
for i, item := range data {
for _, t := range []string{"file", "directory", "playlist"} {
if p, ok := item[t]; ok {
out[i] = &FileListEntry{
Path: p,
Type: t,
Base: path.Base(p),
LastModified: item["last-modified"],
}
break
}
}
}

// Sort the list by the specified field and direction.
sortFileList(out, r.FormValue("sort"), r.FormValue("direction"))

b, err := json.Marshal(out)
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, string(b))
})
}

func sortFileList(f []*FileListEntry, by string, dir string) {
var t sort.Interface
// Sort by date or name?
if by == "date" {
t = ByDate(f)
} else {
t = ByName(f)
}
// Sort asc or desc?
if dir == "desc" {
sort.Sort(sort.Reverse(t))
} else {
sort.Sort(t)
}
}
77 changes: 4 additions & 73 deletions handlers/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,16 @@ package handlers

import (
"encoding/json"
"fmt"
"net/http"
"path"
"sort"

"github.com/golang/glog"
"github.com/zefer/mothership/mpd"
)

type FileListEntry struct {
Path string `json:"path"`
Type string `json:"type"`
Base string `json:"base"`
LastModified string `json:"lastModified"`
type LibraryUpdater interface {
Update(string) (int, error)
}

type ByDate []*FileListEntry
type ByName []*FileListEntry

func (a ByDate) Len() int { return len(a) }
func (a ByDate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDate) Less(i, j int) bool { return a[i].LastModified < a[j].LastModified }

func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool { return a[i].Path < a[j].Path }

func FileListHandler(c *mpd.Client) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
}
data, err := c.C.ListInfo(r.FormValue("uri"))
if err != nil {
glog.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
out := make([]*FileListEntry, len(data))
for i, item := range data {
for _, t := range []string{"file", "directory", "playlist"} {
if p, ok := item[t]; ok {
out[i] = &FileListEntry{
Path: p,
Type: t,
Base: path.Base(p),
LastModified: item["last-modified"],
}
break
}
}
}

// Sort the list by the specified field and direction.
sortFileList(out, r.FormValue("sort"), r.FormValue("direction"))

b, err := json.Marshal(out)
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, string(b))
})
}

func sortFileList(f []*FileListEntry, by string, dir string) {
var t sort.Interface
// Sort by date or name?
if by == "date" {
t = ByDate(f)
} else {
t = ByName(f)
}
// Sort asc or desc?
if dir == "desc" {
sort.Sort(sort.Reverse(t))
} else {
sort.Sort(t)
}
}

func LibraryUpdateHandler(c *mpd.Client) http.Handler {
func LibraryUpdateHandler(c LibraryUpdater) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" && r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
Expand All @@ -100,7 +31,7 @@ func LibraryUpdateHandler(c *mpd.Client) http.Handler {
w.WriteHeader(http.StatusBadRequest)
return
}
_, err = c.C.Update(uri)
_, err = c.Update(uri)
if err != nil {
glog.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func main() {
http.Handle("/pause", handlers.PauseHandler(client.C))
http.Handle("/randomOn", handlers.RandomOnHandler(client.C))
http.Handle("/randomOff", handlers.RandomOffHandler(client.C))
http.Handle("/files", handlers.FileListHandler(client))
http.Handle("/files", handlers.FileListHandler(client.C))
http.Handle("/playlist", handlers.PlayListHandler(client.C))
http.Handle("/library/updated", handlers.LibraryUpdateHandler(client))
http.Handle("/library/updated", handlers.LibraryUpdateHandler(client.C))

// The front-end assets are served from a go-bindata file.
http.Handle("/", http.FileServer(&assetfs.AssetFS{Asset, AssetDir, ""}))
Expand Down

0 comments on commit c51345f

Please sign in to comment.