mirror of
https://github.com/tarampampam/error-pages.git
synced 2024-08-30 18:22:40 +00:00
40 lines
825 B
Go
40 lines
825 B
Go
package templates
|
|
|
|
import (
|
|
"embed"
|
|
"io/fs"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed *.html
|
|
var content embed.FS
|
|
|
|
// 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 {
|
|
var (
|
|
list, _ = fs.ReadDir(content, ".") // error check is covered by unit tests
|
|
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
|
|
}
|