Skip to content
This repository was archived by the owner on May 11, 2019. It is now read-only.

Commit e60be60

Browse files
committed
clean up
1 parent 1f2d60b commit e60be60

File tree

10 files changed

+101
-46
lines changed

10 files changed

+101
-46
lines changed

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,41 @@ goim 是 非常成功的 IM ( 即时消息平台), 依赖项为 kafka ( 消息
8181
参见 [/goim-usage-cn.md](goim-usage-cn.md) ( chinese )
8282

8383

84+
85+
为简化配置, 新增 /third-party/ 将 nats / liftbridge / discovery 放在这里, 并加入到 Makefile 中, 可以在本 repo 在编译依赖
86+
87+
88+
89+
请查看 [/Makefile](/Makefile) , 运行 make
90+
91+
**编译**
92+
```
93+
make setup
94+
make build
95+
```
96+
或者
97+
```
98+
make setup
99+
make build-mac
100+
```
101+
102+
**运行**
103+
```
104+
make run
105+
```
106+
107+
108+
109+
**清除编译**
110+
111+
```
112+
make clean
113+
```
114+
115+
116+
117+
118+
84119
____________________
85120

86121
goim v2.0

cmd/nats/comet/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func main() {
4343

4444
flag.Parse()
4545
var err error
46-
cfg, err = conf.Init(confPath)
46+
cfg, err = conf.Load(confPath)
4747
if err != nil {
4848
panic(err)
4949
}

cmd/nats/job/main.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"flag"
45
"os"
56
"os/signal"
67
"syscall"
@@ -9,6 +10,7 @@ import (
910

1011
"github.com/tsingson/ex-goim/goim-nats/job"
1112
"github.com/tsingson/ex-goim/goim-nats/job/conf"
13+
"github.com/tsingson/ex-goim/pkg/utils"
1214

1315
resolver "github.com/tsingson/discovery/naming/grpc"
1416
log "github.com/tsingson/zaplogger"
@@ -21,23 +23,14 @@ var (
2123

2224
func main() {
2325

24-
// path, _ := utils.GetCurrentExecDir()
25-
// confPath := path + "/job-config.toml"
26-
// flag.Parse()
27-
//
28-
// cfg, err := conf.Init(confPath)
29-
// if err != nil {
30-
// panic(err)
31-
// }
26+
path, _ := utils.GetCurrentExecDir()
27+
confPath := path + "/job-config.toml"
28+
flag.Parse()
3229

33-
cfg = conf.Default()
34-
// env := &conf.Env{
35-
// Region: "china",
36-
// Zone: "gd",
37-
// DeployEnv: "dev",
38-
// Host: "job",
39-
// }
40-
// cfg.Env = env
30+
cfg, err := conf.Load(confPath)
31+
if err != nil {
32+
panic(err)
33+
}
4134

4235
log.Infof("goim-job [version: %s env: %+v] start", ver, cfg.Env)
4336
// grpc register naming

cmd/nats/logic/main.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strconv"
99
"syscall"
1010

11-
"github.com/sanity-io/litter"
1211
"github.com/tsingson/discovery/naming"
1312
resolver "github.com/tsingson/discovery/naming/grpc"
1413
"github.com/tsingson/fastx/utils"
@@ -35,14 +34,12 @@ func main() {
3534
confPath := path + "/logic-config.toml"
3635

3736
var err error
38-
cfg, err = conf.LoadToml(confPath)
37+
cfg, err = conf.Load(confPath)
3938

4039
if err != nil {
4140
panic(err)
4241
}
4342

44-
litter.Dump(cfg)
45-
4643
{
4744
// env := &conf.Env{
4845
// Region: "china",

goim-nats/comet/conf/conf.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ type Whitelist struct {
109109
WhiteLog string
110110
}
111111

112-
// Init init config.
113-
func Init(path string) (cfg *Config, err error) {
112+
// Load init config.
113+
func Load(path string) (cfg *Config, err error) {
114114

115115
if len(path) == 0 {
116116
return cfg, xerrors.New("config path is nil")

goim-nats/job/conf/conf.go

+25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package conf
33
import (
44
"time"
55

6+
"github.com/BurntSushi/toml"
7+
"github.com/imdario/mergo"
68
"github.com/tsingson/discovery/naming"
9+
"golang.org/x/xerrors"
710

811
xtime "github.com/tsingson/ex-goim/pkg/time"
912
)
@@ -107,3 +110,25 @@ func Default() *Config {
107110
},
108111
}
109112
}
113+
114+
// Load init config.
115+
func Load(path string) (cfg *Config, err error) {
116+
117+
if len(path) == 0 {
118+
return cfg, xerrors.New("config path is nil")
119+
}
120+
121+
Conf = Default()
122+
cfg = Default()
123+
124+
_, err = toml.DecodeFile(path, &cfg)
125+
if err != nil {
126+
return
127+
}
128+
err = mergo.Merge(&Conf, cfg, mergo.WithOverride)
129+
if err != nil {
130+
return Conf, err
131+
}
132+
133+
return Conf, nil
134+
}

goim-nats/logic/conf/conf.go

+24-18
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"time"
55

66
"github.com/BurntSushi/toml"
7+
"github.com/imdario/mergo"
78
"github.com/tsingson/discovery/naming"
89
"golang.org/x/xerrors"
910

1011
xtime "github.com/tsingson/ex-goim/pkg/time"
1112
)
1213

13-
// LogicConfig config.
14+
// Config config.
1415
type Config struct {
1516
Env *Env
1617
Discovery *naming.Config
@@ -25,6 +26,7 @@ type Config struct {
2526
Regions map[string][]string
2627
}
2728

29+
// LogicConfig as alias of Config
2830
type LogicConfig = Config
2931

3032
// Env is env config.
@@ -84,6 +86,8 @@ type Nats struct {
8486
ChannelID string // "channel-stream"
8587
AckInbox string // "acks"
8688
}
89+
90+
// NatsConfig as alias of Nats
8791
type NatsConfig = Nats
8892

8993
// RPCClient is RPC client config.
@@ -126,30 +130,32 @@ var (
126130

127131
func init() {
128132
Conf = Default()
129-
// var (
130-
// defHost, _ = os.Hostname()
131-
// defWeight, _ = strconv.ParseInt(os.Getenv("WEIGHT"), 10, 32)
132-
// )
133-
// flag.StringVar(&confPath, "conf", "logic-example.toml", "default config path")
134-
// flag.StringVar(&region, "region", os.Getenv("REGION"), "avaliable region. or use REGION env variable, value: sh etc.")
135-
// flag.StringVar(&zone, "zone", os.Getenv("ZONE"), "avaliable zone. or use ZONE env variable, value: sh001/sh002 etc.")
136-
// flag.StringVar(&deployEnv, "deploy.env", os.Getenv("DEPLOY_ENV"), "deploy env. or use DEPLOY_ENV env variable, value: dev/fat1/uat/pre/prod etc.")
137-
// flag.StringVar(&host, "host", defHost, "machine hostname. or use default machine hostname.")
138-
// flag.Int64Var(&weight, "weight", defWeight, "load balancing weight, or use WEIGHT env variable, value: 10 etc.")
133+
139134
}
140135

141-
// Init init config.
142-
func Init(path string) (cfg *Config, err error) {
136+
// Load init config.
137+
func Load(path string) (cfg *Config, err error) {
138+
139+
if len(path) == 0 {
140+
return cfg, xerrors.New("config path is nil")
141+
}
142+
143143
Conf = Default()
144-
if len(path) > 0 {
145-
_, err = toml.DecodeFile(path, &Conf)
146-
} else {
147-
_, err = toml.DecodeFile(confPath, &Conf)
144+
cfg = Default()
145+
146+
_, err = toml.DecodeFile(path, &cfg)
147+
if err != nil {
148+
return
148149
}
150+
err = mergo.Merge(&Conf, cfg, mergo.WithOverride)
151+
if err != nil {
152+
return Conf, err
153+
}
154+
149155
return Conf, nil
150156
}
151157

152-
// Init init config.
158+
// LoadToml init config.
153159
func LoadToml(path string) (cfg *Config, err error) {
154160
Conf = Default()
155161
if len(path) == 0 {

third-party/discoveryd/init.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import (
55

66
"github.com/spf13/afero"
77

8-
config "github.com/tsingson/discovery/conf"
8+
config "github.com/tsingson/discovery/conf"
99
)
1010

1111
var ( // global variable
1212
// cacheSize int
1313
// cacheTimeOut int64
1414
path, logPath string
15-
cfg *config.Config
15+
cfg *config.Config
1616
)
1717

1818
func init() {

third-party/discoveryd/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ func main() {
2020
runtime.MemProfileRate = 0
2121
runtime.GOMAXPROCS(128)
2222

23-
2423
/**
2524
tw = timingwheel.NewTimingWheel(time.Minute, 60)
2625
tw.StartCron()
@@ -50,7 +49,7 @@ func main() {
5049

5150
svr, cancel := discovery.New(cfg)
5251

53-
http.Init(cfg, svr)
52+
http.Init(cfg, svr)
5453

5554
// if err != nil {
5655
// cancel()

third-party/gnatsd/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func main() {
2525
runtime.MemProfileRate = 0
2626
runtime.GOMAXPROCS(128)
2727
signal := make(chan struct{})
28-
s:= RunDefaultServer()
28+
s := RunDefaultServer()
2929
s.Start()
3030
defer s.Shutdown()
3131
<-signal

0 commit comments

Comments
 (0)