-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f74b4a5
commit 8ed9baa
Showing
34 changed files
with
2,631 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
version: v1 | ||
plugins: | ||
- remote: buf.build/protocolbuffers/plugins/go:v1.28.0-1 | ||
out: proto | ||
out: proto/gen/ | ||
opt: paths=source_relative | ||
- remote: buf.build/grpc/plugins/go:v1.2.0-1 | ||
out: proto | ||
out: proto/gen/ | ||
opt: paths=source_relative,require_unimplemented_servers=false | ||
- remote: buf.build/grpc-ecosystem/plugins/grpc-gateway:v2.7.2-1 | ||
out: proto | ||
out: proto/gen/ | ||
opt: paths=source_relative | ||
- remote: buf.build/grpc-ecosystem/plugins/openapiv2:v2.7.2-1 | ||
out: third_party/OpenAPI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package event | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
||
amqp "github.com/rabbitmq/amqp091-go" | ||
) | ||
|
||
const ExchangeName = "orders_topic" | ||
|
||
type Consumer struct { | ||
conn *amqp.Connection | ||
} | ||
|
||
func NewConsumer(conn *amqp.Connection) (Consumer, error) { | ||
consumer := Consumer{ | ||
conn: conn, | ||
} | ||
|
||
err := consumer.setup() | ||
if err != nil { | ||
return Consumer{}, err | ||
} | ||
|
||
return consumer, nil | ||
} | ||
|
||
func (c *Consumer) setup() error { | ||
channel, err := c.conn.Channel() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return channel.ExchangeDeclare( | ||
ExchangeName, // name | ||
"topic", // type | ||
true, // durable | ||
false, // auto-deleted | ||
false, // internal | ||
false, // no-wait | ||
nil, // arguments | ||
) | ||
} | ||
|
||
type Payload struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
func (c *Consumer) Listen(topics []string) error { | ||
ch, err := c.conn.Channel() | ||
if err != nil { | ||
return err | ||
} | ||
defer ch.Close() | ||
|
||
q, err := ch.QueueDeclare( | ||
"", // name | ||
false, // durable | ||
false, // delete when unused | ||
true, // exclusive | ||
false, // no-wait | ||
nil, // arguments | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, s := range topics { | ||
err = ch.QueueBind( | ||
q.Name, | ||
s, | ||
ExchangeName, | ||
false, | ||
nil, | ||
) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
|
||
return err | ||
} | ||
} | ||
|
||
messages, err := ch.Consume(q.Name, "", true, false, false, false, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
forever := make(chan bool) | ||
|
||
go func() { | ||
for d := range messages { | ||
var payload Payload | ||
|
||
_ = json.Unmarshal(d.Body, &payload) | ||
|
||
go func() { | ||
switch payload.Name { | ||
case "drink_made": | ||
fmt.Println("Got it") | ||
default: | ||
fmt.Println("default") | ||
} | ||
}() | ||
} | ||
}() | ||
|
||
log.Printf("[*] Waiting for message [Exchange, Queue][%s, %s].", ExchangeName, q.Name) | ||
<-forever | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"math" | ||
"os" | ||
"time" | ||
|
||
amqp "github.com/rabbitmq/amqp091-go" | ||
"github.com/thangchung/go-coffeeshop/cmd/barista/event" | ||
) | ||
|
||
const ( | ||
RetryTimes = 5 | ||
PowOf = 2 | ||
) | ||
|
||
func main() { | ||
rabbitConn, err := connect() | ||
if err != nil { | ||
log.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
defer rabbitConn.Close() | ||
|
||
log.Println("Listening for and consuming RabbitMQ messages...") | ||
|
||
consumer, err := event.NewConsumer(rabbitConn) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = consumer.Listen([]string{"log.INFO", "log.WARNING", "log.ERROR"}) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
} | ||
|
||
func connect() (*amqp.Connection, error) { | ||
var ( | ||
counts int64 | ||
backOff = 1 * time.Second | ||
connection *amqp.Connection | ||
rabbitURL = "amqp://guest:guest@172.28.177.17:5672/" | ||
) | ||
|
||
for { | ||
c, err := amqp.Dial(rabbitURL) | ||
if err != nil { | ||
fmt.Println("RabbitMQ not yet ready...") | ||
counts++ | ||
} else { | ||
connection = c | ||
fmt.Println() | ||
|
||
break | ||
} | ||
|
||
if counts > RetryTimes { | ||
fmt.Println(err) | ||
|
||
return nil, err | ||
} | ||
|
||
fmt.Printf("Backing off for %d seconds...\n", int(math.Pow(float64(counts), PowOf))) | ||
backOff = time.Duration(math.Pow(float64(counts), PowOf)) * time.Second | ||
time.Sleep(backOff) | ||
|
||
continue | ||
} | ||
|
||
return connection, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
app: | ||
name: 'counter-service' | ||
version: '1.0.0' | ||
|
||
http: | ||
host: '0.0.0.0' | ||
port: 5002 | ||
|
||
logger: | ||
log_level: 'debug' | ||
rollbar_env: 'counter-service' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/ilyakaznacheev/cleanenv" | ||
configs "github.com/thangchung/go-coffeeshop/pkg/config" | ||
) | ||
|
||
type ( | ||
Config struct { | ||
configs.App `yaml:"app"` | ||
configs.HTTP `yaml:"http"` | ||
configs.Log `yaml:"logger"` | ||
} | ||
) | ||
|
||
func NewConfig() (*Config, error) { | ||
cfg := &Config{} | ||
|
||
dir, err := os.Getwd() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// debug | ||
fmt.Println(dir) | ||
|
||
err = cleanenv.ReadConfig(dir+"/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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
|
||
"github.com/golang/glog" | ||
"github.com/thangchung/go-coffeeshop/cmd/counter/config" | ||
"github.com/thangchung/go-coffeeshop/internal/counter/app" | ||
mylogger "github.com/thangchung/go-coffeeshop/pkg/logger" | ||
) | ||
|
||
func main() { | ||
cfg, err := config.NewConfig() | ||
if err != nil { | ||
glog.Fatal(err) | ||
} | ||
|
||
fmt.Println(reflect.TypeOf(struct{}{})) | ||
|
||
mylog := mylogger.New(cfg.Level) | ||
|
||
a := app.New(mylog, cfg) | ||
if err = a.Run(context.Background()); err != nil { | ||
glog.Fatal(err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.