Skip to content

Commit

Permalink
improve config: moved the config create to the relevant package (#98)
Browse files Browse the repository at this point in the history
Signed-off-by: Login Victor <[email protected]>

mend

improve config: moved the config create to the relevant package
  • Loading branch information
batazor committed Nov 30, 2021
1 parent c8549e4 commit 1fe49a4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
8 changes: 2 additions & 6 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ package main
import (
"log"

"github.com/ilyakaznacheev/cleanenv"

"github.com/evrone/go-clean-template/config"
"github.com/evrone/go-clean-template/internal/app"
)

func main() {
// Configuration
var cfg config.Config

err := cleanenv.ReadConfig("./config/config.yml", &cfg)
cfg, err := config.NewConfig()
if err != nil {
log.Fatalf("Config error: %s", err)
}

// Run
app.Run(&cfg)
app.Run(cfg)
}
23 changes: 23 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package config

import (
"fmt"

"github.com/ilyakaznacheev/cleanenv"
)

type (
// Config -.
Config struct {
Expand Down Expand Up @@ -39,3 +45,20 @@ type (
URL string `env-required:"true" env:"RMQ_URL"`
}
)

// NewConfig returns app config.
func NewConfig() (*Config, error) {
cfg := &Config{}

err := cleanenv.ReadConfig("./config/config.yml", cfg)
if err != nil {
return nil, fmt.Errorf("config error: %w", err)
}

err = cleanenv.ReadEnv(cfg)
if err != nil {
return nil, err
}

return cfg, nil
}
14 changes: 7 additions & 7 deletions pkg/rabbitmq/rmq_rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Client struct {
error chan error
stop chan struct{}

sync.RWMutex
rw sync.RWMutex
calls map[string]*pendingCall

timeout time.Duration
Expand Down Expand Up @@ -198,9 +198,9 @@ func (c *Client) reconnect() {
}

func (c *Client) getCall(d *amqp.Delivery) {
c.RLock()
c.rw.RLock()
call, ok := c.calls[d.CorrelationId]
c.RUnlock()
c.rw.RUnlock()

if !ok {
return
Expand All @@ -212,15 +212,15 @@ func (c *Client) getCall(d *amqp.Delivery) {
}

func (c *Client) addCall(corrID string, call *pendingCall) {
c.Lock()
c.rw.Lock()
c.calls[corrID] = call
c.Unlock()
c.rw.Unlock()
}

func (c *Client) deleteCall(corrID string) {
c.Lock()
c.rw.Lock()
delete(c.calls, corrID)
c.Unlock()
c.rw.Unlock()
}

// Notify -.
Expand Down

0 comments on commit 1fe49a4

Please sign in to comment.