error-pages/templates/embed.go

40 lines
825 B
Go
Raw Normal View History

2024-06-21 23:32:10 +00:00
package templates
import (
"embed"
"io/fs"
"path/filepath"
"strings"
)
//go:embed *.html
var content embed.FS
2024-06-25 21:06:02 +00:00
// BuiltIn returns a map of built-in templates. The key is the template name and the value is the template content.
func BuiltIn() map[string]string {
2024-06-21 23:32:10 +00:00
var (
2024-06-25 21:06:02 +00:00
list, _ = fs.ReadDir(content, ".") // error check is covered by unit tests
2024-06-21 23:32:10 +00:00
result = make(map[string]string, len(list))
)
for _, file := range list {
if data, err := fs.ReadFile(content, file.Name()); err == nil {
var (
fileName = filepath.Base(file.Name())
ext = filepath.Ext(fileName)
templateName string
)
if ext != "" && fileName != ext {
templateName = strings.TrimSuffix(fileName, ext)
} else {
templateName = fileName
}
result[templateName] = string(data)
}
}
return result
}