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:
36
internal/static/static.go
Normal file
36
internal/static/static.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user