Skip to content

Commit a7c22c2

Browse files
authored
Merge pull request #20 from rog-golang-buddies/feature/configuration
Add config population from the environment.
2 parents c687b83 + afce648 commit a7c22c2

File tree

7 files changed

+76
-23
lines changed

7 files changed

+76
-23
lines changed

docker/docker-compose-dev.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ services:
1313
build:
1414
context: ../.
1515
dockerfile: Dockerfile
16+
environment:
17+
QUEUE_URL: amqp://guest:guest@rabbit:5672/
1618
restart: unless-stopped
1719
depends_on:
1820
- rabbit

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.18
44

55
require (
66
github.com/golang/mock v1.6.0
7+
github.com/kelseyhightower/envconfig v1.4.0
78
github.com/rabbitmq/amqp091-go v1.4.0
89
github.com/stretchr/testify v1.8.0
910
github.com/wagslane/go-rabbitmq v0.10.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
55
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
6+
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
7+
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
68
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
79
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
810
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

internal/app.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,32 @@ func Start() int {
1313
ctx, cancel := context.WithCancel(context.Background())
1414
defer cancel()
1515

16-
conf := config.ReadConfig() //read configuration from file & env
16+
conf, err := config.ReadConfig() //read configuration from file & env
17+
if err != nil {
18+
log.Println("error while reading configuration")
19+
return 1
20+
}
21+
1722
//initialize publisher connection to the queue
1823
//this library assumes using one publisher and one consumer per application
1924
//https://github.com/wagslane/go-rabbitmq/issues/79
20-
pub, err := publisher.NewPublisher(conf.QueueConfig) //TODO pass logger here and add it to publisher options
25+
pub, err := publisher.NewPublisher(conf.Queue) //TODO pass logger here and add it to publisher options
2126
if err != nil {
2227
log.Println("error while starting publisher: ", err)
2328
return 1
2429
}
2530
defer publisher.ClosePublisher(pub)
2631
//initialize consumer connection to the queue
27-
consumer, err := queue.NewConsumer(conf.QueueConfig) //TODO pass logger here and add it to consumer options
32+
consumer, err := queue.NewConsumer(conf.Queue) //TODO pass logger here and add it to consumer options
2833
if err != nil {
2934
log.Println("error while connecting to the queue: ", err)
3035
return 1
3136
}
3237
defer queue.CloseConsumer(consumer)
3338

34-
handl := handler.NewApiSpecDocHandler(pub, conf.QueueConfig)
39+
handl := handler.NewApiSpecDocHandler(pub, conf.Queue)
3540
listener := queue.NewListener()
36-
err = listener.Start(consumer, &conf.QueueConfig, handl)
41+
err = listener.Start(consumer, &conf.Queue, handl)
3742
if err != nil {
3843
log.Println("error while listening queue ", err)
3944
return 1

internal/config/application.go

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

3+
import (
4+
"fmt"
5+
"github.com/kelseyhightower/envconfig"
6+
)
7+
38
type ApplicationConfig struct {
4-
QueueConfig QueueConfig
9+
Queue QueueConfig
510
}
611

7-
func ReadConfig() ApplicationConfig {
8-
//Stub this method before the configuration task is not resolved
9-
//https://github.com/rog-golang-buddies/api-hub_data-scraping-service/issues/10
10-
//TODO implement with the method to read configuration from file and env
11-
return ApplicationConfig{
12-
QueueConfig: QueueConfig{
13-
UrlRequestQueue: "data-scraping-asd",
14-
ScrapingResultQueue: "storage-update-asd",
15-
NotificationQueue: "gateway-scrape_notifications",
16-
Url: "amqp://guest:guest@rabbit:5672/",
17-
Concurrency: 10,
18-
},
12+
//ReadConfig reads configuration from the environment and populates the structure with it
13+
func ReadConfig() (*ApplicationConfig, error) {
14+
var conf ApplicationConfig
15+
if err := envconfig.Process("", &conf); err != nil {
16+
return nil, err
1917
}
18+
fmt.Printf("conf: %+v\n", conf)
19+
return &conf, nil
2020
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package config
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"strconv"
7+
"testing"
8+
)
9+
10+
func TestReadConfig(t *testing.T) {
11+
expUrlRequestQueue := "url_request_queue"
12+
err := os.Setenv("QUEUE_URL_REQUEST_QUEUE", expUrlRequestQueue)
13+
assert.Nil(t, err)
14+
15+
expScrResQueue := "scraping_res_queue"
16+
err = os.Setenv("QUEUE_SCRAPING_RESULT_QUEUE", expScrResQueue)
17+
assert.Nil(t, err)
18+
19+
expNotificationQueue := "test_notifications"
20+
err = os.Setenv("QUEUE_NOTIFICATION_QUEUE", expNotificationQueue)
21+
assert.Nil(t, err)
22+
23+
expQueueUrl := "test_url"
24+
err = os.Setenv("QUEUE_URL", expQueueUrl)
25+
assert.Nil(t, err)
26+
27+
expQueueConcurrency := 50
28+
err = os.Setenv("QUEUE_CONCURRENCY", strconv.Itoa(expQueueConcurrency))
29+
assert.Nil(t, err)
30+
31+
conf, err := ReadConfig()
32+
assert.Nil(t, err)
33+
assert.Equal(t, expUrlRequestQueue, conf.Queue.UrlRequestQueue)
34+
assert.Equal(t, expScrResQueue, conf.Queue.ScrapingResultQueue)
35+
assert.Equal(t, expNotificationQueue, conf.Queue.NotificationQueue)
36+
assert.Equal(t, expQueueUrl, conf.Queue.Url)
37+
assert.Equal(t, expQueueConcurrency, conf.Queue.Concurrency)
38+
}

internal/config/queue.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ package config
22

33
//QueueConfig queue configuration
44
type QueueConfig struct {
5-
UrlRequestQueue string //UrlRequestQueue name to listen to the new events
6-
ScrapingResultQueue string //Queue name to send processed ApiSpecDoc
7-
NotificationQueue string //Queue name to notify a user about error or success (if required)
8-
Url string //RabbitMQ url
9-
Concurrency int //Number of parallel handlers
5+
//UrlRequestQueue name to listen to the new events
6+
UrlRequestQueue string `default:"data-scraping-asd" envconfig:"URL_REQUEST_QUEUE"`
7+
//ScrapingResultQueue represents a queue name to send processed ApiSpecDoc
8+
ScrapingResultQueue string `default:"storage-update-asd" envconfig:"SCRAPING_RESULT_QUEUE"`
9+
//NotificationQueue represents a queue name to notify a user about an error or success (if required)
10+
NotificationQueue string `default:"gateway-scrape-notifications" envconfig:"NOTIFICATION_QUEUE"`
11+
//Url is a RabbitMQ url
12+
Url string `default:"amqp://guest:guest@localhost:5672/"`
13+
//Concurrency represents number of parallel handlers
14+
Concurrency int `default:"30"`
1015
}

0 commit comments

Comments
 (0)