Refactor out the use of rice in favor of embed (#2)

Co-authored-by: James Mills <prologic@shortcircuit.net.au>
Reviewed-on: https://git.mills.io/prologic/spyda/pulls/2
This commit is contained in:
James Mills
2022-10-05 00:19:23 +00:00
parent 7001f3bf51
commit d4ab395515
13 changed files with 270 additions and 178 deletions

36
internal/static/static.go Normal file
View File

@@ -0,0 +1,36 @@
package static
import (
"embed"
"io/fs"
"log"
"net/http"
)
//go:embed css/*.css
//go:embed img/*.png img/*.svg
//go:embed js/*.js
var files embed.FS
// MustGetFile returns the contents of a file from static as bytes.
func MustGetFile(name string) []byte {
b, err := files.ReadFile(name)
if err != nil {
panic(err)
}
return b
}
// GetFilesystem returns a http.FileSystem for the static files.
func GetFilesystem() http.FileSystem {
return http.FS(files)
}
// GetSubFilesystem returns a http.FileSystem for the static sub-files.
func GetSubFilesystem(name string) http.FileSystem {
fsys, err := fs.Sub(files, name)
if err != nil {
log.Fatalf("error loading sub-filesystem for %q: %s", name, err)
}
return http.FS(fsys)
}