diff --git a/go.mod b/go.mod index ae7a310..0c9034c 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,10 @@ module github.com/patwie/digitalmusicstand -go 1.12 +go 1.19 require ( github.com/akamensky/argparse v1.1.0 github.com/go-chi/chi/v5 v5.0.0 - github.com/markbates/pkger v0.17.1 + github.com/gobuffalo/here v0.6.7 // indirect gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index e45aa4c..be19d9d 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/go-chi/chi/v5 v5.0.0 h1:DBPx88FjZJH3FsICfDAfIfnb7XxKIYVGG6lOPlhENAg= github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs= github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI= github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM= +github.com/gobuffalo/here v0.6.7 h1:hpfhh+kt2y9JLDfhYUxxCRxQol540jsVfKUZzjlbp8o= +github.com/gobuffalo/here v0.6.7/go.mod h1:vuCfanjqckTuRlqAitJz6QC4ABNnS27wLb816UhsPcc= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -17,8 +19,11 @@ github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -26,3 +31,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 175112e..e54fc2f 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,11 @@ package main import ( + "embed" "encoding/json" "fmt" "html/template" - "io/ioutil" + "io/fs" "log" "net/http" "os" @@ -12,16 +13,33 @@ import ( "github.com/akamensky/argparse" "github.com/go-chi/chi/v5" - "github.com/markbates/pkger" ) +//go:embed static.min/* +var embededFiles embed.FS + +func getAllFilenames(efs fs.FS) (files []string, err error) { + if err := fs.WalkDir(efs, ".", func(path string, d fs.DirEntry, err error) error { + if d.IsDir() { + return nil + } + + files = append(files, path) + + return nil + }); err != nil { + return nil, err + } + + return files, nil +} + func FileServer(r chi.Router, path string, root http.FileSystem) { if strings.ContainsAny(path, "{}*") { panic("FileServer does not permit URL parameters.") } fs := http.StripPrefix(path, http.FileServer(root)) - if path != "/" && path[len(path)-1] != '/' { r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP) path += "/" @@ -34,11 +52,14 @@ func FileServer(r chi.Router, path string, root http.FileSystem) { } func ReadStringFromFile(path string) string { - packagedFile, _ := pkger.Open(path) - html_buf, err := ioutil.ReadAll(packagedFile) + fmt.Println(path) + fsys, err := fs.Sub(embededFiles, "static.min") if err != nil { panic(err) } + + html_buf, err := fs.ReadFile(fsys, path) + return string(html_buf) } @@ -54,7 +75,11 @@ func main() { log.Fatal(parser.Usage(err)) } - html := ReadStringFromFile("/static.min/index.html") + staticFilesFS, err := fs.Sub(embededFiles, "static.min") + if err != nil { + panic(err) + } + indexHTML := ReadStringFromFile("index.html") r := chi.NewRouter() @@ -68,7 +93,7 @@ func main() { if *allowUploads { r.Post("/upload", UploadSheet(*sheetDir)) - html = strings.Replace(html, "data-upload=disabled", "data-upload=enabled", 1) + indexHTML = strings.Replace(indexHTML, "data-upload=disabled", "data-upload=enabled", 1) } else { r.Post("/upload", func(w http.ResponseWriter, r *http.Request) { @@ -79,7 +104,7 @@ func main() { } t := template.New("index") - t, err = t.Parse(html) + t, err = t.Parse(indexHTML) if err != nil { panic(err) } @@ -90,7 +115,7 @@ func main() { }) FileServer(r, "/sheet", http.Dir(*sheetDir)) - FileServer(r, "/", pkger.Dir("/static.min")) + FileServer(r, "/", http.FS(staticFilesFS)) fmt.Println("Digital Music Stand (https://github.com/PatWie/digitalmusicstand)") fmt.Println("listens at ", *addr)