mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Inverting deps on SQL, Log and MQ plugins to make them optional dependencies of extended servers, Removing some dead code that brought in unused dependencies Filtering out some non-linux transitive deps. (#1057)
* initial Db helper split - make SQL and datastore packages optional * abstracting log store * break out DB, MQ and log drivers as extensions * cleanup * fewer deps * fixing docker test * hmm dbness * updating db startup * Consolidate all your extensions into one convenient package * cleanup * clean up dep constraints
This commit is contained in:
@@ -2,35 +2,49 @@ package datastore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"fmt"
|
||||
"github.com/fnproject/fn/api/common"
|
||||
"github.com/fnproject/fn/api/datastore/internal/datastoreutil"
|
||||
"github.com/fnproject/fn/api/datastore/sql"
|
||||
"github.com/fnproject/fn/api/models"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// New creates a DataStore from the specified URL
|
||||
func New(ctx context.Context, dbURL string) (models.Datastore, error) {
|
||||
return newds(ctx, dbURL) // teehee
|
||||
}
|
||||
|
||||
func Wrap(ds models.Datastore) models.Datastore {
|
||||
return datastoreutil.MetricDS(datastoreutil.NewValidator(ds))
|
||||
}
|
||||
|
||||
func newds(ctx context.Context, dbURL string) (models.Datastore, error) {
|
||||
log := common.Logger(ctx)
|
||||
u, err := url.Parse(dbURL)
|
||||
if err != nil {
|
||||
log.WithError(err).WithFields(logrus.Fields{"url": dbURL}).Fatal("bad DB URL")
|
||||
}
|
||||
log.WithFields(logrus.Fields{"db": u.Scheme}).Debug("creating new datastore")
|
||||
switch u.Scheme {
|
||||
case "sqlite3", "postgres", "mysql":
|
||||
return sql.New(ctx, u)
|
||||
default:
|
||||
return nil, fmt.Errorf("db type not supported %v", u.Scheme)
|
||||
|
||||
for _, provider := range providers {
|
||||
if provider.Supports(u) {
|
||||
return provider.New(ctx, u)
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("no data store provider found for storage url %s", u)
|
||||
}
|
||||
|
||||
func Wrap(ds models.Datastore) models.Datastore {
|
||||
return datastoreutil.MetricDS(datastoreutil.NewValidator(ds))
|
||||
}
|
||||
|
||||
// Provider is a datastore provider
|
||||
type Provider interface {
|
||||
fmt.Stringer
|
||||
// Supports indicates if this provider can handle a given data store.
|
||||
Supports(url *url.URL) bool
|
||||
// New creates a new data store from the specified URL
|
||||
New(ctx context.Context, url *url.URL) (models.Datastore, error)
|
||||
}
|
||||
|
||||
var providers []Provider
|
||||
|
||||
// AddProvider globally registers a data store provider
|
||||
func AddProvider(provider Provider) {
|
||||
logrus.Infof("Adding DataStore provider %s", provider)
|
||||
providers = append(providers, provider)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user