Documentation ¶
Overview ¶
Package memfs implements an in memory fs.FS.
The filesystem can be statically generated, or loaded (and modified) at runtime. It is safe for concurrent reads (not writes), and biased towards read performance.
File names should be valid according to fs.ValidPath. Directories are implicit. Files can be gzip-compressed in memory. Methods are provided to serve gziped content directly to accepting HTTP clients.
Usage:
assets, err = memfs.LoadCompressed(http.Dir("static"), gzip.BestCompression) if err != nil { log.Fatal(err) } log.Fatal(http.ListenAndServe("localhost:http", assets))
Index ¶
- type FileSystem
- func (fsys *FileSystem) Create(name, mimetype string, modtime time.Time, r io.Reader) error
- func (fsys *FileSystem) CreateCompressed(name, mimetype string, modtime time.Time, r io.Reader, level int) error
- func (fsys *FileSystem) CreateString(name, mimetype string, modtime time.Time, hash uint32, size int, ...)
- func (fsys *FileSystem) Open(name string) (fs.File, error)
- func (fsys *FileSystem) ReadFile(name string) ([]byte, error)
- func (fsys *FileSystem) ServeContent(w http.ResponseWriter, r *http.Request, name string)
- func (fsys *FileSystem) ServeFile(w http.ResponseWriter, r *http.Request, name string)
- func (fsys *FileSystem) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (fsys *FileSystem) Stat(name string) (fs.FileInfo, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileSystem ¶
type FileSystem struct {
// contains filtered or unexported fields
}
FileSystem is the in memory fs.FS implementation.
func Load ¶
func Load(in fs.FS) (*FileSystem, error)
Load loads the contents of an fs.FS into a new FileSystem instance.
func LoadCompressed ¶
func LoadCompressed(in fs.FS, level int) (*FileSystem, error)
LoadCompressed loads the contents of an fs.FS into a new FileSystem instance. Files are gzip-compressed with the specified compression level.
func (*FileSystem) Create ¶
Create creates a file. Overwrites an existing file (but not a directory). Sniffs the MIME type if none is provided.
func (*FileSystem) CreateCompressed ¶
func (fsys *FileSystem) CreateCompressed(name, mimetype string, modtime time.Time, r io.Reader, level int) error
CreateCompressed creates a compressed file. Overwrites an existing file (but not a directory). Files are gzip-compressed with the specified compression level. Sniffs the MIME type if none is provided.
func (*FileSystem) CreateString ¶
func (fsys *FileSystem) CreateString(name, mimetype string, modtime time.Time, hash uint32, size int, content string)
CreateString creates a file from a string. This intended to be used by code generators. Bad things happen if you violate its expectations.
Overwrites an existing file. Files are expected to be passed in fs.WalkDir order. MIME type will NOT be sniffed and content will NOT be compressed. If size != len(content), content is assumed to be gzip-compressed, and size its uncompressed size.
func (*FileSystem) Open ¶
func (fsys *FileSystem) Open(name string) (fs.File, error)
Open implements fs.FS, opening files for reading. Compressed files are decompressed on-the-fly. Seeking compressed files is emulated and can be extremely slow.
func (*FileSystem) ReadFile ¶ added in v0.2.0
func (fsys *FileSystem) ReadFile(name string) ([]byte, error)
ReadFile implements fs.ReadFileFS, reading the named file and returning its contents. Compressed files are decompressed on-the-fly.
func (*FileSystem) ServeContent ¶
func (fsys *FileSystem) ServeContent(w http.ResponseWriter, r *http.Request, name string)
ServeContent replaces http.ServeContent. Serves the named file. No redirects or rewrites.
func (*FileSystem) ServeFile ¶
func (fsys *FileSystem) ServeFile(w http.ResponseWriter, r *http.Request, name string)
ServeFile replaces http.ServeFile. Redirects to canonical paths. Serves index.html for directories, 404.html for not found. Doesn't list directories.
func (*FileSystem) ServeHTTP ¶
func (fsys *FileSystem) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler using ServeFile. Replaces http.FileServer.