Skip to content

Commit de82ae2

Browse files
committed
feat: Add config population from the environment.
1 parent 79e12b5 commit de82ae2

File tree

6 files changed

+33
-23
lines changed

6 files changed

+33
-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.7.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
@@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
44
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
5+
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
6+
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
57
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
68
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
79
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 populate 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
}

internal/config/queue.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ 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 string `default:"data-scraping-asd"` //UrlRequestQueue name to listen to the new events
6+
ScrapingResultQueue string `default:"storage-update-asd"` //Queue name to send processed ApiSpecDoc
7+
NotificationQueue string `default:"gateway-scrape_notifications"` //Queue name to notify a user about error or success (if required)
8+
Url string `default:"amqp://guest:guest@localhost:5672/"` //RabbitMQ url
9+
Concurrency int `default:"30"` //Number of parallel handlers
1010
}

0 commit comments

Comments
 (0)