Skip to content

Commit 8666317

Browse files
committed
feat: Add initial queue event listening logic.
1 parent 9f41f56 commit 8666317

File tree

8 files changed

+156
-16
lines changed

8 files changed

+156
-16
lines changed

cmd/main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/config"
5+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue"
6+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue/handler"
7+
"log"
8+
)
49

510
func main() {
6-
// Feel free to delete this file.
7-
fmt.Println("Hello Gophers")
11+
conf := config.ReadConfig()
12+
13+
handl := handler.NewApiSpecDocHandler()
14+
listener := queue.NewListener(&conf.QueueConfig, handl)
15+
16+
err := listener.Start()
17+
if err != nil {
18+
log.Println("error while listening queue ", err)
19+
return
20+
}
21+
log.Println("application stopped gracefully (not)")
822
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
module github.com/rog-golang-buddies/api-hub_data-scraping-service
22

33
go 1.18
4+
5+
require github.com/wagslane/go-rabbitmq v0.10.0
6+
7+
require github.com/rabbitmq/amqp091-go v1.4.0 // indirect

go.sum

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
5-
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
6-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
3+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
75
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/rabbitmq/amqp091-go v1.4.0 h1:T2G+J9W9OY4p64Di23J6yH7tOkMocgnESvYeBjuG9cY=
7+
github.com/rabbitmq/amqp091-go v1.4.0/go.mod h1:JsV0ofX5f1nwOGafb8L5rBItt9GyhfQfcJj+oyz0dGg=
88
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
10-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11-
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
12-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
9+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/wagslane/go-rabbitmq v0.10.0 h1:y9Bw8Q/9gOvsHfjMOGQjCW3033aYTKabxDm8eyjUGjs=
11+
github.com/wagslane/go-rabbitmq v0.10.0/go.mod h1:u6xM1V7OO4D0szUy/F6Bya/9r0lLae/2FXBijkAQmn0=
1312
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
13+
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
14+
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
1415
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1516
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
17+
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
1618
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
19+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
1720
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
1821
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
1922
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
@@ -28,13 +31,12 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
2831
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
2932
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
3033
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
34+
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
3135
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
32-
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
36+
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
3337
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3438
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3539
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
36-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3740
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
41+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3842
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
39-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
40-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/config/application.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package config
2+
3+
type ApplicationConfig struct {
4+
QueueConfig QueueConfig
5+
}
6+
7+
func ReadConfig() ApplicationConfig {
8+
//Stub this method before configuration task not resolved
9+
//https://github.com/rog-golang-buddies/api-hub_data-scraping-service/issues/10
10+
//TODO implement with method to read configuration from file and env
11+
return ApplicationConfig{
12+
QueueConfig: QueueConfig{
13+
ConsumerQueue: "data-scraping-asd",
14+
Url: "amqp://guest:guest@localhost:5672/",
15+
Concurrency: 10,
16+
},
17+
}
18+
}

internal/config/queue.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package config
2+
3+
//QueueConfig queue configuration
4+
type QueueConfig struct {
5+
ConsumerQueue string //ConsumerQueue name to listen for the new events
6+
ProducerQueue string //Queue name to send processed ApiSpecDoc
7+
ErrorQueue string //Queue name to notify user about error (if required)
8+
Url string //RabbitMQ url
9+
Concurrency int //Number of parallel handlers
10+
}

internal/queue/handler/apispec.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package handler
2+
3+
import (
4+
"github.com/wagslane/go-rabbitmq"
5+
"log"
6+
)
7+
8+
type ApiSpecDocHandler struct {
9+
}
10+
11+
func (asdh *ApiSpecDocHandler) Handle(delivery rabbitmq.Delivery) rabbitmq.Action {
12+
log.Printf("consumed: %v", string(delivery.Body))
13+
//call process here
14+
15+
//publish to the required queue success or error
16+
return rabbitmq.Ack
17+
}
18+
19+
func NewApiSpecDocHandler() Handler {
20+
return &ApiSpecDocHandler{}
21+
}

internal/queue/handler/handler.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package handler
2+
3+
import "github.com/wagslane/go-rabbitmq"
4+
5+
//Handler represents common interface to any queue message processing struct
6+
type Handler interface {
7+
//Handle message and return action to response to the queue
8+
Handle(delivery rabbitmq.Delivery) rabbitmq.Action
9+
}

internal/queue/listener.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package queue
2+
3+
import (
4+
"errors"
5+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/config"
6+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue/handler"
7+
"github.com/wagslane/go-rabbitmq"
8+
"log"
9+
)
10+
11+
//Listener represents consumer wrapper with the method to start listening for all events for this service
12+
type Listener interface {
13+
Start() error
14+
}
15+
16+
type ApiSpecListenerImpl struct {
17+
config *config.QueueConfig //configuration struct
18+
handler handler.Handler
19+
}
20+
21+
func (asl *ApiSpecListenerImpl) Start() error {
22+
if asl.config == nil {
23+
return errors.New("queue configuration must not be nil")
24+
}
25+
consumer, err := rabbitmq.NewConsumer(
26+
asl.config.Url,
27+
rabbitmq.Config{},
28+
rabbitmq.WithConsumerOptionsLogging,
29+
)
30+
if err != nil {
31+
return err
32+
}
33+
defer func() {
34+
log.Printf("closing consumer")
35+
err := consumer.Close()
36+
if err != nil {
37+
log.Println("error while closing consumer: ", err)
38+
}
39+
}()
40+
err = consumer.StartConsuming(
41+
asl.handler.Handle,
42+
asl.config.ConsumerQueue,
43+
[]string{}, //No binding, consuming with the default exchange directly by queue name
44+
rabbitmq.WithConsumeOptionsConcurrency(asl.config.Concurrency),
45+
rabbitmq.WithConsumeOptionsQueueDurable,
46+
rabbitmq.WithConsumeOptionsBindingExchangeDurable,
47+
)
48+
49+
if err != nil {
50+
return err
51+
}
52+
log.Println("listening stopped")
53+
54+
return nil
55+
}
56+
57+
func NewListener(config *config.QueueConfig, handler handler.Handler) Listener {
58+
return &ApiSpecListenerImpl{
59+
config: config,
60+
handler: handler,
61+
}
62+
}

0 commit comments

Comments
 (0)