Documentation ¶
Overview ¶
Package base is where we can define some interfaces and global variables to access services like cache and storage.
In theory, I would have preferred to avoid global variables. But the code base already exists and I don't want to take too much time to refactor it. Using interfaces and global variables is a good compromise to my eyes. It allows to easily test each service in its own package and to use an in-memory service for other tests.
Index ¶
- Constants
- Variables
- func DBName(name string) string
- func NewFileNotFoundError(cause error) error
- func NewInternalError(cause error) error
- func NewTooLargeError(cause error) error
- func SprintfJSON(format string, a ...interface{}) json.RawMessage
- func VirtualDBName(virtualSpaceName string) string
- func VirtualVersionsDBName(virtualSpaceName string) string
- type Asset
- type AssetStore
- type Cache
- type CleanParameters
- type ConfigParameters
- type Error
- type Key
- type Prefix
- type Value
- type VirtualSpace
- type VirtualStorage
- type WalkFn
Constants ¶
const DefaultCacheTTL = 5 * time.Minute
DefaultCacheTTL is the default duration for caching items before they are expired and removed from the cache.
Variables ¶
var ( // ErrFileNotFound is returned when trying to read a file that does not // exist. ErrFileNotFound = errors.New("File not found") // ErrTooLarge is returned when the size limit is reached. ErrTooLarge = errors.New("File is too large") // ErrInternal can be used as a sentinel error for unexpected errors on the // server. ErrInternal = errors.New("Internal server error") )
var DBClient *kivik.Client
DBClient is the kivik client to use to make requests to CouchDB.
var DatabaseNamespace = "registry"
DatabaseNamespace is a prefix used for naming the CouchDB databases.
var SessionSecret []byte
SessionSecret is the secret used to check the tokens.
Functions ¶
func NewFileNotFoundError ¶
NewFileNotFoundError returns an Error that wraps the given error, with a Not found code.
func NewInternalError ¶
NewInternalError returns an Error that wraps the given error, with an Internal server error code.
func NewTooLargeError ¶
NewTooLargeError returns an Error that wraps the given error, with a Request Entity Too Large code.
func SprintfJSON ¶
func SprintfJSON(format string, a ...interface{}) json.RawMessage
SprintfJSON can be used to generate valid JSON with a sprintf-like format.
func VirtualDBName ¶
VirtualDBName returns the name of the database used for overwrites.
func VirtualVersionsDBName ¶
VirtualDBName returns the name of the database used for overwritten versions.
Types ¶
type Asset ¶
type Asset struct { ID string `json:"_id,omitempty"` Rev string `json:"_rev,omitempty"` Name string `json:"name"` Shasum string `json:"shasum"` AppSlug string `json:"appslug,omitempty"` ContentType string `json:"content_type"` UsedBy []string `json:"used_by"` }
Asset is a file persisted in the storage, with metadata in CouchDB. It can be used for icons, screenshorts, etc.
type AssetStore ¶
type AssetStore interface { // Prepare makes sure that CouchDB and swift spaces are ready to save // assets. Prepare() error // Add can be used to add an asset to the store. Add(asset *Asset, content io.Reader, source string) error // Get returns the asset content and the headers. Get(shasum string) (*bytes.Buffer, map[string]string, error) // Remove can be used to remove an asset from the store. Remove(shasum string, source string) error // GetDB returns the kivik.DB objects for low-level operations. GetDB() *kivik.DB }
AssetStore is an interface for a store to persist assets. Their content goes to the storage, their metadata to CouchDB, and the store maintains the consistency between both. The store also deduplicates assets with the same content.
var GlobalAssetStore AssetStore
GlobalAssetStore is used for persisting assets like icons and screenshots.
type Cache ¶
type Cache interface { // Status check if the cache is up, and returns an error if it is not. Status() error // Add adds a value to the cache. Add(Key, Value) // Get looks up a key's value from the cache. Get(Key) (Value, bool) // MGet looks up several keys at once from the cache. MGet([]Key) []interface{} // Remove removes the provided key from the cache. Remove(Key) }
Cache is an interface for a key-value caching service.
var LatestVersionsCache Cache
LatestVersionsCache is used for caching the latest version of an app.
var ListVersionsCache Cache
ListVersionsCache is used for caching the list of apps in a space.
type CleanParameters ¶
type CleanParameters struct { // NbMajor specifies how many major versions should be kept for app // cleaning tasks. NbMajor int // NbMinor specifies for each major version how many minor versions should // be kept for app cleaning tasks. NbMinor int // NbMonths specifies how many months to look up for app versions cleaning // tasks. NbMonths int }
CleanParameters regroups the parameters for cleaning the old versions.
type ConfigParameters ¶
type ConfigParameters struct { // CleanEnabled specifies if the app cleaning task is enabled or not. CleanEnabled bool // CleanParameters is the parameters list for the cleaning task. CleanParameters CleanParameters // VirtualSpaces is the list of virtual spaces: name -> virtual space. VirtualSpaces map[string]VirtualSpace // DomainSpaces links a domain host to a space (for universal links). DomainSpaces map[string]string // TrustedDomains is used by the universal link to allow redirections on // trusted domains. TrustedDomains map[string][]string // TrustedProtocols is used by the universal link to allow redirections on // trusted protocols (like cozy://). TrustedProtocols map[string][]string }
ConfigParameters is a list of parameters that can be configured.
var Config ConfigParameters
Config is the parameters that have been read from the config file, environment or flags.
type Error ¶
type Error struct { // Code is the HTTP status code to return to the client Code int // Wrapped should be a sentinel error that can be checked with errors.Is() Wrapped error // Cause can be used to give more details in logs Cause error }
Error is a struct that allow us to have information about errors.
type Key ¶
type Key string
Key is a type used for the keys in the cache.
type Prefix ¶
type Prefix string
Prefix is a way to regroup apps. It can be related to a space, but there is also a prefix for the global assets. And it is __default__, not the empty string for the default space.
const DefaultSpacePrefix Prefix = "__default__"
DefaultSpacePrefix is the prefix used for the default space.
type VirtualSpace ¶
type VirtualSpace struct { // Name of the virtual space Name string // Source is the name of a space Source string // Filter can be select (whitelist) or reject (blacklist) Filter string // Slugs is a list of webapp/connector slugs to filter Slugs []string }
VirtualSpace is a view on another space, with a filter to restrict the list of available applications.
func (VirtualSpace) AcceptApp ¶
func (v VirtualSpace) AcceptApp(slug string) bool
AcceptApp returns if the configuration says that the app can be seen in this virtual space.
func (VirtualSpace) Init ¶
func (v VirtualSpace) Init() error
func (VirtualSpace) OverrideDb ¶
func (v VirtualSpace) OverrideDb() *kivik.DB
func (VirtualSpace) VersionDB ¶
func (v VirtualSpace) VersionDB() *kivik.DB
type VirtualStorage ¶
type VirtualStorage interface { // Status check if the storage is up, and returns an error if it is not. Status() error // EnsureExists makes sure that the Swift container or local directory // exists. EnsureExists(prefix Prefix) error // EnsureEmpty makes sure that the Swift container or local directory // exists and does not contain any files. EnsureEmpty(prefix Prefix) error // EnsureDeleted makes sure that the Swift container or local directory // does no longer exist. EnsureDeleted(prefix Prefix) error // Create adds a file to the given container/directory. Create(prefix Prefix, name, contentType string, content io.Reader) error // Get fetches a file from the given container/directory. Get(prefix Prefix, name string) (*bytes.Buffer, map[string]string, error) // Remove deletes a file from the given container/directory. Remove(prefix Prefix, name string) error // Walk is a function to iterate on all object names of a given // container/directory. Walk(prefix Prefix, fn WalkFn) error // FindByPrefix returns a list of object names that starts with the given // string. FindByPrefix(prefix Prefix, namePrefix string) ([]string, error) }
VirtualStorage is an interface with the operations that can be done on the storage.
var Storage VirtualStorage
Storage is the global variable that can be used to perform operations on files.