/
handle_index.go
66 lines (57 loc) · 1.49 KB
/
handle_index.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
)
const (
AuthTokenUrl = "http://api.meican.loc/v2.1/accounts/authtoken"
)
func HandleHome(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//display home page
fmt.Println("Cookies: ", r.Cookies())
remember, err := r.Cookie("remember")
if err != nil || remember == nil {
fmt.Println("no valid cookie found, err: ", err)
}
req, err := http.NewRequest("GET", AuthTokenUrl, nil)
req.AddCookie(remember)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Errorf("failed at calling api, error: %s", err)
}
defer resp.Body.Close()
code := resp.StatusCode
if code != http.StatusOK {
fmt.Errorf("failed at calling api, got status code: %d", code)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Errorf("failed at reading responseBody, error: %s", err)
}
var data map[string]interface{}
if err = json.Unmarshal(body, &data); err != nil {
fmt.Errorf("json.Umarshal failed, error: %s", err)
}
fmt.Println(data)
uid, ok := data["userId"]
if !ok {
fmt.Errorf("couldn't get userId from api")
}
userId, err := strconv.ParseInt(uid.(string), 10, 64)
if err != nil {
fmt.Errorf("invalid userId: %s", uid.(string))
}
fmt.Println("Userid is :", userId)
images, err := globalImageStore.FindAll(0)
if err != nil {
panic(err)
}
RenderTemplate(w, r, "index/home", map[string]interface{}{
"Images": images,
})
}