Skip to content

Commit

Permalink
Added support for custom data providers
Browse files Browse the repository at this point in the history
  • Loading branch information
runabol committed Sep 7, 2023
1 parent 1314684 commit 0d4dcb1
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
14 changes: 11 additions & 3 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ const (
type Mode string

var (
quit = make(chan os.Signal, 1)
terminate = make(chan any, 1)
onStarted = defaultOnStartedHander
quit = make(chan os.Signal, 1)
terminate = make(chan any, 1)
onStarted = defaultOnStartedHander
dsProviders = map[string]datastore.Provider{}
)

func Start(mode Mode) error {
Expand Down Expand Up @@ -165,6 +166,9 @@ func runStandalone() error {
func createDatastore() (datastore.Datastore, error) {
dstype := conf.StringDefault("datastore.type", datastore.DATASTORE_INMEMORY)
var ds datastore.Datastore
if provider, ok := dsProviders[dstype]; ok {
return provider()
}
switch dstype {
case datastore.DATASTORE_INMEMORY:
ds = datastore.NewInMemoryDatastore()
Expand Down Expand Up @@ -299,3 +303,7 @@ func awaitTerm() {
case <-terminate:
}
}

func RegisterDatastoreProvider(dsType string, provider datastore.Provider) {
dsProviders[dsType] = provider
}
26 changes: 26 additions & 0 deletions bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package bootstrap_test

import (
"os"
"testing"

"github.com/runabol/tork/bootstrap"
"github.com/runabol/tork/conf"
"github.com/runabol/tork/datastore"
"github.com/stretchr/testify/assert"
)

Expand All @@ -24,3 +26,27 @@ func TestLoadConfig(t *testing.T) {

assert.True(t, started)
}

func TestRegisterDatastoreProvider(t *testing.T) {
assert.NoError(t, os.Setenv("TORK_DATASTORE_TYPE", "inmem2"))
defer func() {
assert.NoError(t, os.Unsetenv("TORK_DATASTORE_TYPE"))
}()
err := conf.LoadConfig()
assert.NoError(t, err)
calledProvider := false
bootstrap.RegisterDatastoreProvider("inmem2", func() (datastore.Datastore, error) {
calledProvider = true
return datastore.NewInMemoryDatastore(), nil
})
started := false
bootstrap.OnStarted(func() error {
started = true
bootstrap.Terminate()
return nil
})
err = bootstrap.Start(bootstrap.ModeStandalone)
assert.NoError(t, err)
assert.True(t, started)
assert.True(t, calledProvider)
}
9 changes: 9 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"github.com/pkg/errors"
Expand All @@ -28,6 +29,7 @@ func LoadConfig(paths ...string) error {
if len(paths) == 0 {
paths = defaultConfigPaths
}
// load configs from file paths
for _, f := range paths {
err := konf.Load(file.Provider(f), toml.Parser())
if errors.Is(err, os.ErrNotExist) {
Expand All @@ -39,6 +41,13 @@ func LoadConfig(paths ...string) error {
logger.Info().Msgf("Config loaded from %s", f)
return nil
}
// load configs from env vars
if err := konf.Load(env.Provider("TORK_", ".", func(s string) string {
return strings.Replace(strings.ToLower(
strings.TrimPrefix(s, "TORK_")), "_", ".", -1)
}), nil); err != nil {
return errors.Wrapf(err, "error loading config from env")
}
errMsg := fmt.Sprintf("could not find config file in any of the following paths: %s", strings.Join(paths, ","))
if userSpecified {
return errors.Errorf(errMsg)
Expand Down
11 changes: 11 additions & 0 deletions conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,14 @@ func TestLoadConfigCustomPath(t *testing.T) {
err = conf.LoadConfig("myconfig.toml")
assert.Error(t, err)
}

func TestLoadConfigEnv(t *testing.T) {
assert.NoError(t, os.Setenv("TORK_HELLO", "world"))
defer func() {
assert.NoError(t, os.Unsetenv("TORK_HELLO"))
}()
err := conf.LoadConfig()
assert.NoError(t, err)

assert.Equal(t, "world", conf.String("hello"))
}
3 changes: 3 additions & 0 deletions datastore/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package datastore

type Provider func() (Datastore, error)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/google/uuid v1.3.0
github.com/jmoiron/sqlx v1.3.5
github.com/knadh/koanf/parsers/toml v0.1.0
github.com/knadh/koanf/providers/env v0.1.0
github.com/knadh/koanf/providers/file v0.1.0
github.com/knadh/koanf/v2 v2.0.1
github.com/labstack/echo/v4 v4.11.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NI
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
github.com/knadh/koanf/parsers/toml v0.1.0 h1:S2hLqS4TgWZYj4/7mI5m1CQQcWurxUz6ODgOub/6LCI=
github.com/knadh/koanf/parsers/toml v0.1.0/go.mod h1:yUprhq6eo3GbyVXFFMdbfZSo928ksS+uo0FFqNMnO18=
github.com/knadh/koanf/providers/env v0.1.0 h1:LqKteXqfOWyx5Ab9VfGHmjY9BvRXi+clwyZozgVRiKg=
github.com/knadh/koanf/providers/env v0.1.0/go.mod h1:RE8K9GbACJkeEnkl8L/Qcj8p4ZyPXZIQ191HJi44ZaQ=
github.com/knadh/koanf/providers/file v0.1.0 h1:fs6U7nrV58d3CFAFh8VTde8TM262ObYf3ODrc//Lp+c=
github.com/knadh/koanf/providers/file v0.1.0/go.mod h1:rjJ/nHQl64iYCtAW2QQnF0eSmDEX/YZ/eNFj5yR6BvA=
github.com/knadh/koanf/v2 v2.0.1 h1:1dYGITt1I23x8cfx8ZnldtezdyaZtfAuRtIFOiRzK7g=
Expand Down

0 comments on commit 0d4dcb1

Please sign in to comment.