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 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)) + +}