1
0
Fork 0
mirror of https://github.com/Luzifer/cloudkeys-go.git synced 2024-11-14 08:52:44 +00:00
cloudkeys-go/storage.go
Knut Ahlers b4793cbab5
Reimplement Go server as API-Server
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-11-04 11:50:03 +01:00

44 lines
1 KiB
Go

package main
import (
"context"
"fmt"
"io"
"net/url"
)
var (
storageAdapters = map[string]storageAdapterInitializer{}
)
type storageAdapter interface {
Write(ctx context.Context, identifier string, data io.Reader) error
Read(ctx context.Context, identifier string) (io.Reader, error)
IsPresent(ctx context.Context, identifier string) bool
Backup(ctx context.Context, identifier string) error
}
type storageAdapterInitializer func(*url.URL) (storageAdapter, error)
func getStorageAdapter(cfg *config) (storageAdapter, error) {
storageURI, _ := cfg.ParsedStorage()
if sa, ok := storageAdapters[storageURI.Scheme]; ok {
s, err := sa(storageURI)
if err != nil {
return nil, err
}
return s, nil
}
return nil, fmt.Errorf("Did not find storage adapter for '%s'", storageURI.Scheme)
}
func registerStorage(scheme string, f storageAdapterInitializer) error {
if _, ok := storageAdapters[scheme]; ok {
return fmt.Errorf("Cannot register '%s', is already registered", scheme)
}
storageAdapters[scheme] = f
return nil
}