Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions filas-aula-2/a/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RABBITMQ_DEFAULT_USER=rabbitmq
RABBITMQ_DEFAULT_PASS=rabbitmq
RABBITMQ_DEFAULT_HOST=localhost
RABBITMQ_DEFAULT_PORT=5672
RABBITMQ_DEFAULT_VHOST=/
RABBITMQ_CONSUMER_NAME=app-name
RABBITMQ_CONSUMER_QUEUE_NAME=exemplo
RABBITMQ_NOTIFICATION_EX=amq.direct
RABBITMQ_NOTIFICATION_ROUTING_KEY=jobs
RABBITMQ_DLX=dlx
65 changes: 65 additions & 0 deletions filas-aula-2/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"encoding/json"
"github.com/joho/godotenv"
"github.com/wesleywillians/go-rabbitmq/queue"
"html/template"
"log"
"net/http"
)

type Order struct {
Coupon string
CcNumber string
}

type Result struct {
Status string
}

func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env")
}
}

func main() {
http.HandleFunc("/", home)
http.HandleFunc("/process", process)
http.ListenAndServe(":9090", nil)
}

func home(w http.ResponseWriter, r *http.Request) {
t := template.Must(template.ParseFiles("templates/home.html"))
t.Execute(w, Result{})
}

func process(w http.ResponseWriter, r *http.Request) {

coupon := r.PostFormValue("coupon")
ccNumber := r.PostFormValue("cc-number")

order := Order{
Coupon: coupon,
CcNumber: ccNumber,
}

jsonOrder, err := json.Marshal(order)
if err != nil {
log.Fatal("Error parsing to json")
}

rabbitMQ := queue.NewRabbitMQ()
ch := rabbitMQ.Connect()
defer ch.Close()

err = rabbitMQ.Notify(string(jsonOrder), "application/json", "orders_ex", "")
if err != nil {
log.Fatal("Error sending message to the queue")
}

t := template.Must(template.ParseFiles("templates/process.html"))
t.Execute(w, "")
}
94 changes: 94 additions & 0 deletions filas-aula-2/a/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<title>Checkout example · Bootstrap</title>
<link href="https://getbootstrap.com/docs/4.5/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<link href="https://getbootstrap.com/docs/4.5/examples/checkout/form-validation.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container">
<div class="py-5 text-center">
<h2>Checkout</h2>
</div>

<form action="/process" method="post">
<div class="row">
<div class="col-md-4 order-md-2 mb-4">
<h4 class="d-flex justify-content-between align-items-center mb-3">
<span class="text-muted">Carrinho</span>
<span class="badge badge-secondary badge-pill">1</span>
</h4>
<ul class="list-group mb-3">
<li class="list-group-item d-flex justify-content-between lh-condensed">
<div>
<h6 class="my-0">Tênis Adidas</h6>
</div>
<span class="text-muted">R$ 100,00</span>
</li>

<li class="list-group-item d-flex justify-content-between">
<span>Total</span>
<strong>R$ 100,00</strong>
</li>
</ul>

<div class="input-group">
<input type="text" class="form-control" name="coupon" placeholder="Cupom">
</div>
</div>
<div class="col-md-8 order-md-1">
<h4 class="mb-3">Dados pessoais</h4>
<div class="row">
<div class="col-md-6 mb-3">
<label for="firstName">Nome</label>
<input type="text" class="form-control" id="name" placeholder="" value="">
</div>
<div class="col-md-6 mb-3">
<label for="lastName">Email</label>
<input type="text" class="form-control" id="email" placeholder="" value="">
</div>
</div>

<hr class="mb-4">

<h4 class="mb-3">Pagamento</h4>


<div class="row">
<div class="col-md-6 mb-3">
<label for="cc-name">Nome no cartão</label>
<input type="text" class="form-control" id="cc-name" placeholder="">
</div>
<div class="col-md-6 mb-3">
<label for="cc-number">Número</label>
<input type="text" class="form-control" id="cc-number" name="cc-number" placeholder="">
</div>
</div>
<div class="row">
<div class="col-md-3 mb-3">
<label for="cc-expiration">Expiração</label>
<input type="text" class="form-control" id="cc-expiration" placeholder="">

</div>
<div class="col-md-3 mb-3">
<label for="cc-cvv">CVV</label>
<input type="text" class="form-control" id="cc-cvv" placeholder="">
</div>
</div>
<hr class="mb-4">
<button class="btn btn-primary btn-lg btn-block" type="submit">Comprar</button>
</div>
</div>
</form>

<footer class="my-5 pt-5 text-muted text-center text-small">
<p class="mb-1">&copy; AvançaDev - 2020</p>
</footer>

</div>
</body>
</html>
26 changes: 26 additions & 0 deletions filas-aula-2/a/templates/process.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<title>Checkout example · Bootstrap</title>
<link href="https://getbootstrap.com/docs/4.5/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<link href="https://getbootstrap.com/docs/4.5/examples/checkout/form-validation.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container">
<div class="py-5 text-center">
<h2>Checkout</h2>
</div>

<h4>Seu pedido foi recebido com sucesso! Aguarde um email de confirmação.</h4>

<footer class="my-5 pt-5 text-muted text-center text-small">
<p class="mb-1">&copy; AvançaDev - 2020</p>
</footer>

</div>
</body>
</html>
10 changes: 10 additions & 0 deletions filas-aula-2/b/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RABBITMQ_DEFAULT_USER=rabbitmq
RABBITMQ_DEFAULT_PASS=rabbitmq
RABBITMQ_DEFAULT_HOST=localhost
RABBITMQ_DEFAULT_PORT=5672
RABBITMQ_DEFAULT_VHOST=/
RABBITMQ_CONSUMER_NAME=payment-ms
RABBITMQ_CONSUMER_QUEUE_NAME=orders
RABBITMQ_NOTIFICATION_EX=amq.direct
RABBITMQ_NOTIFICATION_ROUTING_KEY=
RABBITMQ_DLX=dlx
100 changes: 100 additions & 0 deletions filas-aula-2/b/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"encoding/json"
"github.com/joho/godotenv"
uuid "github.com/satori/go.uuid"
"github.com/streadway/amqp"
"github.com/wesleywillians/go-rabbitmq/queue"
"io/ioutil"
"log"
"net/http"
"net/url"
)

type Result struct {
Status string
}

type Order struct {
ID uuid.UUID
Coupon string
CcNumber string
}

func NewOrder() Order {
return Order{ID: uuid.NewV4()}
}

const (
InvalidCoupon = "invalid"
ValidCoupon = "valid"
ConnectionError = "connection error"
)

func init() {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
}

func main() {
messageChannel := make(chan amqp.Delivery)

rabbitMQ := queue.NewRabbitMQ()
ch := rabbitMQ.Connect()
defer ch.Close()

rabbitMQ.Consume(messageChannel)

for msg := range messageChannel {
process(msg)
}
}

func process(msg amqp.Delivery) {

order := NewOrder()
json.Unmarshal(msg.Body, &order)

resultCoupon := makeHttpCall("http://localhost:9092", order.Coupon)

switch resultCoupon.Status {
case InvalidCoupon:
log.Println("Order: ", order.ID, ": invalid coupon!")

case ConnectionError:
msg.Reject(false)
log.Println("Order: ", order.ID, ": could not process!")

case ValidCoupon:
log.Println("Order: ", order.ID, ": Processed")
}
}

func makeHttpCall(urlMicroservice string, coupon string) Result {

values := url.Values{}
values.Add("coupon", coupon)

res, err := http.PostForm(urlMicroservice, values)
if err != nil {
result := Result{Status: ConnectionError}
return result
}

defer res.Body.Close()

data, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal("Error processing result")
}

result := Result{}

json.Unmarshal(data, &result)

return result

}
57 changes: 57 additions & 0 deletions filas-aula-2/c/c.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"encoding/json"
"fmt"
"log"
"net/http"
)

type Coupon struct {
Code string
}

type Coupons struct {
Coupon []Coupon
}

func (c Coupons) Check(code string) string {
for _, item := range c.Coupon {
if code == item.Code {
return "valid"
}
}
return "invalid"
}

type Result struct {
Status string
}

var coupons Coupons

func main() {
coupon := Coupon{
Code: "abc",
}

coupons.Coupon = append(coupons.Coupon, coupon)

http.HandleFunc("/", home)
http.ListenAndServe(":9092", nil)
}

func home(w http.ResponseWriter, r *http.Request) {
coupon := r.PostFormValue("coupon")
valid := coupons.Check(coupon)

result := Result{Status: valid}

jsonResult, err := json.Marshal(result)
if err != nil {
log.Fatal("Error converting json")
}

fmt.Fprintf(w, string(jsonResult))

}
14 changes: 14 additions & 0 deletions filas-aula-2/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3'

services:

rabbit:
image: "rabbitmq:3-management"
environment:
RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
RABBITMQ_DEFAULT_USER: "rabbitmq"
RABBITMQ_DEFAULT_PASS: "rabbitmq"
RABBITMQ_DEFAULT_VHOST: "/"
ports:
- "15672:15672"
- "5672:5672"
11 changes: 11 additions & 0 deletions filas-aula-2/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/codeedu/avancadev-micrservice-1dia

go 1.15

require (
github.com/joho/godotenv v1.3.0
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/satori/go.uuid v1.2.0
github.com/wesleywillians/go-rabbitmq v0.0.0-20201027193450-55e6a80937af
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
)
Loading