You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2022/07/30 03:03:27 UTC
[servicecomb-kie] branch master updated: [feat]decouple the init func of DB client and DAO (#250)
This is an automated email from the ASF dual-hosted git repository.
tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-kie.git
The following commit(s) were added to refs/heads/master by this push:
new a03aaf3 [feat]decouple the init func of DB client and DAO (#250)
a03aaf3 is described below
commit a03aaf31cbdd206850eacf33aeda2cc427fd6066
Author: little-cui <su...@qq.com>
AuthorDate: Sat Jul 30 11:03:22 2022 +0800
[feat]decouple the init func of DB client and DAO (#250)
---
examples/dev/kie-conf.yaml | 8 +--
server/config/struct.go | 16 +++--
server/{datasource => config}/tlsutil/tlsutil.go | 4 +-
.../{datasource => config}/tlsutil/tlsutil_test.go | 10 ++--
server/datasource/dao.go | 36 ++----------
server/datasource/etcd/init.go | 28 +--------
server/datasource/mongo/init.go | 31 +---------
server/datasource/options.go | 11 +---
server/{datasource/etcd/init.go => db/db.go} | 68 ++++++++++------------
server/server.go | 6 +-
test/init.go | 8 ++-
11 files changed, 73 insertions(+), 153 deletions(-)
diff --git a/examples/dev/kie-conf.yaml b/examples/dev/kie-conf.yaml
index 2f22955..4b40ec0 100644
--- a/examples/dev/kie-conf.yaml
+++ b/examples/dev/kie-conf.yaml
@@ -7,9 +7,6 @@ db:
# kind=embedded_etcd, then is the embedded etcd server's advertise-peer-urls, e.g. default=http://127.0.0.1:2380
#uri: mongodb://kie:123@127.0.0.1:27017/kie
uri: http://127.0.0.1:2379
-sync:
- # turn on the synchronization switch related operations will be written to the task in the db
- enabled: false
# poolSize: 10
# timeout: 5m
# sslEnabled: false
@@ -19,4 +16,7 @@ sync:
# certPwdFile: ./ssl/cert_pwd
#rbac:
# enabled: false
-# rsaPublicKeyFile: ./examples/dev/public.key
\ No newline at end of file
+# rsaPublicKeyFile: ./examples/dev/public.key
+sync:
+ # turn on the synchronization switch related operations will be written to the task in the db
+ enabled: true
\ No newline at end of file
diff --git a/server/config/struct.go b/server/config/struct.go
index d39446e..6cc4e74 100644
--- a/server/config/struct.go
+++ b/server/config/struct.go
@@ -30,20 +30,24 @@ type Config struct {
AdvertiseAddr string
}
-//DB is yaml file struct to set persistent config
-type DB struct {
- URI string `yaml:"uri"`
- Kind string `yaml:"kind"`
- PoolSize int `yaml:"poolSize"`
+type TLS struct {
SSLEnabled bool `yaml:"sslEnabled"`
RootCA string `yaml:"rootCAFile"`
CertFile string `yaml:"certFile"`
KeyFile string `yaml:"keyFile"`
CertPwdFile string `yaml:"certPwdFile"`
- Timeout string `yaml:"timeout"`
VerifyPeer bool `yaml:"verifyPeer"`
}
+//DB is yaml file struct to set persistent config
+type DB struct {
+ TLS `yaml:",inline" json:",inline"`
+ URI string `yaml:"uri" json:"uri,omitempty"`
+ Kind string `yaml:"kind" json:"kind,omitempty"`
+ PoolSize int `yaml:"poolSize" json:"pool_size,omitempty"`
+ Timeout string `yaml:"timeout" json:"timeout,omitempty"`
+}
+
//RBAC is rbac config
type RBAC struct {
Enabled bool `yaml:"enabled"`
diff --git a/server/datasource/tlsutil/tlsutil.go b/server/config/tlsutil/tlsutil.go
similarity index 94%
rename from server/datasource/tlsutil/tlsutil.go
rename to server/config/tlsutil/tlsutil.go
index d857dad..059336a 100644
--- a/server/datasource/tlsutil/tlsutil.go
+++ b/server/config/tlsutil/tlsutil.go
@@ -23,7 +23,7 @@ import (
"io/ioutil"
"github.com/apache/servicecomb-kie/pkg/cipherutil"
- "github.com/apache/servicecomb-kie/server/datasource"
+ "github.com/apache/servicecomb-kie/server/config"
"github.com/go-chassis/foundation/stringutil"
"github.com/go-chassis/foundation/tlsutil"
"github.com/go-chassis/openlog"
@@ -31,7 +31,7 @@ import (
var ErrRootCAMissing = errors.New("rootCAFile is empty in config file")
-func Config(c *datasource.Config) (*tls.Config, error) {
+func Config(c *config.TLS) (*tls.Config, error) {
var password string
if c.CertPwdFile != "" {
pwdBytes, err := ioutil.ReadFile(c.CertPwdFile)
diff --git a/server/datasource/tlsutil/tlsutil_test.go b/server/config/tlsutil/tlsutil_test.go
similarity index 88%
rename from server/datasource/tlsutil/tlsutil_test.go
rename to server/config/tlsutil/tlsutil_test.go
index 0b0cbe6..4d8c28d 100644
--- a/server/datasource/tlsutil/tlsutil_test.go
+++ b/server/config/tlsutil/tlsutil_test.go
@@ -22,8 +22,8 @@ import (
_ "github.com/go-chassis/go-chassis/v2/security/cipher/plugins/plain"
- "github.com/apache/servicecomb-kie/server/datasource"
- "github.com/apache/servicecomb-kie/server/datasource/tlsutil"
+ "github.com/apache/servicecomb-kie/server/config"
+ "github.com/apache/servicecomb-kie/server/config/tlsutil"
"github.com/go-chassis/go-archaius"
"github.com/go-chassis/go-chassis/v2/security/cipher"
"github.com/stretchr/testify/assert"
@@ -44,7 +44,7 @@ func init() {
func TestConfig(t *testing.T) {
t.Run("normal scene, should return ok", func(t *testing.T) {
- cfg, err := tlsutil.Config(&datasource.Config{
+ cfg, err := tlsutil.Config(&config.TLS{
RootCA: sslRoot + "trust.cer",
CertFile: sslRoot + "server.cer",
KeyFile: sslRoot + "server_key.pem",
@@ -55,12 +55,12 @@ func TestConfig(t *testing.T) {
assert.NotNil(t, cfg)
})
t.Run("without ca file, should return false", func(t *testing.T) {
- cfg, err := tlsutil.Config(&datasource.Config{})
+ cfg, err := tlsutil.Config(&config.TLS{})
assert.ErrorIs(t, tlsutil.ErrRootCAMissing, err)
assert.Nil(t, cfg)
})
t.Run("set not exist pwd file, should return false", func(t *testing.T) {
- cfg, err := tlsutil.Config(&datasource.Config{
+ cfg, err := tlsutil.Config(&config.TLS{
RootCA: sslRoot + "trust.cer",
CertFile: sslRoot + "server.cer",
KeyFile: sslRoot + "server_key.pem",
diff --git a/server/datasource/dao.go b/server/datasource/dao.go
index 667e64f..ffd5510 100644
--- a/server/datasource/dao.go
+++ b/server/datasource/dao.go
@@ -22,12 +22,10 @@ import (
"context"
"errors"
"fmt"
- "time"
"github.com/go-chassis/openlog"
"github.com/apache/servicecomb-kie/pkg/model"
- "github.com/apache/servicecomb-kie/server/config"
)
var (
@@ -116,41 +114,17 @@ type ViewDao interface {
GetContent(ctx context.Context, id, domain, project string, options ...FindOption) ([]*model.KVResponse, error)
}
-const DefaultTimeout = 60 * time.Second
-
-func Init(c config.DB) error {
+func Init(kind string) error {
var err error
- if c.Kind == "" {
- c.Kind = "mongo"
- }
- f, ok := plugins[c.Kind]
+ f, ok := plugins[kind]
if !ok {
- return fmt.Errorf("do not support %s", c.Kind)
- }
- var timeout time.Duration
- if c.Timeout != "" {
- timeout, err = time.ParseDuration(c.Timeout)
- if err != nil {
- return errors.New("timeout setting invalid:" + c.Timeout)
- }
- }
- if timeout == 0 {
- timeout = DefaultTimeout
- }
- dbc := &Config{
- URI: c.URI,
- PoolSize: c.PoolSize,
- SSLEnabled: c.SSLEnabled,
- RootCA: c.RootCA,
- CertFile: c.CertFile,
- CertPwdFile: c.CertPwdFile,
- KeyFile: c.KeyFile,
- Timeout: timeout,
+ return fmt.Errorf("do not support '%s'", kind)
}
+ dbc := &Config{}
if b, err = f(dbc); err != nil {
return err
}
- openlog.Info(fmt.Sprintf("use %s as storage", c.Kind))
+ openlog.Info(fmt.Sprintf("use %s as storage", kind))
return nil
}
diff --git a/server/datasource/etcd/init.go b/server/datasource/etcd/init.go
index 8f0524a..6be7c12 100644
--- a/server/datasource/etcd/init.go
+++ b/server/datasource/etcd/init.go
@@ -18,44 +18,18 @@
package etcd
import (
- "crypto/tls"
- "fmt"
-
- "github.com/go-chassis/cari/db"
- dconfig "github.com/go-chassis/cari/db/config"
- "github.com/go-chassis/openlog"
-
- "github.com/apache/servicecomb-kie/server/config"
"github.com/apache/servicecomb-kie/server/datasource"
"github.com/apache/servicecomb-kie/server/datasource/etcd/counter"
"github.com/apache/servicecomb-kie/server/datasource/etcd/history"
"github.com/apache/servicecomb-kie/server/datasource/etcd/kv"
"github.com/apache/servicecomb-kie/server/datasource/etcd/track"
- "github.com/apache/servicecomb-kie/server/datasource/tlsutil"
)
type Broker struct {
}
func NewFrom(c *datasource.Config) (datasource.Broker, error) {
- kind := config.GetDB().Kind
- openlog.Info(fmt.Sprintf("use %s as storage", kind))
- var tlsConfig *tls.Config
- if c.SSLEnabled {
- var err error
- tlsConfig, err = tlsutil.Config(c)
- if err != nil {
- return nil, err
- }
- }
- return &Broker{}, db.Init(&dconfig.Config{
- Kind: kind,
- URI: c.URI,
- PoolSize: c.PoolSize,
- SSLEnabled: c.SSLEnabled,
- TLSConfig: tlsConfig,
- Timeout: c.Timeout,
- })
+ return &Broker{}, nil
}
func (*Broker) GetRevisionDao() datasource.RevisionDao {
return &counter.Dao{}
diff --git a/server/datasource/mongo/init.go b/server/datasource/mongo/init.go
index a104450..ab555e9 100644
--- a/server/datasource/mongo/init.go
+++ b/server/datasource/mongo/init.go
@@ -19,58 +19,31 @@ package mongo
import (
"context"
- "crypto/tls"
- "fmt"
- "github.com/go-chassis/cari/db"
- dconfig "github.com/go-chassis/cari/db/config"
dmongo "github.com/go-chassis/cari/db/mongo"
- "github.com/go-chassis/openlog"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
- "github.com/apache/servicecomb-kie/server/config"
"github.com/apache/servicecomb-kie/server/datasource"
"github.com/apache/servicecomb-kie/server/datasource/mongo/counter"
"github.com/apache/servicecomb-kie/server/datasource/mongo/history"
"github.com/apache/servicecomb-kie/server/datasource/mongo/kv"
"github.com/apache/servicecomb-kie/server/datasource/mongo/model"
"github.com/apache/servicecomb-kie/server/datasource/mongo/track"
- "github.com/apache/servicecomb-kie/server/datasource/tlsutil"
)
type Broker struct {
}
func NewFrom(c *datasource.Config) (datasource.Broker, error) {
- kind := config.GetDB().Kind
- openlog.Info(fmt.Sprintf("use %s as storage", kind))
- var tlsConfig *tls.Config
- if c.SSLEnabled {
- var err error
- tlsConfig, err = tlsutil.Config(c)
- if err != nil {
- return nil, err
- }
- }
broker := Broker{}
- err := db.Init(&dconfig.Config{
- Kind: kind,
- URI: c.URI,
- PoolSize: c.PoolSize,
- SSLEnabled: c.SSLEnabled,
- TLSConfig: tlsConfig,
- Timeout: c.Timeout,
- })
+ err := ensureDB()
if err != nil {
return nil, err
}
- if err = ensureDB(); err != nil {
- return nil, err
- }
- return &broker, err
+ return &broker, nil
}
func (*Broker) GetRevisionDao() datasource.RevisionDao {
return &counter.Dao{}
diff --git a/server/datasource/options.go b/server/datasource/options.go
index 2d6fee1..5d80af0 100644
--- a/server/datasource/options.go
+++ b/server/datasource/options.go
@@ -21,16 +21,9 @@ import (
"time"
)
+const DefaultTimeout = 60 * time.Second
+
type Config struct {
- URI string `yaml:"uri"`
- PoolSize int `yaml:"poolSize"`
- SSLEnabled bool `yaml:"sslEnabled"`
- VerifyPeer bool `yaml:"verifyPeer"`
- RootCA string `yaml:"rootCAFile"`
- CertFile string `yaml:"certFile"`
- KeyFile string `yaml:"keyFile"`
- CertPwdFile string `yaml:"certPwdFile"`
- Timeout time.Duration `yaml:"timeout"`
}
//NewDefaultFindOpts return default options
diff --git a/server/datasource/etcd/init.go b/server/db/db.go
similarity index 51%
copy from server/datasource/etcd/init.go
copy to server/db/db.go
index 8f0524a..5bacd37 100644
--- a/server/datasource/etcd/init.go
+++ b/server/db/db.go
@@ -15,62 +15,56 @@
* limitations under the License.
*/
-package etcd
+package db
import (
"crypto/tls"
- "fmt"
+ "errors"
+ "time"
+ "github.com/apache/servicecomb-kie/server/config"
+ "github.com/apache/servicecomb-kie/server/config/tlsutil"
"github.com/go-chassis/cari/db"
dconfig "github.com/go-chassis/cari/db/config"
"github.com/go-chassis/openlog"
-
- "github.com/apache/servicecomb-kie/server/config"
- "github.com/apache/servicecomb-kie/server/datasource"
- "github.com/apache/servicecomb-kie/server/datasource/etcd/counter"
- "github.com/apache/servicecomb-kie/server/datasource/etcd/history"
- "github.com/apache/servicecomb-kie/server/datasource/etcd/kv"
- "github.com/apache/servicecomb-kie/server/datasource/etcd/track"
- "github.com/apache/servicecomb-kie/server/datasource/tlsutil"
)
-type Broker struct {
-}
+const (
+ DefaultTimeout = 60 * time.Second
+ DefaultKind = "mongo"
+)
-func NewFrom(c *datasource.Config) (datasource.Broker, error) {
- kind := config.GetDB().Kind
- openlog.Info(fmt.Sprintf("use %s as storage", kind))
+func Init(c config.DB) error {
+ var err error
+ if c.Kind == "" {
+ c.Kind = DefaultKind
+ }
+ var timeout time.Duration
+ if c.Timeout != "" {
+ timeout, err = time.ParseDuration(c.Timeout)
+ if err != nil {
+ openlog.Fatal(err.Error())
+ return errors.New("timeout setting invalid:" + c.Timeout)
+ }
+ }
+ if timeout == 0 {
+ timeout = DefaultTimeout
+ }
var tlsConfig *tls.Config
if c.SSLEnabled {
var err error
- tlsConfig, err = tlsutil.Config(c)
+ tlsConfig, err = tlsutil.Config(&c.TLS)
if err != nil {
- return nil, err
+ openlog.Fatal(err.Error())
+ return errors.New("tls setting invalid:" + err.Error())
}
}
- return &Broker{}, db.Init(&dconfig.Config{
- Kind: kind,
+ return db.Init(&dconfig.Config{
+ Kind: c.Kind,
URI: c.URI,
PoolSize: c.PoolSize,
SSLEnabled: c.SSLEnabled,
TLSConfig: tlsConfig,
- Timeout: c.Timeout,
+ Timeout: timeout,
})
}
-func (*Broker) GetRevisionDao() datasource.RevisionDao {
- return &counter.Dao{}
-}
-func (*Broker) GetKVDao() datasource.KVDao {
- return &kv.Dao{}
-}
-func (*Broker) GetHistoryDao() datasource.HistoryDao {
- return &history.Dao{}
-}
-func (*Broker) GetTrackDao() datasource.TrackDao {
- return &track.Dao{}
-}
-
-func init() {
- datasource.RegisterPlugin("etcd", NewFrom)
- datasource.RegisterPlugin("embedded_etcd", NewFrom)
-}
diff --git a/server/server.go b/server/server.go
index c18167b..4214262 100644
--- a/server/server.go
+++ b/server/server.go
@@ -21,6 +21,7 @@ import (
"github.com/apache/servicecomb-kie/pkg/validator"
"github.com/apache/servicecomb-kie/server/config"
"github.com/apache/servicecomb-kie/server/datasource"
+ "github.com/apache/servicecomb-kie/server/db"
"github.com/apache/servicecomb-kie/server/pubsub"
"github.com/apache/servicecomb-kie/server/rbac"
v1 "github.com/apache/servicecomb-kie/server/resource/v1"
@@ -39,7 +40,10 @@ func Run() {
if err := config.Init(); err != nil {
openlog.Fatal(err.Error())
}
- if err := datasource.Init(config.GetDB()); err != nil {
+ if err := db.Init(config.GetDB()); err != nil {
+ openlog.Fatal(err.Error())
+ }
+ if err := datasource.Init(config.GetDB().Kind); err != nil {
openlog.Fatal(err.Error())
}
if err := validator.Init(); err != nil {
diff --git a/test/init.go b/test/init.go
index 435f0d5..40b2e03 100644
--- a/test/init.go
+++ b/test/init.go
@@ -21,6 +21,7 @@ import (
"fmt"
"math/rand"
+ "github.com/apache/servicecomb-kie/server/db"
_ "github.com/go-chassis/cari/db/bootstrap"
_ "github.com/apache/servicecomb-kie/server/datasource/etcd"
@@ -69,8 +70,7 @@ func init() {
if err != nil {
panic(err)
}
- config.Configurations.DB.Kind = kind
- err = datasource.Init(config.DB{
+ err = db.Init(config.DB{
URI: uri,
Timeout: "10s",
Kind: kind,
@@ -78,6 +78,10 @@ func init() {
if err != nil {
panic(err)
}
+ err = datasource.Init(kind)
+ if err != nil {
+ panic(err)
+ }
err = edatasource.Init(kind)
if err != nil {
panic(err)