From 2947c1ef52312883f28b60d01ed874c2a18a3d08 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Fri, 17 Dec 2021 20:58:37 +0800 Subject: [PATCH] store seems ok --- .travis.yml | 1 + common/config.go | 15 +++++----- common/db.go | 19 +++++++------ common/types_test.go | 4 +-- conf.sample.yml | 28 +++++++++---------- helper/compose.redis.yml | 9 ------ helper/{compose.dev.yml => compose.store.yml} | 25 +++++------------ test/main_test.go | 22 +++++++++------ 8 files changed, 54 insertions(+), 69 deletions(-) delete mode 100644 helper/compose.redis.yml rename helper/{compose.dev.yml => compose.store.yml} (54%) diff --git a/.travis.yml b/.travis.yml index a57c97b..36e574d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ branches: - alpha services: - mysql + - redis-server before_install: - go get -t -v ./... - go get github.com/mattn/goveralls diff --git a/common/config.go b/common/config.go index 115a702..5f11e6c 100644 --- a/common/config.go +++ b/common/config.go @@ -3,7 +3,6 @@ package common import ( "errors" "io/ioutil" - "os" "path/filepath" "github.com/yedf/dtm/dtmcli" @@ -23,7 +22,7 @@ type MicroService struct { } type Store struct { - Driver string `yaml:"Driver"` + Driver string `yaml:"Driver" default:"boltdb"` Host string `yaml:"Host"` Port int64 `yaml:"Port"` User string `yaml:"User"` @@ -35,6 +34,10 @@ type Store struct { RedisPrefix string `yaml:"RedisPrefix" default:"{}"` // Redis storage prefix. stored to only one slot in cluster } +func (s *Store) IsDB() bool { + return s.Driver == dtmcli.DBTypeMysql || s.Driver == dtmcli.DBTypePostgres +} + func (s *Store) GetDBConf() dtmcli.DBConf { return dtmcli.DBConf{ Driver: s.Driver, @@ -60,10 +63,6 @@ type configType struct { // Config 配置 var Config = configType{} -func getIntEnv(key string, defaultV string) int64 { - return int64(dtmimp.MustAtoi(dtmimp.OrString(os.Getenv(key), defaultV))) -} - func MustLoadConfig() { loadFromEnv("", &Config) cont := []byte{} @@ -91,8 +90,8 @@ func MustLoadConfig() { } func checkConfig() error { - if Config.Store.Driver == "" { - return errors.New("db driver empty") + if Config.Store.Driver == "boltdb" { + return nil } else if Config.Store.Driver == "redis" && (Config.Store.Host == "" || Config.Store.Port == 0) { return errors.New("db redis config not valid") } else if Config.Store.Driver != "redis" && (Config.Store.User == "" || Config.Store.Host == "" || Config.Store.Port == 0) { diff --git a/common/db.go b/common/db.go index 2fae98d..4baf68c 100644 --- a/common/db.go +++ b/common/db.go @@ -137,14 +137,15 @@ func WaitDBUp() { _, err = rdb.Ping(context.Background()).Result() } return - } - sdb, err := dtmimp.StandaloneDB(Config.Store.GetDBConf()) - dtmimp.FatalIfError(err) - defer func() { - sdb.Close() - }() - for _, err = dtmimp.DBExec(sdb, "select 1"); err != nil; { // wait for mysql to start - time.Sleep(3 * time.Second) - _, err = dtmimp.DBExec(sdb, "select 1") + } else if Config.Store.IsDB() { + sdb, err := dtmimp.StandaloneDB(Config.Store.GetDBConf()) + dtmimp.FatalIfError(err) + defer func() { + sdb.Close() + }() + for _, err = dtmimp.DBExec(sdb, "select 1"); err != nil; { // wait for mysql to start + time.Sleep(3 * time.Second) + _, err = dtmimp.DBExec(sdb, "select 1") + } } } diff --git a/common/types_test.go b/common/types_test.go index 6fc58e3..4607647 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -15,9 +15,7 @@ import ( func TestGeneralDB(t *testing.T) { MustLoadConfig() - if Config.Store.Driver == "redis" { - - } else { + if Config.Store.IsDB() { testSql(t) testDbAlone(t) } diff --git a/conf.sample.yml b/conf.sample.yml index 66d8828..79a9fe2 100644 --- a/conf.sample.yml +++ b/conf.sample.yml @@ -1,19 +1,19 @@ -Store: - Driver: 'redis' - Host: 'localhost' - User: '' - Password: '' - Port: 6379 +# Store: +# Driver: 'redis' +# Host: 'localhost' +# User: '' +# Password: '' +# Port: 6379 - # driver: 'postgres' - # host: 'localhost' - # user: 'postgres' - # password: 'mysecretpassword' - # port: '5432' +# driver: 'postgres' +# host: 'localhost' +# user: 'postgres' +# password: 'mysecretpassword' +# port: '5432' - MaxOpenConns: 499 - # max_idle_conns: 'dbmaxidleconns' - # conn_max_life_time: 'dbconnmaxlifetime' +# MaxOpenConns: 499 +# max_idle_conns: 'dbmaxidleconns' +# conn_max_life_time: 'dbconnmaxlifetime' # MicroService: # Driver: 'dtm-driver-gozero' # name of the driver to handle register/discover # Target: 'etcd://localhost:2379/dtmservice' # register dtm server to this url diff --git a/helper/compose.redis.yml b/helper/compose.redis.yml deleted file mode 100644 index 362a26c..0000000 --- a/helper/compose.redis.yml +++ /dev/null @@ -1,9 +0,0 @@ -version: '3.3' -services: - redis: - image: 'redis' - volumes: - - /etc/localtime:/etc/localtime:ro - - /etc/timezone:/etc/timezone:ro - ports: - - '6379:6379' diff --git a/helper/compose.dev.yml b/helper/compose.store.yml similarity index 54% rename from helper/compose.dev.yml rename to helper/compose.store.yml index d904d35..75a8868 100644 --- a/helper/compose.dev.yml +++ b/helper/compose.store.yml @@ -1,23 +1,5 @@ version: '3.3' services: - api: - image: golang:1.16.6-alpine3.14 - extra_hosts: - - 'host.docker.internal:host-gateway' - volumes: - - /etc/localtime:/etc/localtime:ro - - /etc/timezone:/etc/timezone:ro - environment: - IS_DOCKER: '1' - GOPROXY: 'https://mirrors.aliyun.com/goproxy/,direct' - ports: - - '8080:8080' - - '8082:8082' - - '58080:58080' - volumes: - - ..:/app/work - command: ['go', 'run', '/app/work/app/main.go', 'dev'] - working_dir: /app/work mysql: image: 'mysql:5.7' volumes: @@ -32,3 +14,10 @@ services: ] ports: - '3306:3306' + redis: + image: 'redis' + volumes: + - /etc/localtime:/etc/localtime:ro + - /etc/timezone:/etc/timezone:ro + ports: + - '6379:6379' diff --git a/test/main_test.go b/test/main_test.go index d267507..6e9a229 100644 --- a/test/main_test.go +++ b/test/main_test.go @@ -7,6 +7,7 @@ package test import ( + "os" "testing" "time" @@ -17,6 +18,12 @@ import ( "github.com/yedf/dtm/examples" ) +func exitIf(code int) { + if code != 0 { + os.Exit(code) + } +} + func TestMain(m *testing.M) { common.MustLoadConfig() dtmcli.SetCurrentDBType(common.Config.ExamplesDB.Driver) @@ -33,19 +40,18 @@ func TestMain(m *testing.M) { return disorderHandler(c) })) - config.Store.Driver = "boltdb" - dtmsvr.PopulateDB(false) - examples.PopulateDB(false) - - m.Run() - config.Store.Driver = "redis" config.Store.Host = "localhost" config.Store.Port = 6379 dtmsvr.PopulateDB(false) examples.PopulateDB(false) + exitIf(m.Run()) + + config.Store.Driver = "boltdb" + dtmsvr.PopulateDB(false) + examples.PopulateDB(false) + exitIf(m.Run()) - m.Run() config.Store.Driver = "mysql" config.Store.Host = "localhost" config.Store.Port = 3306 @@ -53,5 +59,5 @@ func TestMain(m *testing.M) { config.Store.Password = "" dtmsvr.PopulateDB(false) examples.PopulateDB(false) - m.Run() + exitIf(m.Run()) }