From 9306447c39fc5e0682ebe7b62a9f42744e292efb Mon Sep 17 00:00:00 2001 From: Edislei Reis Date: Sat, 7 Nov 2020 11:42:01 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Desafio=2001=20-=20Incluindo=20chamada=20do?= =?UTF-8?q?=20microservi=C3=A7o=203=20para=20o=204.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- microsservicos-aula-1/c/c.go | 48 +++++++++++++++++++++++++++++++++++- microsservicos-aula-1/d/d.go | 32 ++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 microsservicos-aula-1/d/d.go diff --git a/microsservicos-aula-1/c/c.go b/microsservicos-aula-1/c/c.go index 68690b1..1a4e482 100644 --- a/microsservicos-aula-1/c/c.go +++ b/microsservicos-aula-1/c/c.go @@ -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 { @@ -31,6 +35,7 @@ type Result struct { var coupons Coupons func main() { + coupon := Coupon{ Code: "abc", } @@ -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) @@ -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)) -} \ No newline at end of file +} + +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 + +} diff --git a/microsservicos-aula-1/d/d.go b/microsservicos-aula-1/d/d.go new file mode 100644 index 0000000..564d3b7 --- /dev/null +++ b/microsservicos-aula-1/d/d.go @@ -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)) + +} From d2d556092b194d5bf0ccf1776603f34cd0908c38 Mon Sep 17 00:00:00 2001 From: Edislei Reis Date: Sat, 7 Nov 2020 12:04:04 -0300 Subject: [PATCH 2/2] =?UTF-8?q?Inclus=C3=A3o=20do=20README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- microsservicos-aula-1/README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 microsservicos-aula-1/README.md diff --git a/microsservicos-aula-1/README.md b/microsservicos-aula-1/README.md new file mode 100644 index 0000000..195ebda --- /dev/null +++ b/microsservicos-aula-1/README.md @@ -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. \ No newline at end of file