Files
storage/storage.go

49 lines
1.5 KiB
Go
Raw Normal View History

package storage
2025-07-06 22:59:08 +03:00
import (
"code.uint32.ru/tiny/storage/internal/filesystem"
"code.uint32.ru/tiny/storage/internal/natsobj"
"code.uint32.ru/tiny/storage/internal/vault"
2025-07-06 22:59:08 +03:00
)
var (
_ Storage = (*natsobj.Storage)(nil)
_ Storage = (*filesystem.Storage)(nil)
_ Storage = (*vault.Storage)(nil)
2025-07-06 22:59:08 +03:00
)
// Storage is a very basic object store.
type Storage interface {
// Save puts object with name 'key' into the store.
// If a key already exists it gets overwritten.
2025-07-06 22:59:08 +03:00
Save(key string, data []byte) error
// Load returns contents of object named 'key'.
2025-07-06 22:59:08 +03:00
Load(key string) ([]byte, error)
// Delete removes object named 'key' from the store.
// If key does not exist Delete returns nil.
2025-07-06 22:59:08 +03:00
Delete(key string) error
// Close must be called when you're done working with Storage.
Close() error
}
// NewNats connects to NATS messaging system and tries to create
// a new object storage with name 'bucket'. The returned Storage
// uses the created bucket as underlying physical store.
func NewNats(bucket string, url string) (Storage, error) {
return natsobj.Open(bucket, url)
}
// NewFS established a key/value within the directory 'path'
// and uses is as underlying physical store.
func NewFS(path string) (Storage, error) {
return filesystem.Open(path)
}
// NewVault connects to Vault at addr and uses path as base path for
// keys. Objects saved to Storage will be put at
// /path/key as new secrets.
// Bytes passed to storage will be base64 encoded and saved as string.
func NewVault(token string, path string, addr string) (Storage, error) {
return vault.Open(token, path, addr)
}