Skip to content

Commit 092777f

Browse files
committed
Merge remote-tracking branch 'origin/main' into feature/logger
# Conflicts: # go.mod # go.sum # internal/app.go # internal/config/application.go
2 parents 14f8d52 + 0d013a5 commit 092777f

File tree

9 files changed

+87
-29
lines changed

9 files changed

+87
-29
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ repos:
99
- id: check-yaml
1010
- id: check-added-large-files
1111
- repo: https://github.com/golangci/golangci-lint
12-
rev: v1.47.3
12+
rev: v1.48.0
1313
hooks:
1414
- id: golangci-lint
1515
- repo: https://github.com/zricethezav/gitleaks
16-
rev: v8.9.0
16+
rev: v8.10.2
1717
hooks:
1818
- id: gitleaks
1919

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.18-alpine as builder
1+
FROM golang:1.19.0-alpine as builder
22

33
RUN apk update && apk upgrade && \
44
apk add --no-cache make bash

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ 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
8-
github.com/stretchr/testify v1.7.0
9+
github.com/stretchr/testify v1.8.0
910
github.com/wagslane/go-rabbitmq v0.10.0
1011
go.uber.org/zap v1.21.0
1112
)
@@ -16,4 +17,5 @@ require (
1617
go.uber.org/atomic v1.7.0 // indirect
1718
go.uber.org/multierr v1.6.0 // indirect
1819
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
20+
gopkg.in/yaml.v3 v3.0.1 // indirect
1921
)

go.sum

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
23
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
3-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
77
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
8+
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
9+
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
810
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
911
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1012
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -18,8 +20,11 @@ github.com/rabbitmq/amqp091-go v1.4.0 h1:T2G+J9W9OY4p64Di23J6yH7tOkMocgnESvYeBju
1820
github.com/rabbitmq/amqp091-go v1.4.0/go.mod h1:JsV0ofX5f1nwOGafb8L5rBItt9GyhfQfcJj+oyz0dGg=
1921
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2022
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
21-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
23+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
2224
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
25+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
26+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
27+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
2328
github.com/wagslane/go-rabbitmq v0.10.0 h1:y9Bw8Q/9gOvsHfjMOGQjCW3033aYTKabxDm8eyjUGjs=
2429
github.com/wagslane/go-rabbitmq v0.10.0/go.mod h1:u6xM1V7OO4D0szUy/F6Bya/9r0lLae/2FXBijkAQmn0=
2530
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
@@ -66,3 +71,5 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
6671
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6772
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
6873
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
74+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
75+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/app.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ func Start() int {
1414
ctx, cancel := context.WithCancel(context.Background())
1515
defer cancel()
1616

17-
conf := config.ReadConfig() //read configuration from file & env
17+
conf, err := config.ReadConfig() //read configuration from env
18+
if err != nil {
19+
fmt.Println("error while reading configuration")
20+
return 1
21+
}
1822
log, err := logger.NewLogger(&conf)
1923
if err != nil {
2024
fmt.Println("error creating logger: ", err)
@@ -23,23 +27,23 @@ func Start() int {
2327
//initialize publisher connection to the queue
2428
//this library assumes using one publisher and one consumer per application
2529
//https://github.com/wagslane/go-rabbitmq/issues/79
26-
pub, err := publisher.NewPublisher(conf.QueueConfig, log)
30+
pub, err := publisher.NewPublisher(conf.Queue, log)
2731
if err != nil {
2832
log.Error("error while starting publisher: ", err)
2933
return 1
3034
}
3135
defer publisher.ClosePublisher(pub, log)
3236
//initialize consumer connection to the queue
33-
consumer, err := queue.NewConsumer(conf.QueueConfig, log)
37+
consumer, err := queue.NewConsumer(conf.Queue, log)
3438
if err != nil {
3539
log.Error("error while connecting to the queue: ", err)
3640
return 1
3741
}
3842
defer queue.CloseConsumer(consumer, log)
3943

40-
handl := handler.NewApiSpecDocHandler(pub, conf.QueueConfig, log)
44+
handl := handler.NewApiSpecDocHandler(pub, conf.Queue, log)
4145
listener := queue.NewListener()
42-
err = listener.Start(consumer, &conf.QueueConfig, handl)
46+
err = listener.Start(consumer, &conf.Queue, handl)
4347
if err != nil {
4448
log.Error("error while listening queue ", err)
4549
return 1

internal/config/application.go

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

3+
import (
4+
"fmt"
5+
"github.com/kelseyhightower/envconfig"
6+
)
7+
38
type ApplicationConfig struct {
49
Env Environment `default:"dev"`
510
Logger LoggerConfig
6-
QueueConfig QueueConfig
11+
Queue QueueConfig
712
}
813

9-
func ReadConfig() ApplicationConfig {
10-
//Stub this method before the configuration task is not resolved
11-
//https://github.com/rog-golang-buddies/api-hub_data-scraping-service/issues/10
12-
//TODO implement with the method to read configuration from file and env
13-
return ApplicationConfig{
14-
QueueConfig: QueueConfig{
15-
UrlRequestQueue: "data-scraping-asd",
16-
ScrapingResultQueue: "storage-update-asd",
17-
NotificationQueue: "gateway-scrape_notifications",
18-
Url: "amqp://guest:guest@rabbit:5672/",
19-
Concurrency: 10,
20-
},
14+
//ReadConfig reads configuration from the environment and populates the structure with it
15+
func ReadConfig() (*ApplicationConfig, error) {
16+
var conf ApplicationConfig
17+
if err := envconfig.Process("", &conf); err != nil {
18+
return nil, err
2119
}
20+
fmt.Printf("conf: %+v\n", conf)
21+
return &conf, nil
2222
}

internal/config/application_test.go

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)