-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
executable file
·163 lines (161 loc) · 4.4 KB
/
handler.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
163
/*
Package account handles account requests.
*/
package account
import (
"encoding/json"
"github.com/MerinEREN/iiPackages/api"
"github.com/MerinEREN/iiPackages/datastore/account"
"github.com/MerinEREN/iiPackages/datastore/user"
"github.com/MerinEREN/iiPackages/session"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/memcache"
"log"
"net/http"
)
// Handler returns and modifies account entities.
func Handler(s *session.Session) {
k := new(datastore.Key)
var err error
ID := s.R.URL.Path[len("/accounts/"):]
if ID == "" && s.R.Method != "GET" {
log.Printf("Path: %s, Error: no user ID\n", s.R.URL.Path)
http.Error(s.W, "No user ID", http.StatusBadRequest)
return
}
if ID != "" {
k, err = datastore.DecodeKey(ID)
if err != nil {
log.Printf("Page:%s, Error: %v\n", s.R.URL.Path, err)
http.Error(s.W, err.Error(), http.StatusInternalServerError)
return
}
}
switch s.R.Method {
case "PUT":
// Handle PUT requests
case "DELETE":
err = datastore.Delete(s.Ctx, k)
if err != nil {
log.Printf("Path: %s, Error: %v\n", s.R.URL.Path, err)
http.Error(s.W, err.Error(), http.StatusInternalServerError)
return
}
s.W.WriteHeader(http.StatusNoContent)
default:
// Handles GET requests
acc := new(account.Account)
if ID == "" {
item, err := memcache.Get(s.Ctx, "acc")
if err == nil {
err = json.Unmarshal(item.Value, acc)
if err != nil {
log.Printf("Page:%s, Error: %v\n",
s.R.URL.Path, err)
http.Error(s.W, err.Error(),
http.StatusInternalServerError)
return
}
} else {
ku := new(datastore.Key)
item, err = memcache.Get(s.Ctx, "uKey")
if err == nil {
err = json.Unmarshal(item.Value, ku)
if err != nil {
log.Printf("Page:%s, Error: %v\n",
s.R.URL.Path, err)
http.Error(s.W, err.Error(),
http.StatusInternalServerError)
return
}
acc, err = account.Get(s.Ctx, ku.Parent())
if err != nil {
log.Printf("Page:%s, Error: %v\n",
s.R.URL.Path, err)
// ALSO LOG THIS WITH DATASTORE LOG !!!!!!!
http.Error(s.W, err.Error(),
http.StatusInternalServerError)
return
}
} else {
ku, err = user.GetKeyViaEmail(s)
if err == datastore.Done {
// IMPOSIBLE BUT !!!!!!!!!!!!!!!!!!!!!!!!!!
log.Printf("Page:%s, Error: %v\n",
s.R.URL.Path, err)
// ALSO LOG THIS WITH DATASTORE LOG !!!!!!!
http.Error(s.W, err.Error(),
http.StatusNoContent)
return
} else if err != nil {
log.Printf("Page:%s, Error: %v\n",
s.R.URL.Path, err)
// ALSO LOG THIS WITH DATASTORE LOG !!!!!!!
http.Error(s.W, err.Error(),
http.StatusInternalServerError)
return
} else {
acc, err = account.Get(s.Ctx, ku.Parent())
if err != nil {
log.Printf("Page:%s, Error: %v\n",
s.R.URL.Path, err)
// ALSO LOG THIS WITH DATASTORE LOG
http.Error(s.W, err.Error(),
http.StatusInternalServerError)
return
}
}
bs, err := json.Marshal(ku)
if err != nil {
log.Printf("Page:%s, Error: %v\n",
s.R.URL.Path, err)
}
item = &memcache.Item{
Key: "uKey",
Value: bs,
}
err = memcache.Add(s.Ctx, item)
if err != nil {
log.Printf("Page:%s, Error: %v\n",
s.R.URL.Path, err)
}
}
bs, err := json.Marshal(acc)
if err != nil {
log.Printf("Page:%s, Error: %v\n",
s.R.URL.Path, err)
}
item = &memcache.Item{
Key: "acc",
Value: bs,
}
err = memcache.Add(s.Ctx, item)
if err != nil {
log.Printf("Page:%s, Error: %v\n",
s.R.URL.Path, err)
}
}
} else {
acc, err = account.Get(s.Ctx, k)
if err != nil {
log.Printf("Path: %s, Error: %v\n", s.R.URL.Path, err)
http.Error(s.W, err.Error(),
http.StatusInternalServerError)
return
}
}
s.W.Header().Set("Content-Type", "application/json")
api.WriteResponseJSON(s, acc)
}
/* t := &http.Transport{}
t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
c := &http.Client{Transport: t}
res, err := c.Get("file:///etc/passwd")
log.Println(res, err) */
// To respond to request without any data
// w.WriteHeader(StatusOK)
// Always send corresponding header values instead of defaults !!!!
//w.Header().Set("Content-Type", "application/json; charset=utf-8")
// http.NotFound(w, r)
// http.Redirect(w, r, "/MerinEREN", http.StatusFound)
}