Documentation
¶
Overview ¶
Package storage provides an abstraction layer for storing snapshots and resources.
This package defines the Storage interface which can be implemented by various backends (filesystem, S3, etc.). Currently, only filesystem storage is implemented.
Snapshots are compressed HTML archives of bookmarked web pages, while resources are embedded assets like images, stylesheets, and scripts extracted from pages.
All stored content is compressed with gzip to save disk space. Files are organized in a two-character prefix directory structure based on their hash to avoid filesystem limitations with too many files in a single directory.
The package provides a global storage instance that is initialized at startup and used throughout the application. All functions panic if storage is accessed before initialization.
Example usage:
err := storage.Init(cfg.Storage)
if err != nil {
log.Fatal(err)
}
// Save a snapshot
key := storage.Hash(htmlContent) + ".html"
err = storage.SaveSnapshot(key, htmlContent)
// Retrieve a snapshot
reader, err := storage.GetSnapshot(key)
if err != nil {
return err
}
defer reader.Close()
Index ¶
- Variables
- func FS() iofs.FS
- func GetResource(key string) (io.ReadCloser, error)
- func GetResourceSize(key string) uint
- func GetResourceURL(key string) string
- func GetSnapshot(key string) (io.ReadCloser, error)
- func GetSnapshotSize(key string) uint
- func Hash(x []byte) string
- func Init(sCfg config.Storage) error
- func SaveResource(key string, resource []byte) error
- func SaveSnapshot(key string, snapshot []byte) error
- type Storage
Constants ¶
This section is empty.
Variables ¶
var ErrResourceNotFound = errors.New("resource not found")
ErrResourceNotFound is returned when a resource cannot be found.
var ErrSnapshotNotFound = errors.New("snapshot not found")
ErrSnapshotNotFound is returned when a snapshot cannot be found.
var ErrUninitialized = errors.New("uninitialized storage")
ErrUninitialized is returned when storage is accessed before initialization.
var ErrUnknownStorage = errors.New("unknown storage type")
ErrUnknownStorage is returned when an unknown storage type is configured.
Functions ¶
func FS ¶ added in v0.5.0
FS returns the storage backend as an io/fs.FS interface for file system operations. This allows reading stored files using the standard library's fs.FS methods. Panics if storage has not been initialized.
func GetResource ¶ added in v0.2.0
func GetResource(key string) (io.ReadCloser, error)
GetResource retrieves a resource by its key and returns a reader for its contents. Resources are typically images, stylesheets, or other embedded assets from web pages. The returned reader must be closed by the caller when done. Returns ErrUninitialized if storage is not initialized, or ErrResourceNotFound if the resource doesn't exist.
func GetResourceSize ¶ added in v0.2.0
GetResourceSize returns the size in bytes of a stored resource. Returns 0 if the resource doesn't exist. Panics if storage has not been initialized.
func GetResourceURL ¶ added in v0.3.0
GetResourceURL returns the URL path for accessing a resource via HTTP. This is typically used to generate URLs for embedded images and assets in HTML. Panics if storage has not been initialized.
func GetSnapshot ¶
func GetSnapshot(key string) (io.ReadCloser, error)
GetSnapshot retrieves a snapshot by its key and returns a reader for its contents. The returned reader must be closed by the caller when done. Returns ErrUninitialized if storage is not initialized, or ErrSnapshotNotFound if the snapshot doesn't exist.
func GetSnapshotSize ¶ added in v0.2.0
GetSnapshotSize returns the size in bytes of a stored snapshot. Returns 0 if the snapshot doesn't exist. Panics if storage has not been initialized.
func Hash ¶
Hash computes a SHA256 hash of the given bytes and returns it as a hexadecimal string. This is used to generate unique keys for storing snapshots and resources.
func Init ¶
Init initializes the storage backend with the given configuration. This must be called before using any other storage functions. Returns an error if the storage configuration is invalid or initialization fails.
func SaveResource ¶ added in v0.2.0
SaveResource stores a resource with the given key. Resources are typically images, stylesheets, or other embedded assets. The resource data is compressed before storage to save disk space. Returns ErrUninitialized if storage is not initialized, or an error if saving fails.
func SaveSnapshot ¶
SaveSnapshot stores a snapshot with the given key. The snapshot data is compressed before storage to save disk space. Returns ErrUninitialized if storage is not initialized, or an error if saving fails.
Types ¶
type Storage ¶
type Storage interface {
FS() (iofs.FS, error)
GetSnapshot(string) io.ReadCloser
GetSnapshotSize(string) uint
SaveSnapshot(string, []byte) error
SaveResource(string, []byte) error
GetResource(string) io.ReadCloser
GetResourceSize(string) uint
GetResourceURL(string) string
}
Storage defines the interface for snapshot and resource storage backends.