gas/template
template/fs
import "github.com/gasmod/gas/template/fs"Package fs reads templates from an fs.FS, such as an embed.FS, and cannot be written to.
Store implements gas.TemplateProvider with the contract of the template package. Register and RegisterFS return template.ErrReadOnly.
//go:embed templatesvar embedded embed.FS
sub, err := fs.Sub(embedded, "templates")if err != nil { return err}store, err := templatefs.NewStore(sub)()After this Get(ctx, “pages/home.html”) returns the content of
templates/pages/home.html. In an app, register the constructor:
gas.WithSingletonService[gas.TemplateProvider](templatefs.NewStore(sub))Lookup
Section titled “Lookup”Get looks a name up in these steps:
- It stats the name, so a name that is not there costs no directory read.
- It reads the name element by element. Each directory on the way is
read, a chunk of entries at a time, until an entry whose name is the
element byte for byte. So
Home.htmlis not found for a filehome.html, even on a file system that ignores case, such as macOS’s. - Every element but the last must be a directory, and the last a regular file.
The fs.FS must therefore support reading directories, as fs.WalkDir needs too.
The errors of a lookup:
- A directory that cannot be read, or a file that is there but cannot be read, is an error wrapping template.ErrRead.
- Nothing there is template.ErrTemplateNotFound.
- A file over the store’s maximum size, WithMaxSize, is an error wrapping template.ErrTooLarge, and is not read past it.
Listing
Section titled “Listing”List walks the fs.FS with fs.WalkDir and returns the names Get would find, sorted in byte order. It checks the context before each entry. A failure to walk is an error wrapping template.ErrList that names the path where the walk failed, inside the fs.FS.
Symlinks
Section titled “Symlinks”The store never follows a symlink it can tell is one. An entry that fs.ReadDir reports with fs.ModeSymlink is neither listed nor read, nor walked through on the way to a name. os.DirFS and os.Root’s FS report symlinks so. An fs.FS whose directory entries do not report them gets whatever its Open does.
os.DirFS follows symlinks out of its directory when a file is opened:
with evil.html a symlink to /etc/passwd.html, fs.ReadFile on it
returns that file. The store refuses evil.html because the directory
entry is a symlink, but a symlink that replaces a file between that
check and the read is still followed.
For a directory on disk, use gas/template/dir, which reads through an os.Root, opens every file without blocking and checks what it opened. Or give this store os.Root’s FS, which refuses every path out of the root:
root, err := os.OpenRoot("templates")store, err := templatefs.NewStore(root.FS())()An fs.FS has no way to open a file without blocking. So over a directory on disk, a FIFO put in place of a template between Get’s lookup and its open makes Get wait for a writer. Keep this store to file systems that do not change under it, such as an embed.FS, or to a directory no one the app does not trust can write to.
Concurrency
Section titled “Concurrency”A Store holds no state of its own beyond its fs.FS and extensions, and is safe for concurrent use when the fs.FS is, as embed.FS, os.DirFS and os.Root’s FS are.
Errors
Section titled “Errors”The constructor returns template.ErrNilFS for a nil fs.FS, and an error wrapping template.ErrInvalidExtension or template.ErrInvalidMaxSize for a bad option.
The methods return errors wrapping template.ErrInvalidName, template.ErrTemplateNotFound, template.ErrRead, template.ErrTooLarge, template.ErrList, the context’s error and template.ErrReadOnly, with the texts the template package lists.
A path in an error is the path inside the fs.FS. The error of an *fs.PathError is kept without its path, which some file systems give in full.
API reference
Section titled “API reference”func NewStore
Section titled “func NewStore”func NewStore(fsys iofs.FS, opts ...Option) func() (*Store, error)NewStore returns the constructor of a Store over fsys with opts applied, in the shape the gas container expects. The constructor returns template.ErrNilFS for a nil fsys and an error wrapping template.ErrInvalidExtension or template.ErrInvalidMaxSize for a bad option.
type Option
Section titled “type Option”type Option func(*Store)Option configures a Store.
func WithExtensions
Section titled “func WithExtensions”func WithExtensions(exts ...string) OptionWithExtensions sets the extensions a template name must end in. The constructor checks them, as described in the template package documentation.
Default: template.DefaultExtension.
func WithMaxSize
Section titled “func WithMaxSize”func WithMaxSize(n int64) OptionWithMaxSize sets the most bytes a template may hold. The constructor checks it with template.CheckMaxSize.
Default: template.DefaultMaxSize.
type Store
Section titled “type Store”type Store struct { // contains filtered or unexported fields}Store reads templates from an fs.FS and cannot be written to, as described in the package documentation. It is safe for concurrent use when its fs.FS is.
func (*Store) Get
Section titled “func (*Store) Get”func (s *Store) Get(_ context.Context, name string) ([]byte, error)Get returns the content of the template name, read from the file system, as described in the package documentation. Its errors wrap template.ErrInvalidName, template.ErrTemplateNotFound, template.ErrRead or template.ErrTooLarge.
func (*Store) List
Section titled “func (*Store) List”func (s *Store) List(ctx context.Context) ([]string, error)List returns the names of the templates of the file system in byte order, as described in the package documentation. A failure to walk it is an error wrapping template.ErrList.
func (*Store) Register
Section titled “func (*Store) Register”func (s *Store) Register(_ context.Context, name string, _ []byte) errorRegister returns an error wrapping template.ErrInvalidName for a name the store does not accept, and template.ErrReadOnly for any other.
func (*Store) RegisterFS
Section titled “func (*Store) RegisterFS”func (*Store) RegisterFS(context.Context, iofs.FS) errorRegisterFS returns template.ErrReadOnly.