go.astrophena.name/base Module
Base Go packages for my projects.
package hashfs
import "go.astrophena.name/base/web/internal/hashfs"
Package hashfs implements a fs.FS that appends SHA256 hashes to filenames to allow for aggressive HTTP caching.
Index
- func FileServer(fsys fs.FS) http.Handler
- func FormatName(filename, hash string) string
- func ParseName(filename string) (base, hash string)
- type FS
Functions
func FileServer
func FileServer(fsys fs.FS) http.Handler
FileServer returns an http.Handler for serving FS files. It provides a simplified implementation of http.FileServer which is used to aggressively cache files on the client since the file hash is in the filename.
Because FileServer is focused on small known path files, several features of http.FileServer have been removed including canonicalizing directories, defaulting index.html pages, precondition checks, & content range headers.
func FormatName
func FormatName(filename, hash string) string
FormatName returns a hash name that inserts hash before the filename's extension. If no extension exists on filename then the hash is appended. Returns blank string the original filename if hash is blank. Returns a blank string if the filename is blank.
func ParseName
func ParseName(filename string) (base, hash string)
ParseName splits formatted hash filename into its base & hash components.
Types
type FS
type FS struct { // contains filtered or unexported fields }
FS represents an fs.FS file system that can optionally use content addressable hashes in the filename. This allows the caller to aggressively cache the data since the filename will change if the data changes.
func NewFS
func NewFS(fsys fs.FS) *FS
NewFS returns a new FS.
func (*FS) HashName
func (fsys *FS) HashName(name string) string
HashName returns the hash name for a path, if exists. Otherwise returns the original path.
func (*FS) Open
func (fsys *FS) Open(name string) (fs.File, error)
Open returns a reference to the named file. If name is a hash name then the underlying file is used.
func (*FS) ParseName
func (fsys *FS) ParseName(filename string) (base, hash string)
ParseName splits formatted hash filename into its base & hash components.