memfs

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2023 License: MIT Imports: 13 Imported by: 0

README

An in memory fs.FS

GoDoc

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.

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

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 Create

func Create() *FileSystem

Create creates an empty FileSystem instance.

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

func (fsys *FileSystem) Create(name, mimetype string, modtime time.Time, r io.Reader) error

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.

func (*FileSystem) Stat

func (fsys *FileSystem) Stat(name string) (fs.FileInfo, error)

Stat implements fs.StatFS, returning a fs.FileInfo that describes the file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL