Skip to content

Commit

Permalink
✨ Update config (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone authored May 5, 2024
1 parent 0c1289a commit 88528f3
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ pkg/**/*.html
web/.yarn/
docs/.yarn/
coverage.txt
/locker/
/badger/
34 changes: 21 additions & 13 deletions conf/config-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,30 @@ database:
sslmode: disable

redis:
# redis type available: none, external
# redis type available: none, external. Following all of redis config just use reference here.
# none: means never use redis
# external: means use the specific redis instance
type: external
url: redis://:sigma@localhost:6379/0

badger:
# badger is used to implement lock and cache in a single-node mode.
enabled: true
path: ./badger/

cache:
# the cache type available is: redis, inmemory, database
type: redis
ttl: 72h
# please attention in multi
# the cache type available is: redis, inmemory, badger
# please attention in multi-node mode, you should use redis
type: badger
inmemory:
prefix: sigma-cache
size: 10240
redis:
database:
size: 10240
threshold: 0.2
prefix: sigma-cache
ttl: 72h
badger:
prefix: sigma-cache
ttl: 72h

workqueue:
# the workqueue type available: redis, kafka, database, inmemory
Expand All @@ -53,11 +60,12 @@ workqueue:
concurrency: 1024

locker:
# the locker type available: redis, database
type: database
database:
path: ./locker
redis: {}
# the locker type available: redis, badger
type: badger
badger:
prefix: sigma-locker
redis:
prefix: sigma-locker

namespace:
# push image to registry, if namespace not exist, it will be created automatically
Expand Down
32 changes: 21 additions & 11 deletions conf/config-full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,30 @@ database:
deploy: single

redis:
# redis type available: none, external
# redis type available: none, external. Following all of redis config just use reference here.
# none: means never use redis
# external: means use the specific redis instance
type: none
url: redis://:sigma@localhost:6379/0

badger:
# badger is used to implement lock and cache in a single-node mode.
enabled: true
path: /var/lib/sigma/badger/

cache:
# the cache type available is: redis, inmemory, database
type: redis
# the cache type available is: redis, inmemory, badger
# please attention in multi-node mode, you should use redis
type: badger
inmemory:
prefix: sigma-cache
size: 10240
ttl: 72h
redis:
database:
size: 10240
threshold: 0.2
prefix: sigma-cache
ttl: 72h
badger:
prefix: sigma-cache
ttl: 72h

workqueue:
# the workqueue type available: redis, kafka, database, inmemory
Expand All @@ -56,10 +64,12 @@ workqueue:
concurrency: 1024

locker:
# the locker type available: redis, database
type: database
database: {}
redis: {}
# the locker type available: redis, badger
type: badger
badger:
prefix: sigma-locker
redis:
prefix: sigma-locker

namespace:
# push image to registry, if namespace not exist, it will be created automatically
Expand Down
30 changes: 20 additions & 10 deletions docs/docs/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,30 @@ database:
sslmode: disable

redis:
# redis type available: none, external
# redis type available: none, external. Following all of redis config just use reference here.
# none: means never use redis
# external: means use the specific redis instance
type: none
url: redis://:sigma@localhost:6379/0

badger:
# badger is used to implement lock and cache in a single-node mode.
enabled: true
path: /var/lib/sigma/badger/

cache:
# the cache type available is: redis, inmemory, database
type: redis
# the cache type available is: redis, inmemory, badger
# please attention in multi-node mode, you should use redis
type: badger
inmemory:
prefix: sigma-cache
size: 10240
redis:
prefix: sigma-cache
ttl: 72h
badger:
prefix: sigma-cache
ttl: 72h
database:
size: 10240
threshold: 0.2

workqueue:
# the workqueue type available: redis, kafka, database, inmemory
Expand All @@ -56,10 +64,12 @@ workqueue:
concurrency: 1024

locker:
# the locker type available: redis, database
type: database
database: {}
redis: {}
# the locker type available: redis, badger
type: badger
badger:
prefix: sigma-locker
redis:
prefix: sigma-locker

namespace:
# push image to registry, if namespace not exist, it will be created automatically
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/cos/cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net/http"
"net/url"
"path"
"reflect"
"strings"

gonanoid "github.com/matoous/go-nanoid"
Expand All @@ -30,11 +29,12 @@ import (
"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/storage"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils"
)

func init() {
utils.PanicIf(storage.RegisterDriverFactory(path.Base(reflect.TypeOf(factory{}).PkgPath()), &factory{}))
utils.PanicIf(storage.RegisterDriverFactory(enums.StorageTypeCos, &factory{}))
}

type factory struct{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"os"
"path"
"path/filepath"
"reflect"

gonanoid "github.com/matoous/go-nanoid"
"github.com/rs/zerolog/log"

"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/storage"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils"
)

Expand All @@ -38,7 +38,7 @@ type fs struct {
}

func init() {
utils.PanicIf(storage.RegisterDriverFactory(path.Base(reflect.TypeOf(factory{}).PkgPath()), &factory{}))
utils.PanicIf(storage.RegisterDriverFactory(enums.StorageTypeFilesystem, &factory{}))
}

type factory struct{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/oss/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net/textproto"
"os"
"path"
"reflect"
"strconv"
"strings"

Expand All @@ -32,11 +31,12 @@ import (
"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/storage"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils"
)

func init() {
utils.PanicIf(storage.RegisterDriverFactory(path.Base(reflect.TypeOf(factory{}).PkgPath()), &factory{}))
utils.PanicIf(storage.RegisterDriverFactory(enums.StorageTypeOss, &factory{}))
}

type factory struct{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io"
"os"
"path"
"reflect"
"strings"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -35,6 +34,7 @@ import (
"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/storage"
"github.com/go-sigma/sigma/pkg/types/enums"
"github.com/go-sigma/sigma/pkg/utils"
"github.com/go-sigma/sigma/pkg/utils/ptr"
)
Expand All @@ -47,7 +47,7 @@ type awss3 struct {
}

func init() {
utils.PanicIf(storage.RegisterDriverFactory(path.Base(reflect.TypeOf(factory{}).PkgPath()), &factory{}))
utils.PanicIf(storage.RegisterDriverFactory(enums.StorageTypeS3, &factory{}))
}

type factory struct{}
Expand Down
12 changes: 5 additions & 7 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import (
"path"
"strings"

"github.com/spf13/viper"

"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/types/enums"
)

const (
Expand Down Expand Up @@ -80,11 +79,11 @@ type Factory interface {
New(config configs.Configuration) (StorageDriver, error)
}

var driverFactories = make(map[string]Factory)
var driverFactories = make(map[enums.StorageType]Factory)

// RegisterDriverFactory registers a storage factory driver by name.
// If RegisterDriverFactory is called twice with the same name or if driver is nil, it panics.
func RegisterDriverFactory(name string, factory Factory) error {
func RegisterDriverFactory(name enums.StorageType, factory Factory) error {
if _, ok := driverFactories[name]; ok {
return fmt.Errorf("driver %q already registered", name)
}
Expand Down Expand Up @@ -115,10 +114,9 @@ func (s *storageDriverFactory) New() StorageDriver {

// Initialize initializes the storage driver
func Initialize(config configs.Configuration) error {
typ := viper.GetString("storage.type")
factory, ok := driverFactories[typ]
factory, ok := driverFactories[config.Storage.Type]
if !ok {
return fmt.Errorf("driver %q not registered", typ)
return fmt.Errorf("driver %q not registered", config.Storage.Type)
}
var err error
Driver, err = factory.New(config)
Expand Down
30 changes: 16 additions & 14 deletions pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"fmt"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/types/enums"
)

type dummyFactory struct{}
Expand All @@ -37,35 +37,37 @@ func (dummyFactoryError) New(_ configs.Configuration) (StorageDriver, error) {
}

func TestRegisterDriverFactory(t *testing.T) {
driverFactories = make(map[string]Factory)
driverFactories = make(map[enums.StorageType]Factory)

err := RegisterDriverFactory("dummy", &dummyFactory{})
err := RegisterDriverFactory(enums.StorageTypeDummy, &dummyFactory{})
assert.NoError(t, err)

err = RegisterDriverFactory("dummy", &dummyFactory{})
err = RegisterDriverFactory(enums.StorageTypeDummy, &dummyFactory{})
assert.Error(t, err)
}

func TestInitialize(t *testing.T) {
driverFactories = make(map[string]Factory)
driverFactories = make(map[enums.StorageType]Factory)

err := RegisterDriverFactory("dummy", &dummyFactory{})
err := RegisterDriverFactory(enums.StorageTypeDummy, &dummyFactory{})
assert.NoError(t, err)

viper.SetDefault("storage.type", "dummy")
err = Initialize(configs.Configuration{})
err = Initialize(configs.Configuration{
Storage: configs.ConfigurationStorage{
Type: enums.StorageTypeDummy,
},
})
assert.NoError(t, err)

viper.SetDefault("storage.type", "fake")
err = Initialize(configs.Configuration{})
err = Initialize(configs.Configuration{
Storage: configs.ConfigurationStorage{
Type: "fake",
},
})
assert.Error(t, err)

err = RegisterDriverFactory("dummy-error", &dummyFactoryError{})
assert.NoError(t, err)

viper.SetDefault("storage.type", "dummy-error")
err = Initialize(configs.Configuration{})
assert.Error(t, err)
}

func TestSanitizePath(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/types/enums/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ type BuilderSource string
// s3,
// filesystem,
// cos,
// oss,
// dummy,
// )
type StorageType string

Expand Down
6 changes: 6 additions & 0 deletions pkg/types/enums/enums_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 88528f3

Please sign in to comment.