Browse Source

store seems ok

pull/90/head
yedf2 4 years ago
parent
commit
2947c1ef52
  1. 1
      .travis.yml
  2. 15
      common/config.go
  3. 19
      common/db.go
  4. 4
      common/types_test.go
  5. 28
      conf.sample.yml
  6. 9
      helper/compose.redis.yml
  7. 25
      helper/compose.store.yml
  8. 22
      test/main_test.go

1
.travis.yml

@ -10,6 +10,7 @@ branches:
- alpha
services:
- mysql
- redis-server
before_install:
- go get -t -v ./...
- go get github.com/mattn/goveralls

15
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) {

19
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")
}
}
}

4
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)
}

28
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

9
helper/compose.redis.yml

@ -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'

25
helper/compose.dev.yml → 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'

22
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())
}

Loading…
Cancel
Save