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
5 changes: 5 additions & 0 deletions microsservicos-aula-1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Desafio 01

Foi criado um microserviço de número 4 na linguagem Golang, serviço esse à ser consumido pelo microserviço de número 3.

O microserviço de número 4 processará as requisições enviadas para a porta 9093.
48 changes: 47 additions & 1 deletion microsservicos-aula-1/c/c.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"

"github.com/hashicorp/go-retryablehttp"
)

type Coupon struct {
Expand All @@ -31,6 +35,7 @@ type Result struct {
var coupons Coupons

func main() {

coupon := Coupon{
Code: "abc",
}
Expand All @@ -39,9 +44,11 @@ func main() {

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

}

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

coupon := r.PostFormValue("coupon")
valid := coupons.Check(coupon)

Expand All @@ -52,6 +59,45 @@ func home(w http.ResponseWriter, r *http.Request) {
log.Fatal("Error converting json")
}

resultMicroservice4 := makeHttpCall("http://localhost:9093", "teste desafio microserviço 4")

jsonResultMicroService, errMicroService := json.Marshal(resultMicroservice4)
if errMicroService != nil {
log.Fatal("Error converting json")
}

log.Printf(resultMicroservice4.Status)

fmt.Fprintf(w, string(jsonResultMicroService))
fmt.Fprintf(w, string(jsonResult))

}
}

func makeHttpCall(urlMicroservice string, coupon string) Result {

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

retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 5

res, err := retryClient.PostForm(urlMicroservice, values)
if err != nil {
result := Result{Status: "Servidor fora do ar!"}
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

}
32 changes: 32 additions & 0 deletions microsservicos-aula-1/d/d.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

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

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

type Result struct {
Status string
}

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

log.Printf("Requisição recebida!")

result := Result{Status: "teste microserviço 4"}

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

fmt.Fprintf(w, string(jsonResult))

}