storj/private/revocation/common.go
Egon Elbre 7802ab714f pkg/,private/: merge with private package
Initially there were pkg and private packages, however for all practical
purposes there's no significant difference between them. It's clearer to
have a single private package - and when we do get a specific
abstraction that needs to be reused, we can move it to storj.io/common
or storj.io/private.

Change-Id: Ibc2036e67f312f5d63cb4a97f5a92e38ae413aa5
2021-04-23 16:37:28 +03:00

71 lines
1.7 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package revocation
import (
"context"
"storj.io/common/peertls/extensions"
"storj.io/common/peertls/tlsopts"
"storj.io/private/dbutil"
"storj.io/storj/storage/boltdb"
"storj.io/storj/storage/redis"
)
// OpenDBFromCfg is a convenience method to create a revocation DB
// directly from a config. If the revocation extension option is not set, it
// returns a nil db with no error.
func OpenDBFromCfg(ctx context.Context, cfg tlsopts.Config) (*DB, error) {
if !cfg.Extensions.Revocation {
return &DB{}, nil
}
return OpenDB(ctx, cfg.RevocationDBURL)
}
// OpenDB returns a new revocation database given the URL.
func OpenDB(ctx context.Context, dbURL string) (*DB, error) {
driver, source, _, err := dbutil.SplitConnStr(dbURL)
if err != nil {
return nil, extensions.ErrRevocationDB.Wrap(err)
}
var db *DB
switch driver {
case "bolt":
db, err = openDBBolt(ctx, source)
if err != nil {
return nil, extensions.ErrRevocationDB.Wrap(err)
}
case "redis":
db, err = openDBRedis(ctx, dbURL)
if err != nil {
return nil, extensions.ErrRevocationDB.Wrap(err)
}
default:
return nil, extensions.ErrRevocationDB.New("database scheme not supported: %s", driver)
}
return db, nil
}
// openDBBolt creates a bolt-backed DB.
func openDBBolt(ctx context.Context, path string) (*DB, error) {
client, err := boltdb.New(path, extensions.RevocationBucket)
if err != nil {
return nil, err
}
return &DB{
store: client,
}, nil
}
// openDBRedis creates a redis-backed DB.
func openDBRedis(ctx context.Context, address string) (*DB, error) {
client, err := redis.OpenClientFrom(ctx, address)
if err != nil {
return nil, err
}
return &DB{
store: client,
}, nil
}