Files
remark42/backend/app/templates/templates.go
T
Dmitry Verkhoturov 86d059bf99 move templates from rakyll/statik to go:embed
There is no need for the rakyll/statik package starting with Go 1.16,
which provides us with tools for embedding files
without third-party libraries.
2022-07-29 19:07:49 +02:00

25 lines
522 B
Go

package templates
import (
"embed"
"io/fs"
"os"
"path/filepath"
)
//go:embed static
var templateFS embed.FS
// Read reads either template from disk if it exists, or from embedded template
func Read(path string) ([]byte, error) {
if _, err := os.Stat(filepath.Clean(path)); err == nil {
return os.ReadFile(filepath.Clean(path))
}
// remove "static/" prefix from path
var contentFS, err = fs.Sub(templateFS, "static")
if err != nil {
return nil, err
}
return fs.ReadFile(contentFS, filepath.Clean(path))
}