Skip to content

Commit

Permalink
add more code
Browse files Browse the repository at this point in the history
  • Loading branch information
thangchung committed Nov 11, 2022
1 parent 528d89f commit e6bdb70
Show file tree
Hide file tree
Showing 33 changed files with 2,246 additions and 506 deletions.
5 changes: 4 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
// These scripts (and its dependencies) should never be consumed by the actual server directly
// Thus they are flagged to require the "scripts" build tag.
// We only inform gopls and the vscode go compiler here, that it has to set this build tag if it sees such a file.
"go.buildTags": "scripts",
"go.buildTags": "scripts,migrate",
"gopls": {
// Add parameter placeholders when completing a function.
"usePlaceholders": true,
Expand All @@ -58,6 +58,9 @@
// DISABLED, done via
"staticcheck": false
},
"gopls.env": {
"GOFLAGS": "-tags=scripts,migrate"
},
// https://code.visualstudio.com/docs/languages/go#_intellisense
"go.autocompleteUnimportedPackages": true,
// https://github.com/golangci/golangci-lint#editor-integration
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ test_report*
# Go Workspaces (introduced in Go 1.18+)
go.work

**/*/.env
*.env
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
include .env
export

PRODUCT_BINARY_NAME=product.out
PROXY_BINARY_NAME=proxy.out

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ In here, we can find `172.17.0.1`, then use it for all accesses in `docker-from-
> buf generate
```

## Env

Create `.env` at your root with content as

```bash
PG_URL=postgres://postgres:P@ssw0rd@<your devcontainer IP>:5432/postgres
```

## Migrations

Add migration for `counter-api`

```bash
> migrate create -seq -dir cmd/counter/db/migrations -ext sql init_db
```

## Sample

``` mermaid
Expand Down
2 changes: 1 addition & 1 deletion cmd/barista/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ http:
port: 5002

rabbit_mq:
url: amqp://guest:[email protected].168.110:5672/
url: amqp://guest:[email protected].174.138:5672/
exchange: orders-exchange
queue: orders-queue
routing_key: orders-routing-key
Expand Down
28 changes: 0 additions & 28 deletions cmd/barista/db/migrations/20221030134103_init_db.sql

This file was deleted.

112 changes: 0 additions & 112 deletions cmd/barista/event/consumer.go

This file was deleted.

116 changes: 28 additions & 88 deletions cmd/barista/main.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
package main

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/golang/glog"
"github.com/thangchung/go-coffeeshop/cmd/barista/config"
"github.com/thangchung/go-coffeeshop/internal/barista/features/orders/eventhandlers"
baristaRabbitMQ "github.com/thangchung/go-coffeeshop/internal/barista/rabbitmq"
"github.com/thangchung/go-coffeeshop/internal/barista/app"
mylog "github.com/thangchung/go-coffeeshop/pkg/logger"
"github.com/thangchung/go-coffeeshop/pkg/rabbitmq"
)

// const (
// RetryTimes = 5
// BackOffSeconds = 2
// )

func main() {
cfg, err := config.NewConfig()
if err != nil {
Expand All @@ -27,92 +17,42 @@ func main() {

logger := mylog.New(cfg.Level)

amqpConn, err := rabbitmq.NewRabbitMQConn(cfg.RabbitMQ.URL, logger)
if err != nil {
logger.Fatal("app - Run - rabbitmq.NewRabbitMQConn: %s", err.Error())
}
defer amqpConn.Close()

handler := eventhandlers.NewBaristaOrderedEventHandler()
consumer, err := baristaRabbitMQ.NewOrderConsumer(amqpConn, handler, logger)

if err != nil {
logger.Fatal("app - Run - baristaRabbitMQ.NewOrderConsumer: %s", err.Error())
}

ctx, cancel := context.WithCancel(context.Background())

go func() {
err := consumer.StartConsumer(cfg.RabbitMQ.WorkerPoolSize, cfg.RabbitMQ.Exchange, cfg.RabbitMQ.Queue, cfg.RabbitMQ.RoutingKey, cfg.RabbitMQ.ConsumerTag)
if err != nil {
logger.Error("StartConsumer: %v", err)
cancel()
}
}()

quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)

select {
case v := <-quit:
logger.Error("signal.Notify: %v", v)
case done := <-ctx.Done():
logger.Error("ctx.Done: %v", done)
a := app.New(logger, cfg)
if err = a.Run(); err != nil {
glog.Fatal(err)
os.Exit(1)
}

// rabbitConn, err := connect(cfg)
// amqpConn, err := rabbitmq.NewRabbitMQConn(cfg.RabbitMQ.URL, logger)
// if err != nil {
// log.Println(err)
// os.Exit(1)
// logger.Fatal("app - Run - rabbitmq.NewRabbitMQConn: %s", err.Error())
// }
// defer amqpConn.Close()

// defer rabbitConn.Close()
// handler := eventhandlers.NewBaristaOrderedEventHandler()
// consumer, err := baristaRabbitMQ.NewOrderConsumer(amqpConn, handler, logger)

// 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)
// logger.Fatal("app - Run - baristaRabbitMQ.NewOrderConsumer: %s", err.Error())
// }
}

// func connect(cfg *config.Config) (*amqp.Connection, error) {
// var (
// counts int64
// backOff = 1 * time.Second
// connection *amqp.Connection
// rabbitURL = cfg.RabbitMQ.URL
// )

// for {
// c, err := amqp.Dial(rabbitURL)
// if err != nil {
// fmt.Println("RabbitMQ not yet ready...")
// counts++
// } else {
// connection = c
// fmt.Println()
// ctx, cancel := context.WithCancel(context.Background())

// break
// }
// go func() {
// err := consumer.StartConsumer(cfg.RabbitMQ.WorkerPoolSize, cfg.RabbitMQ.Exchange, cfg.RabbitMQ.Queue, cfg.RabbitMQ.RoutingKey, cfg.RabbitMQ.ConsumerTag)
// if err != nil {
// logger.Error("StartConsumer: %v", err)
// cancel()
// }
// }()

// if counts > RetryTimes {
// fmt.Println(err)
// quit := make(chan os.Signal, 1)
// signal.Notify(quit, os.Interrupt, syscall.SIGTERM)

// return nil, err
// }

// fmt.Printf("Backing off for %d seconds...\n", int(math.Pow(float64(counts), BackOffSeconds)))
// backOff = time.Duration(math.Pow(float64(counts), BackOffSeconds)) * time.Second
// time.Sleep(backOff)

// continue
// }

// return connection, nil
// }
// select {
// case v := <-quit:
// logger.Error("signal.Notify: %v", v)
// case done := <-ctx.Done():
// logger.Error("ctx.Done: %v", done)
// }
}
7 changes: 5 additions & 2 deletions cmd/counter/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ http:

postgres:
pool_max: 2
url: postgres://postgres:P@[email protected].168.110:5432/counterdb_dev?sslmode=disable
url: postgres://postgres:P@[email protected].174.138:5432/postgres?sslmode=disable

rabbit_mq:
url: amqp://guest:[email protected].168.110:5672/
url: amqp://guest:[email protected].174.138:5672/
exchange: orders-exchange
queue: orders-queue
routing_key: orders-routing-key
consumer_tag: orders-consumer

product_client:
url: 0.0.0.0:5001

logger:
log_level: 'debug'
rollbar_env: 'counter-service'
Loading

0 comments on commit e6bdb70

Please sign in to comment.