diff --git a/cli/daemon/daemon.go b/cli/daemon/daemon.go index 71c64ec..a0f22de 100644 --- a/cli/daemon/daemon.go +++ b/cli/daemon/daemon.go @@ -19,7 +19,7 @@ func InitCmd(cmd *cobra.Command) error { } daemonCmd.Flags().Bool("compression", true, "Compress measured values") - daemonCmd.Flags().Uint("cached-values", 500, "Number of cached values before saveing into the storage endpoint") + daemonCmd.Flags().Uint("cached-values", 500, "Number of cached values before saveing into the backend") daemonCmd.Flags().Float64("round", 0.5, "Round values. The value 0 deactivates the function") cmd.AddCommand(daemonCmd) diff --git a/cli/root.go b/cli/root.go index fecf145..44a7341 100644 --- a/cli/root.go +++ b/cli/root.go @@ -83,7 +83,7 @@ func preRunError(cmd *cobra.Command, args []string) error { Name: hostname, CreationDate: postgresTimeStamp, }, - StorageEndpoint: "sqlite3:///var/log/flucky/sqlite.db?cache=shared&mode=memory&FKSupport=True", + DSN: "sqlite3:///var/log/flucky/sqlite.db?cache=shared&mode=memory&FKSupport=True", } err = config.Write(&cnf, configFile) diff --git a/cli/sensor/sensor.go b/cli/sensor/sensor.go index 18a753a..3cc0014 100644 --- a/cli/sensor/sensor.go +++ b/cli/sensor/sensor.go @@ -164,7 +164,7 @@ func addSensor(cmd *cobra.Command, args []string) error { return err } - storageEndpoint, err := url.Parse(cnf.StorageEndpoint) + dsnURL, err := url.Parse(cnf.DSN) if err != nil { return err } @@ -176,7 +176,7 @@ func addSensor(cmd *cobra.Command, args []string) error { flogger := logger.NewDefaultLogger(logger.LogLevelDebug) - repo, err := repository.New(storageEndpoint, flogger) + repo, err := repository.New(dsnURL, flogger) if err != nil { return err } @@ -208,7 +208,7 @@ func disableSensor(cmd *cobra.Command, args []string) error { return err } - storageEndpoint, err := url.Parse(cnf.StorageEndpoint) + dsnURL, err := url.Parse(cnf.DSN) if err != nil { return err } @@ -220,7 +220,7 @@ func disableSensor(cmd *cobra.Command, args []string) error { flogger := logger.NewDefaultLogger(logger.LogLevelDebug) - repo, err := repository.New(storageEndpoint, flogger) + repo, err := repository.New(dsnURL, flogger) if err != nil { return err } @@ -240,7 +240,7 @@ func enableSensor(cmd *cobra.Command, args []string) error { return err } - storageEndpoint, err := url.Parse(cnf.StorageEndpoint) + dsnURL, err := url.Parse(cnf.DSN) if err != nil { return err } @@ -252,7 +252,7 @@ func enableSensor(cmd *cobra.Command, args []string) error { flogger := logger.NewDefaultLogger(logger.LogLevelDebug) - repo, err := repository.New(storageEndpoint, flogger) + repo, err := repository.New(dsnURL, flogger) if err != nil { return err } @@ -272,7 +272,7 @@ func listSensors(cmd *cobra.Command, args []string) error { return err } - storageEndpoint, err := url.Parse(cnf.StorageEndpoint) + dsnURL, err := url.Parse(cnf.DSN) if err != nil { return err } @@ -284,7 +284,7 @@ func listSensors(cmd *cobra.Command, args []string) error { flogger := logger.NewDefaultLogger(logger.LogLevelDebug) - repo, err := repository.New(storageEndpoint, flogger) + repo, err := repository.New(dsnURL, flogger) if err != nil { return err } @@ -315,7 +315,7 @@ func removeSensor(cmd *cobra.Command, args []string) error { return err } - storageEndpoint, err := url.Parse(cnf.StorageEndpoint) + dsnURL, err := url.Parse(cnf.DSN) if err != nil { return err } @@ -327,7 +327,7 @@ func removeSensor(cmd *cobra.Command, args []string) error { flogger := logger.NewDefaultLogger(logger.LogLevelDebug) - repo, err := repository.New(storageEndpoint, flogger) + repo, err := repository.New(dsnURL, flogger) if err != nil { return err } @@ -347,7 +347,7 @@ func renameSensor(cmd *cobra.Command, args []string) error { return err } - storageEndpoint, err := url.Parse(cnf.StorageEndpoint) + dsnURL, err := url.Parse(cnf.DSN) if err != nil { return err } @@ -359,7 +359,7 @@ func renameSensor(cmd *cobra.Command, args []string) error { flogger := logger.NewDefaultLogger(logger.LogLevelDebug) - repo, err := repository.New(storageEndpoint, flogger) + repo, err := repository.New(dsnURL, flogger) if err != nil { return err } diff --git a/pkg/config/config.go b/pkg/config/config.go index 039fd7f..7685e72 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -20,9 +20,10 @@ var ( // Config represent the configuration type Config struct { - Device *types.Device `json:"device"` - Sensors []*types.Sensor `json:"sensors"` - StorageEndpoint string `json:"storage_endpoint"` + Device *types.Device `json:"device"` + Sensors []*types.Sensor `json:"sensors"` + DSN string `json:"dsn"` + DSNFallback string `json:"dsn_fallback"` } // AddSensor add a new sensor diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index cdbf22c..5f4990a 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -18,13 +18,13 @@ func Start(cnf *config.Config, flogger logger.Logger) error { measuredValueChannel := make(chan *types.MeasuredValue, 0) - // load storage endpoint - storageEndpointURL, err := url.Parse(cnf.StorageEndpoint) + // load data source name (dsn) + dsnURL, err := url.Parse(cnf.DSN) if err != nil { return err } - repo, err := repository.New(storageEndpointURL, flogger) + repo, err := repository.New(dsnURL, flogger) if err != nil { return err } diff --git a/pkg/repository/db/db.go b/pkg/repository/db/db.go index ff26dde..e6b28e5 100644 --- a/pkg/repository/db/db.go +++ b/pkg/repository/db/db.go @@ -13,7 +13,8 @@ import ( "github.com/volker-raschek/go-logger/pkg/logger" ) -// Database is a general interface for a storage endpoint +// Database is a general interface for a database backend like postgres, oracle +// or sqlite type Database interface { DeleteDevices(ctx context.Context, deviceIDs ...string) error DeleteSensors(ctx context.Context, sensorIDs ...string) error @@ -35,12 +36,12 @@ type Database interface { UpdateSensors(ctx context.Context, sensors ...*types.Sensor) error } -// New returns a new storage provider -func New(storageEndpoint *url.URL, flogger logger.Logger) (Database, error) { +// New returns a new database backend interface +func New(dsnURL *url.URL, flogger logger.Logger) (Database, error) { // Check of nil pointer for _, parameter := range []interface{}{ - storageEndpoint, + dsnURL, flogger, } { if parameter == nil { @@ -51,7 +52,7 @@ func New(storageEndpoint *url.URL, flogger logger.Logger) (Database, error) { // Load Queryfiles queries := make(map[string]string, 0) for _, asset := range AssetNames() { - if !strings.Contains(asset, storageEndpoint.Scheme) { + if !strings.Contains(asset, dsnURL.Scheme) { continue } @@ -70,7 +71,7 @@ func New(storageEndpoint *url.URL, flogger logger.Logger) (Database, error) { err error ) - switch storageEndpoint.Scheme { + switch dsnURL.Scheme { // case "postgres": // // postgres://[user]:[password]@[host]:[port]/[path]?[query] // newDBO, err := sql.Open(storageEndpoint.Scheme, storageEndpoint.String()) @@ -86,8 +87,8 @@ func New(storageEndpoint *url.URL, flogger logger.Logger) (Database, error) { case "sqlite3": // Create directory where the db file will be created if not exists. - if _, err := os.Stat(filepath.Dir(storageEndpoint.Path)); os.IsNotExist(err) { - err := os.MkdirAll(filepath.Dir(storageEndpoint.Path), 0755) + if _, err := os.Stat(filepath.Dir(dsnURL.Path)); os.IsNotExist(err) { + err := os.MkdirAll(filepath.Dir(dsnURL.Path), 0755) if err != nil { return nil, err } @@ -95,7 +96,7 @@ func New(storageEndpoint *url.URL, flogger logger.Logger) (Database, error) { // sqlite3:///[path]?[query] flucky dsn // file:///[path]?[query] sql-lib dsn - newDBO, err := sql.Open(storageEndpoint.Scheme, fmt.Sprintf("file://%v?%v", storageEndpoint.Path, storageEndpoint.RawQuery)) + newDBO, err := sql.Open(dsnURL.Scheme, fmt.Sprintf("file://%v?%v", dsnURL.Path, dsnURL.RawQuery)) if err != nil { return nil, err } @@ -106,7 +107,7 @@ func New(storageEndpoint *url.URL, flogger logger.Logger) (Database, error) { queries: queries, } default: - return nil, fmt.Errorf("Unsupported database scheme: %v", storageEndpoint.Scheme) + return nil, fmt.Errorf("Unsupported database scheme: %v", dsnURL.Scheme) } // Initialize database scheme if not exists diff --git a/pkg/repository/db/sqlite.go b/pkg/repository/db/sqlite.go index 5072fa3..196600e 100644 --- a/pkg/repository/db/sqlite.go +++ b/pkg/repository/db/sqlite.go @@ -10,10 +10,6 @@ import ( "github.com/volker-raschek/go-logger/pkg/logger" ) -var ( - sqliteAssetPath = "pkg/storage/sqlite" -) - // SQLite implementation type SQLite struct { dbo *sql.DB diff --git a/pkg/repository/repository.go b/pkg/repository/repository.go index 014056d..5005773 100644 --- a/pkg/repository/repository.go +++ b/pkg/repository/repository.go @@ -176,9 +176,9 @@ func (repo *Repository) UpdateSensors(sensors ...*types.Sensor) error { return repo.database.UpdateSensors(context.Background(), sensors...) } -// New returns a new repository based on the storageEndpoint -func New(storageEndpoint *url.URL, flogger logger.Logger) (*Repository, error) { - database, err := db.New(storageEndpoint, flogger) +// New returns a new repository based on the data source name (dsn) +func New(dsnURL *url.URL, flogger logger.Logger) (*Repository, error) { + database, err := db.New(dsnURL, flogger) if err != nil { return nil, err } diff --git a/pkg/repository/repository_test.go b/pkg/repository/repository_test.go index 550dddb..69c71d0 100644 --- a/pkg/repository/repository_test.go +++ b/pkg/repository/repository_test.go @@ -42,10 +42,10 @@ func TestPostgresBackend(t *testing.T) { require.NoError(err) // postgres://[user]:[password]@[host]:[port]/[path]?[query] - storageEndpoint, err := url.Parse(fmt.Sprintf("postgres://postgres:%v@localhost:%v", postgresDBPasswort, postgresHostPort)) + dsnURL, err := url.Parse(fmt.Sprintf("postgres://postgres:%v@localhost:%v", postgresDBPasswort, postgresHostPort)) require.NoError(err) - repo, err := repository.New(storageEndpoint, logger.NewDefaultLogger(logger.LogLevelDebug)) + repo, err := repository.New(dsnURL, logger.NewDefaultLogger(logger.LogLevelDebug)) require.NoError(err) testBackend(t, repo) @@ -58,10 +58,10 @@ func TestSQLiteBackend(t *testing.T) { require.NoError(err) defer os.RemoveAll(workspace) - storageEndpoint, err := url.Parse(fmt.Sprintf("sqlite3://%v/test.db", workspace)) + dsnURL, err := url.Parse(fmt.Sprintf("sqlite3://%v/test.db", workspace)) require.NoError(err) - repo, err := repository.New(storageEndpoint, logger.NewDefaultLogger(logger.LogLevelDebug)) + repo, err := repository.New(dsnURL, logger.NewDefaultLogger(logger.LogLevelDebug)) require.NoError(err) testBackend(t, repo)