-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_user.go
160 lines (121 loc) · 2.92 KB
/
http_user.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
/**
HTTP API for Users
*/
package main
import (
"log"
"regexp"
"strings"
"encoding/json"
"net/http"
)
type User_Flag struct {
Stub string `json:"stub"`
Name string `json:"name"`
Data string `json:"data"`
}
type User_Data struct {
User User_Record
Flag_List []User_Flag
}
type User_JSON struct {
id string
Hash string `json:"hash"`
Name string `json:"name"`
}
// Return a list of Users in JSON format
func HTTP_api_user_select(w http.ResponseWriter, r *http.Request) {
log.Println("HTTP_api_users_handler(%s)", r.URL.Path)
//HTTP_auth(r)
res0 := DB_user_list()
user_list := []User_JSON{}
for res0.Next() {
u := User_JSON{}
err := res0.Scan(&u.id, &u.Hash, &u.Name)
if err != nil {
log.Print("Error:", err)
panic("Failed to Scan the Flags")
}
user_list = append(user_list, u)
}
// FOreach THese to JSON
res1, err := json.Marshal(user_list)
if err != nil {
log.Print("Error:", err)
panic("Failed to Marshal user_list")
}
//log.Print("Error:", err)
w.Header().Set("Content-Type", "application/json")
w.Write(res1)
//log.Println(res0)
}
// Return a User
func HTTP_api_user_single(w http.ResponseWriter, r *http.Request) {
log.Println("HTTP_api_user_handler(%s)", r.URL.Path)
if !HTTP_auth(r) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("{ \"status\": \"failure\", \"detail\": \"Not Authorized\" }"))
return
}
// Handle Errors
defer func() {
if r := recover(); r != nil {
// w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("{ \"status\": \"failure\", \"detail\": \"Trap\" }"))
}
}()
path := strings.ToLower(r.URL.Path)
re, _ := regexp.Compile("/user/([0-9a-f]+)(/?([0-9a-f]+)?)")
match := re.FindStringSubmatch(path)
//log.Println(match)
var user string
var flag string
switch len(match) {
case 2:
user = match[1]
flag = ""
case 4:
user = match[1]
flag = match[3]
default:
panic("Everything Sucks")
}
if 0 == len(user) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("{ \"status\": \"failure\", \"detail\": \"No User\" }"))
return
}
// Lookup in Database
userO := DB_user_pick(user)
//log.Println(userO)
retO := &User_Data{
User: userO,
}
if 0 == len(flag) {
resF := DB_user_flag_list(userO.Id)
// Foreach these to JSON
for resF.Next() {
F := User_Flag{}
err := resF.Scan(&F.Stub, &F.Name, &F.Data)
if err != nil {
log.Print("Error:", err)
panic("Failed to Scan the Flags")
}
log.Println(F)
retO.Flag_List = append(retO.Flag_List, F)
}
}
// if (len(flag) > 0) {
// F := User_Flag{}
// F.stub = flag
// F.data = "zyxw"
// resD.Flag_List = append(resD.Flag_List, F)
//}
ret1, _ := json.Marshal(retO)
w.Header().Set("Content-Type", "application/json")
w.Write(ret1)
}