Skip to content

Commit 351d9e9

Browse files
authored
Merge pull request #18 from pioneerlfn/master
change the manner we get the configure.
2 parents 40f15c3 + b780016 commit 351d9e9

15 files changed

+48
-65
lines changed

conf/gbe_config.go

+11-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package conf
1717
import (
1818
"encoding/json"
1919
"io/ioutil"
20-
"sync"
2120
)
2221

2322
type GbeConfig struct {
@@ -56,20 +55,16 @@ type RestServerConfig struct {
5655
Addr string `json:"addr"`
5756
}
5857

59-
var config GbeConfig
60-
var configOnce sync.Once
58+
var Config *GbeConfig
6159

62-
func GetConfig() *GbeConfig {
63-
configOnce.Do(func() {
64-
bytes, err := ioutil.ReadFile("conf.json")
65-
if err != nil {
66-
panic(err)
67-
}
68-
69-
err = json.Unmarshal(bytes, &config)
70-
if err != nil {
71-
panic(err)
72-
}
73-
})
74-
return &config
60+
func Init() {
61+
bytes, err := ioutil.ReadFile("conf.json")
62+
if err != nil {
63+
panic(err)
64+
}
65+
err = json.Unmarshal(bytes, &Config)
66+
if err != nil {
67+
panic(err)
68+
}
7569
}
70+

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ github.com/pingcap/kvproto v0.0.0-20190904075355-9a1bd6a31da2 h1:wBORZD4gvEKK0tG
188188
github.com/pingcap/kvproto v0.0.0-20190904075355-9a1bd6a31da2/go.mod h1:QMdbTAXCHzzygQzqcG9uVUgU2fKeSN1GmfMiykdSzzY=
189189
github.com/pingcap/parser v0.0.0-20190506092653-e336082eb825 h1:U9Kdnknj4n2v76Mg7wazevZ5N9U1OIaMwSNRVLEcLX0=
190190
github.com/pingcap/parser v0.0.0-20190506092653-e336082eb825/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
191+
github.com/pingcap/pd v2.1.18+incompatible h1:ytgHQQlEwvuYWN6gKjivUq8QOL7TIeaPfb5kapaaFNo=
191192
github.com/pingcap/tidb v2.0.11+incompatible h1:Shz+ry1DzQNsPk1QAejnM+5tgjbwZuzPnIER5aCjQ6c=
192193
github.com/pingcap/tidb v2.0.11+incompatible/go.mod h1:I8C6jrPINP2rrVunTRd7C9fRRhQrtR43S1/CL5ix/yQ=
193194
github.com/pingcap/tipb v0.0.0-20190428032612-535e1abaa330 h1:rRMLMjIMFulCX9sGKZ1hoov/iROMsKyC8Snc02nSukw=

main.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
package main
1616

1717
import (
18+
"net/http"
19+
_ "net/http/pprof"
20+
1821
"github.com/gitbitex/gitbitex-spot/conf"
1922
"github.com/gitbitex/gitbitex-spot/matching"
2023
"github.com/gitbitex/gitbitex-spot/models"
@@ -23,12 +26,11 @@ import (
2326
"github.com/gitbitex/gitbitex-spot/service"
2427
"github.com/gitbitex/gitbitex-spot/worker"
2528
"github.com/prometheus/common/log"
26-
"net/http"
27-
_ "net/http/pprof"
2829
)
2930

3031
func main() {
31-
gbeConfig := conf.GetConfig()
32+
// gbeConfig := conf.GetConfig()
33+
conf.Init()
3234

3335
go func() {
3436
log.Info(http.ListenAndServe("localhost:6060", nil))
@@ -47,9 +49,9 @@ func main() {
4749
panic(err)
4850
}
4951
for _, product := range products {
50-
worker.NewTickMaker(product.Id, matching.NewKafkaLogReader("tickMaker", product.Id, gbeConfig.Kafka.Brokers)).Start()
51-
worker.NewFillMaker(matching.NewKafkaLogReader("fillMaker", product.Id, gbeConfig.Kafka.Brokers)).Start()
52-
worker.NewTradeMaker(matching.NewKafkaLogReader("tradeMaker", product.Id, gbeConfig.Kafka.Brokers)).Start()
52+
worker.NewTickMaker(product.Id, matching.NewKafkaLogReader("tickMaker", product.Id, conf.Config.Kafka.Brokers)).Start()
53+
worker.NewFillMaker(matching.NewKafkaLogReader("fillMaker", product.Id, conf.Config.Kafka.Brokers)).Start()
54+
worker.NewTradeMaker(matching.NewKafkaLogReader("tradeMaker", product.Id, conf.Config.Kafka.Brokers)).Start()
5355
}
5456

5557
rest.StartServer()

matching/bootstrap.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ import (
2121
)
2222

2323
func StartEngine() {
24-
gbeConfig := conf.GetConfig()
25-
2624
products, err := service.GetProducts()
2725
if err != nil {
2826
panic(err)
2927
}
3028
for _, product := range products {
31-
orderReader := NewKafkaOrderReader(product.Id, gbeConfig.Kafka.Brokers)
29+
orderReader := NewKafkaOrderReader(product.Id, conf.Config.Kafka.Brokers)
3230
snapshotStore := NewRedisSnapshotStore(product.Id)
33-
logStore := NewKafkaLogStore(product.Id, gbeConfig.Kafka.Brokers)
31+
logStore := NewKafkaLogStore(product.Id, conf.Config.Kafka.Brokers)
3432
matchEngine := NewEngine(product, orderReader, logStore, snapshotStore)
3533
matchEngine.Start()
3634
}

matching/redis_snapshot_store.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ type RedisSnapshotStore struct {
3131
}
3232

3333
func NewRedisSnapshotStore(productId string) SnapshotStore {
34-
gbeConfig := conf.GetConfig()
3534

3635
redisClient := redis.NewClient(&redis.Options{
37-
Addr: gbeConfig.Redis.Addr,
38-
Password: gbeConfig.Redis.Password,
36+
Addr: conf.Config.Redis.Addr,
37+
Password: conf.Config.Redis.Password,
3938
DB: 0,
4039
})
4140

models/binlog_stream.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ type BinLogStream struct {
3232
}
3333

3434
func NewBinLogStream() *BinLogStream {
35-
gbeConfig := conf.GetConfig()
36-
3735
redisClient := redis.NewClient(&redis.Options{
38-
Addr: gbeConfig.Redis.Addr,
39-
Password: gbeConfig.Redis.Password,
36+
Addr: conf.Config.Redis.Addr,
37+
Password: conf.Config.Redis.Password,
4038
DB: 0,
4139
})
4240

@@ -158,16 +156,15 @@ func (s *BinLogStream) getColumnIndexByName(e *canal.RowsEvent, name string) int
158156
}
159157

160158
func (s *BinLogStream) Start() {
161-
gbeConfig := conf.GetConfig()
162159

163160
cfg := canal.NewDefaultConfig()
164-
cfg.Addr = gbeConfig.DataSource.Addr
165-
cfg.User = gbeConfig.DataSource.User
166-
cfg.Password = gbeConfig.DataSource.Password
161+
cfg.Addr = conf.Config.DataSource.Addr
162+
cfg.User = conf.Config.DataSource.User
163+
cfg.Password = conf.Config.DataSource.Password
167164
cfg.Dump.ExecutionPath = ""
168-
cfg.Dump.TableDB = gbeConfig.DataSource.Database
165+
cfg.Dump.TableDB = conf.Config.DataSource.Database
169166
cfg.ParseTime = true
170-
cfg.IncludeTableRegex = []string{gbeConfig.DataSource.Database + "\\..*"}
167+
cfg.IncludeTableRegex = []string{conf.Config.DataSource.Database + "\\..*"}
171168
cfg.ExcludeTableRegex = []string{"mysql\\..*"}
172169
c, err := canal.NewCanal(cfg)
173170
if err != nil {

models/mysql/store.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewStore(db *gorm.DB) *Store {
5050
}
5151

5252
func initDb() error {
53-
cfg := conf.GetConfig()
53+
cfg := conf.Config
5454

5555
url := fmt.Sprintf("%v:%v@tcp(%v)/%v?charset=utf8&parseTime=True&loc=Local",
5656
cfg.DataSource.User, cfg.DataSource.Password, cfg.DataSource.Addr, cfg.DataSource.Database)

pushing/bootstrap.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
)
2323

2424
func StartServer() {
25-
gbeConfig := conf.GetConfig()
2625

2726
sub := newSubscription()
2827

@@ -33,12 +32,12 @@ func StartServer() {
3332
panic(err)
3433
}
3534
for _, product := range products {
36-
newTickerStream(product.Id, sub, matching.NewKafkaLogReader("tickerStream", product.Id, gbeConfig.Kafka.Brokers)).Start()
37-
newMatchStream(product.Id, sub, matching.NewKafkaLogReader("matchStream", product.Id, gbeConfig.Kafka.Brokers)).Start()
38-
newOrderBookStream(product.Id, sub, matching.NewKafkaLogReader("orderBookStream", product.Id, gbeConfig.Kafka.Brokers)).Start()
35+
newTickerStream(product.Id, sub, matching.NewKafkaLogReader("tickerStream", product.Id, conf.Config.Kafka.Brokers)).Start()
36+
newMatchStream(product.Id, sub, matching.NewKafkaLogReader("matchStream", product.Id, conf.Config.Kafka.Brokers)).Start()
37+
newOrderBookStream(product.Id, sub, matching.NewKafkaLogReader("orderBookStream", product.Id, conf.Config.Kafka.Brokers)).Start()
3938
}
4039

41-
go NewServer(gbeConfig.PushServer.Addr, gbeConfig.PushServer.Path, sub).Run()
40+
go NewServer(conf.Config.PushServer.Addr, conf.Config.PushServer.Path, sub).Run()
4241

4342
log.Info("websocket server ok")
4443
}

pushing/order_book.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,10 @@ var onceStore sync.Once
202202

203203
func sharedSnapshotStore() *redisSnapshotStore {
204204
onceStore.Do(func() {
205-
gbeConfig := conf.GetConfig()
206205

207206
redisClient := redis.NewClient(&redis.Options{
208-
Addr: gbeConfig.Redis.Addr,
209-
Password: gbeConfig.Redis.Password,
207+
Addr: conf.Config.Redis.Addr,
208+
Password: conf.Config.Redis.Password,
210209
DB: 0,
211210
})
212211

pushing/redis_stream.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ func newRedisStream(sub *subscription) *redisStream {
3838
}
3939

4040
func (s *redisStream) Start() {
41-
gbeConfig := conf.GetConfig()
4241

4342
redisClient := redis.NewClient(&redis.Options{
44-
Addr: gbeConfig.Redis.Addr,
45-
Password: gbeConfig.Redis.Password,
43+
Addr: conf.Config.Redis.Addr,
44+
Password: conf.Config.Redis.Password,
4645
DB: 0,
4746
})
4847
_, err := redisClient.Ping().Result()

rest/bootstrap.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ import (
2020
)
2121

2222
func StartServer() {
23-
gbeConfig := conf.GetConfig()
2423

25-
httpServer := NewHttpServer(gbeConfig.RestServer.Addr)
24+
httpServer := NewHttpServer(conf.Config.RestServer.Addr)
2625
go httpServer.Start()
2726

2827
log.Info("rest server ok")

rest/order_controller.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ func getWriter(productId string) *kafka.Writer {
4444
return writer.(*kafka.Writer)
4545
}
4646

47-
gbeConfig := conf.GetConfig()
48-
4947
newWriter := kafka.NewWriter(kafka.WriterConfig{
50-
Brokers: gbeConfig.Kafka.Brokers,
48+
Brokers: conf.Config.Kafka.Brokers,
5149
Topic: matching.TopicOrderPrefix + productId,
5250
Balancer: &kafka.LeastBytes{},
5351
BatchTimeout: 5 * time.Millisecond,

service/user_service.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ func RefreshAccessToken(email, password string) (string, error) {
6363
"expiredAt": time.Now().Unix(),
6464
}
6565
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claim)
66-
return token.SignedString([]byte(conf.GetConfig().JwtSecret))
66+
return token.SignedString([]byte(conf.Config.JwtSecret))
6767
}
6868

6969
func CheckToken(tokenStr string) (*models.User, error) {
7070
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
71-
return []byte(conf.GetConfig().JwtSecret), nil
71+
return []byte(conf.Config.JwtSecret), nil
7272
})
7373
if err != nil {
7474
return nil, err

worker/bill_executor.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ func (s *BillExecutor) Start() {
5757
}
5858

5959
func (s *BillExecutor) runMqListener() {
60-
gbeConfig := conf.GetConfig()
61-
6260
redisClient := redis.NewClient(&redis.Options{
63-
Addr: gbeConfig.Redis.Addr,
64-
Password: gbeConfig.Redis.Password,
61+
Addr: conf.Config.Redis.Addr,
62+
Password: conf.Config.Redis.Password,
6563
DB: 0,
6664
})
6765

worker/fill_executor.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ func (s *FillExecutor) Start() {
8585

8686
// 监听消息队列通知
8787
func (s *FillExecutor) runMqListener() {
88-
gbeConfig := conf.GetConfig()
8988

9089
redisClient := redis.NewClient(&redis.Options{
91-
Addr: gbeConfig.Redis.Addr,
92-
Password: gbeConfig.Redis.Password,
90+
Addr: conf.Config.Redis.Addr,
91+
Password: conf.Config.Redis.Password,
9392
DB: 0,
9493
})
9594

0 commit comments

Comments
 (0)