mirror of
https://github.com/tiennm99/keepalive.git
synced 2026-10-15 18:15:58 +00:00
feat(config): support multi-service yaml config
This commit is contained in:
1 parent
655db8dcbf
commit
f3a23ee91e
19 files changed
+709
-268
No files matched your search
+22
-5
@@ -17,16 +17,33 @@ type Adapter interface {
|
||||
Close(ctx context.Context) error
|
||||
}
|
||||
|
||||
type Factory func() (Adapter, error)
|
||||
type Config map[string]string
|
||||
|
||||
func (c Config) Required(name string) (string, error) {
|
||||
v, ok := c[name]
|
||||
if !ok || v == "" {
|
||||
return "", fmt.Errorf("config %s is required", name)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (c Config) Optional(name, def string) string {
|
||||
if v, ok := c[name]; ok && v != "" {
|
||||
return v
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
type Factory func(Config) (Adapter, error)
|
||||
|
||||
var Registry = map[string]Factory{}
|
||||
|
||||
func New(dbType string) (Adapter, error) {
|
||||
f, ok := Registry[dbType]
|
||||
func New(adapterType string, cfg Config) (Adapter, error) {
|
||||
f, ok := Registry[adapterType]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown adapter %q (known: %v)", dbType, Known())
|
||||
return nil, fmt.Errorf("unknown adapter %q (known: %v)", adapterType, Known())
|
||||
}
|
||||
return f()
|
||||
return f(cfg)
|
||||
}
|
||||
|
||||
func Known() []string {
|
||||
|
||||
Reference in new issue
Block a user