-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (139 loc) · 3.8 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"html/template"
)
var appName = "web"
var accountsURL = "http://accounts:8080/accounts/"
var accountService AccountService
var client *http.Client
// Account represents the format of an account. Helpful, no?
type Account struct {
Username string `json:"username"`
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
Email string `json:"email"`
}
type AccountService struct {
Client http.Client
}
// type AccountsResult struct {
// Accounts []Account `json:"results"`
// Paging Paging `json:"paging"`
// }
type AccountsResult []Account
type Paging struct {
NextPage string `json:"nextPage"`
}
// func showDemoAccount(w http.ResponseWriter, r *http.Request) {
// accountID := "59c3f494427d620038560f57"
// // QueryEscape escapes the account string so
// // it can be safely placed inside a URL query
// safeAccount := url.QueryEscape(accountID)
//
// url := fmt.Sprintf("http://accounts:8080/accounts/%s", safeAccount)
//
// // Build the request
// req, err := http.NewRequest("GET", url, nil)
// if err != nil {
// log.Fatal("NewRequest: ", err)
// return
// }
//
// // For control over HTTP client headers,
// // redirect policy, and other settings,
// // create a Client
// // A Client is an HTTP client
// // client := &http.Client{}
//
// // Send the request via a client
// // Do sends an HTTP request and
// // returns an HTTP response
// resp, err := accountService.getAccount(req)
// if err != nil {
// log.Fatal("Do: ", err)
// return
// }
//
// // Callers should close resp.Body
// // when done reading from it
// // Defer the closing of the body
// defer resp.Body.Close()
//
// // Fill the record with the data from the JSON
// var record Account
//
// // Use json.Decode for reading streams of JSON data
// if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
// log.Println(err)
// }
//
// // username := record.Username
// // firstname := record.FirstName
// // lastname := record.LastName
// // email := record.Email
//
// tmpl := template.Must(template.ParseFiles("accountdetail.html"))
//
// tmpl.Execute(w, record)
//
// }
func accountListHandler(w http.ResponseWriter, r *http.Request) {
accounts, err := accountService.getAccounts()
if err != nil {
log.Fatal(err)
}
t, _ := template.ParseFiles("accountList.html")
t.Execute(w, accounts)
}
func accountHandler(w http.ResponseWriter, r *http.Request) {
accounts, err := accountService.getAccount()
if err != nil {
log.Fatal(err)
}
t, _ := template.ParseFiles("accountdetail.html")
t.Execute(w, accounts)
}
func (svc AccountService) getAccounts() (result AccountsResult, err error) {
resp, err := svc.Client.Get(accountsURL)
if err != nil {
log.Fatal(err)
}
log.Println("Hello")
err = json.NewDecoder(resp.Body).Decode(&result)
// err = json.Unmarshal([]byte(resp.Body), &result)
// if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
// log.Println(err)
// }
return
}
func (svc AccountService) getAccount() (result Account, err error) {
accountID := "59c3f494427d620038560f57"
// QueryEscape escapes the account string so
// it can be safely placed inside a URL query
safeAccount := url.QueryEscape(accountID)
url := fmt.Sprintf("http://accounts:8080/accounts/%s", safeAccount)
resp, err := svc.Client.Get(url)
if err != nil {
log.Fatal(err)
}
err = json.NewDecoder(resp.Body).Decode(&result)
// if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
// log.Println(err)
// }
return
}
func main() {
// accountService = http.Client{}
fmt.Printf("Starting %v\n", appName)
http.HandleFunc("/", accountHandler)
http.HandleFunc("/accounts/", accountListHandler)
err := http.ListenAndServe(":8001", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}