diff --git a/cmd/cdr/generate.go b/cmd/cdr/generate.go new file mode 100644 index 0000000..6583705 --- /dev/null +++ b/cmd/cdr/generate.go @@ -0,0 +1,66 @@ +package main + +import ( + "log" + "net/http" + "os" + "path/filepath" + "strings" + + "github.com/thebaer/cdr" + "github.com/urfave/cli" +) + +var ( + cmdServe = cli.Command{ + Name: "preview", + Usage: "serve the mixtape site", + Action: serveAction, + } +) + +func newMixtape(wd string) (*cdr.Mixtape, error) { + m := &cdr.Mixtape{Tracks: []cdr.Track{}} + + filepath.Walk(wd, func(path string, i os.FileInfo, err error) error { + if !i.IsDir() && !strings.HasPrefix(i.Name(), ".") && i.Name() != "index.html" { + t, err := cdr.NewTrack(i.Name()) + if err == nil { + log.Printf("Skipping track %s: %v", i.Name(), err) + return nil + } + log.Println("Adding track", t.Title) + m.Tracks = append(m.Tracks, *t) + } + + return nil + }) + return m, nil +} + +func serveAction(c *cli.Context) error { + wd, err := os.Getwd() + if err != nil { + return err + } + + m, err := newMixtape(wd) + if err != nil { + return err + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if r.RequestURI != "/" { + log.Printf("GET %s", r.RequestURI) + http.ServeFile(w, r, filepath.Join(wd, r.RequestURI)) + return + } + err := cdr.Render(m, w) + if err != nil { + log.Printf("[ERROR] Render failed! %s", err) + } + log.Printf("GET /") + }) + + return http.ListenAndServe(":9991", nil) +} diff --git a/cmd/cdr/main.go b/cmd/cdr/main.go index 2d50c7f..6bd543a 100644 --- a/cmd/cdr/main.go +++ b/cmd/cdr/main.go @@ -16,6 +16,7 @@ func main() { } app.Commands = []*cli.Command{ + &cmdServe, &cmdClean, } diff --git a/render.go b/render.go new file mode 100644 index 0000000..6b389cd --- /dev/null +++ b/render.go @@ -0,0 +1,26 @@ +//go:generate inline -o templates.go -p cdr templates/parts.tmpl + +package cdr + +import ( + "html/template" + "io" + "io/ioutil" +) + +func Render(m *Mixtape, w io.Writer) error { + partsRawTmpl, err := ReadAsset("templates/parts.tmpl", false) + if err != nil { + return err + } + mixtapeRawTmpl, err := ioutil.ReadFile("mixtape.tmpl") + if err != nil { + return err + } + t, err := template.New("mixtape").Parse(string(mixtapeRawTmpl) + string(partsRawTmpl)) + if err != nil { + return err + } + t.ExecuteTemplate(w, "mixtape", m) + return nil +} diff --git a/sanitize.go b/sanitize.go index 8df5454..2e51859 100644 --- a/sanitize.go +++ b/sanitize.go @@ -13,17 +13,16 @@ import ( var trackNameReg = regexp.MustCompile("^([0-9]{2}).+") -func NewTrack(file string) *Track { +func NewTrack(file string) (*Track, error) { f, err := os.Open(file) if err != nil { - fmt.Printf("error loading file: %v", err) - return nil + return nil, fmt.Errorf("error loading file: %v", err) } defer f.Close() m, err := tag.ReadFrom(f) if err != nil { - return nil + return nil, fmt.Errorf("unable to read file: %v", err) } return &Track{ @@ -36,7 +35,10 @@ func NewTrack(file string) *Track { // RenameTrack takes a filename, opens it, reads the metadata, and returns both // the old and new filename. func RenameTrack(file string) string { - t := NewTrack(file) + t, err := NewTrack(file) + if err != nil { + return "" + } // Extract playlist track number from filename fMatch := trackNameReg.FindStringSubmatch(t.Filename) diff --git a/templates.go b/templates.go new file mode 100644 index 0000000..9677590 --- /dev/null +++ b/templates.go @@ -0,0 +1,81 @@ +// Code generated by "inline -o templates.go -p cdr templates/parts.tmpl" -- DO NOT EDIT -- + +package cdr + +import ( + "fmt" + "io/ioutil" +) + +func ReadAsset(file string, useLocal bool) ([]byte, error) { + if useLocal { + return ioutil.ReadFile(file) + } + if f, ok := files[file]; ok { + return []byte(f), nil + } + return nil, fmt.Errorf("file doesn't exist.") +} + +var files = map[string]string{ + "templates/parts.tmpl": `{{define "player"}} + {{with $x := index . 0}} + + {{end}} +