-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
70 lines (52 loc) · 1.31 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"html/template"
)
var appName = "web"
var accountsURL string
func init() {
accountsURL = "http://accounts:8080/accounts/"
}
func main() {
log.Printf("Starting %v\n", appName)
http.HandleFunc("/", accountHandler)
err := http.ListenAndServe(":8001", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func accountHandler(w http.ResponseWriter, r *http.Request) {
accountID := "599d910660e6e100078f2f4b" // TODO: Replace this with dynamic; take it from argument
safeAccountID := url.QueryEscape(accountID)
url := fmt.Sprintf("%s/%s", accountsURL, safeAccountID)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return
}
defer resp.Body.Close()
var record Account
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
log.Println(err)
}
t, _ := template.ParseFiles("templates/accountDetail.html")
t.Execute(w, record)
}
// 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"`
}