-
Notifications
You must be signed in to change notification settings - Fork 2
/
msb.go
69 lines (50 loc) · 1.15 KB
/
msb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
type Result struct {
Status string
}
func main() {
http.HandleFunc("/", home)
http.ListenAndServe(":9091", nil)
}
func home(w http.ResponseWriter, r *http.Request) {
coupon := r.PostFormValue("coupon")
ccNumber := r.PostFormValue("ccNumber")
resultCoupon := makeHttpCall("http://localhost:9092", coupon)
result := Result{Status: "declined---"}
if ccNumber == "1" {
result.Status = "approved"
}
if resultCoupon.Status == "invalid" {
result.Status = "invalid coupon"
}
jsonData, err := json.Marshal(result)
if err != nil {
log.Fatal("Error processing json")
}
fmt.Fprintf(w, string(jsonData))
}
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: "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
}