Skip to content

Commit 1fe49a4

Browse files
authored
improve config: moved the config create to the relevant package (#98)
Signed-off-by: Login Victor <[email protected]> mend improve config: moved the config create to the relevant package
1 parent c8549e4 commit 1fe49a4

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

cmd/app/main.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@ package main
33
import (
44
"log"
55

6-
"github.com/ilyakaznacheev/cleanenv"
7-
86
"github.com/evrone/go-clean-template/config"
97
"github.com/evrone/go-clean-template/internal/app"
108
)
119

1210
func main() {
1311
// Configuration
14-
var cfg config.Config
15-
16-
err := cleanenv.ReadConfig("./config/config.yml", &cfg)
12+
cfg, err := config.NewConfig()
1713
if err != nil {
1814
log.Fatalf("Config error: %s", err)
1915
}
2016

2117
// Run
22-
app.Run(&cfg)
18+
app.Run(cfg)
2319
}

config/config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package config
22

3+
import (
4+
"fmt"
5+
6+
"github.com/ilyakaznacheev/cleanenv"
7+
)
8+
39
type (
410
// Config -.
511
Config struct {
@@ -39,3 +45,20 @@ type (
3945
URL string `env-required:"true" env:"RMQ_URL"`
4046
}
4147
)
48+
49+
// NewConfig returns app config.
50+
func NewConfig() (*Config, error) {
51+
cfg := &Config{}
52+
53+
err := cleanenv.ReadConfig("./config/config.yml", cfg)
54+
if err != nil {
55+
return nil, fmt.Errorf("config error: %w", err)
56+
}
57+
58+
err = cleanenv.ReadEnv(cfg)
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
return cfg, nil
64+
}

pkg/rabbitmq/rmq_rpc/client/client.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Client struct {
4545
error chan error
4646
stop chan struct{}
4747

48-
sync.RWMutex
48+
rw sync.RWMutex
4949
calls map[string]*pendingCall
5050

5151
timeout time.Duration
@@ -198,9 +198,9 @@ func (c *Client) reconnect() {
198198
}
199199

200200
func (c *Client) getCall(d *amqp.Delivery) {
201-
c.RLock()
201+
c.rw.RLock()
202202
call, ok := c.calls[d.CorrelationId]
203-
c.RUnlock()
203+
c.rw.RUnlock()
204204

205205
if !ok {
206206
return
@@ -212,15 +212,15 @@ func (c *Client) getCall(d *amqp.Delivery) {
212212
}
213213

214214
func (c *Client) addCall(corrID string, call *pendingCall) {
215-
c.Lock()
215+
c.rw.Lock()
216216
c.calls[corrID] = call
217-
c.Unlock()
217+
c.rw.Unlock()
218218
}
219219

220220
func (c *Client) deleteCall(corrID string) {
221-
c.Lock()
221+
c.rw.Lock()
222222
delete(c.calls, corrID)
223-
c.Unlock()
223+
c.rw.Unlock()
224224
}
225225

226226
// Notify -.

0 commit comments

Comments
 (0)