-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
390 lines (379 loc) · 10.1 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import (
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"strings"
"time"
)
type SubscriptionType int
const (
Basic SubscriptionType = iota
Premium
)
func (s SubscriptionType) String() string {
return [...]string{
"Basic",
"Premium",
}[s]
}
type User struct {
ID string
Name string
DeactivatedAt *time.Time
ProductSubscriptions map[string][]SubscriptionType
}
var users = []*User{
{
ID: "1",
Name: "Danilo Bandeira",
DeactivatedAt: nil,
ProductSubscriptions: map[string][]SubscriptionType{
"Product1": {
Basic,
},
},
},
{
ID: "2",
Name: "Ana Banana",
DeactivatedAt: nil,
ProductSubscriptions: map[string][]SubscriptionType{
"Product1": {
Premium,
},
"Product2": {
Premium,
},
},
},
}
func (u *User) addSubscription(productName string, s SubscriptionType) error {
subscriptions, ok := u.ProductSubscriptions[productName]
if !ok {
u.ProductSubscriptions[productName] = []SubscriptionType{s}
return nil
}
var hasSub bool
for _, subscriptionName := range subscriptions {
if subscriptionName.String() == s.String() {
hasSub = true
}
}
if !hasSub {
u.ProductSubscriptions[productName] = append(u.ProductSubscriptions[productName], []SubscriptionType{s}...)
}
return nil
}
func (u *User) subscriptionsFor(productName string) []SubscriptionType {
subscriptions, ok := u.ProductSubscriptions[productName]
if !ok {
return make([]SubscriptionType, 0)
}
result := make([]SubscriptionType, len(subscriptions))
copy(result, subscriptions)
return result
}
func hasSubscription(entity User, feature []string, product string) bool {
permissions, ok := entity.ProductSubscriptions[product]
if !ok {
return false
}
for _, p := range permissions {
for _, f := range feature {
if p.String() == f {
return true
}
}
}
return false
}
func main() {
mux := http.NewServeMux()
logFile, err := os.OpenFile("logs.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
panic("not possible to create file 'logs.log'")
}
defer logFile.Close()
multiWriter := io.MultiWriter(logFile, os.Stdout)
log.SetOutput(multiWriter)
mux.HandleFunc("/users/{userId}/", func(w http.ResponseWriter, r *http.Request) {
userId := r.PathValue("userId")
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html")
var user *User
for idx, u := range users {
if u.ID == userId {
user = users[idx]
break
}
}
var products []string
for key, _ := range user.ProductSubscriptions {
products = append(products, key)
}
t, err := template.ParseFiles("user_info.html")
if err != nil {
log.Printf("error occurred when trying to parse the file %v", err)
http.Redirect(w, r, "/page-not-found/", http.StatusFound)
return
}
if err = t.ExecuteTemplate(w, "user_info.html", struct {
User struct {
Id string
Products []string
}
}{
User: struct {
Id string
Products []string
}{
Id: user.ID,
Products: products,
},
}); err != nil {
log.Printf("error when executing template %v", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
}
return
})
mux.HandleFunc("/users/{userId}/products/{productId}/", func(w http.ResponseWriter, r *http.Request) {
userId := r.PathValue("userId")
productName := strings.Title(strings.Replace(r.PathValue("productId"), "-", " ", -1))
if r.Method == http.MethodGet {
w.Header().Set("Content-Type", "text/html")
var user *User
for idx, u := range users {
if u.ID == userId {
user = users[idx]
break
}
}
if user == nil {
http.Error(w, "user not found", http.StatusNotFound)
return
}
t, err := template.ParseFiles("user_detail.html", "user_permissions.html")
if err != nil {
log.Printf("error occurred when trying to parse the file %v\n", err)
http.Redirect(w, r, "/page-not-found/", http.StatusFound)
return
}
var userPermissions []string
permissions, ok := user.ProductSubscriptions[productName]
if !ok {
http.Redirect(w, r, "/page-not-found/", http.StatusFound)
}
for _, p := range permissions {
userPermissions = append(userPermissions, p.String())
}
style, ok := map[string]struct {
Colors struct {
ButtonBg string
Button string
}
}{
"Product1": {
Colors: struct {
ButtonBg string
Button string
}{
ButtonBg: "yellow",
Button: "black",
},
},
"Product2": {
Colors: struct {
ButtonBg string
Button string
}{
ButtonBg: "purple",
Button: "green",
},
},
}[productName]
if !ok {
log.Printf("style not found for product %s\n", productName)
http.Error(w, "product not found", http.StatusNotFound)
return
}
if err = t.ExecuteTemplate(w, "user_detail.html", struct {
Permissions []string
ProductName string
Style struct {
Colors struct {
ButtonBg string
Button string
}
}
}{
Permissions: userPermissions,
ProductName: productName,
Style: style,
}); err != nil {
log.Printf("error when executing template %v", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
}
return
}
if r.Method == http.MethodPatch {
var user *User
for _, u := range users {
if u.ID == userId {
user = u
break
}
}
var dto struct {
Type string `json:"type"`
}
err := json.NewDecoder(r.Body).Decode(&dto)
defer func(body io.ReadCloser) {
if err = body.Close(); err != nil {
log.Printf("error when closing body %v", err)
}
}(r.Body)
if err != nil {
log.Printf("error in body. must me a json %v", err)
http.Error(w, "invalid body", http.StatusBadRequest)
return
}
var newSubscription SubscriptionType
for _, key := range []SubscriptionType{
Premium,
Basic,
} {
if key.String() == dto.Type {
newSubscription = key
}
}
if err = user.addSubscription(productName, newSubscription); err != nil {
log.Printf("error when trying to add subscription %s for product %s to user with id %s\n", newSubscription.String(), productName, userId)
http.Error(w, fmt.Sprintf("not possivel to add subscription for this user: %v", err), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
resp := map[string]interface{}{
"data": map[string]interface{}{
"message": fmt.Sprintf("user's permissions %v", user.ProductSubscriptions[productName]),
},
"kind": "success",
}
if err = json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("error when trying to send response to the client")
}
return
}
})
mux.HandleFunc("/users/{userId}/products/{productId}/videos/", func(w http.ResponseWriter, r *http.Request) {
userId := r.PathValue("userId")
productName := strings.Title(strings.Replace(r.PathValue("productId"), "-", " ", -1))
var videos []struct {
Type string `json:"type"`
Url string `json:"url"`
Thumbnail string `json:"thumbnail"`
Title string `json:"title"`
ProductName string `json:"product_name"`
}
bytes, err := os.ReadFile("videos_" + productName + ".json")
if err != nil {
resp := map[string]interface{}{
"kind": "error",
"error": map[string]interface{}{
"message": err.Error(),
},
}
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Printf("error when trying to parse resp json %v\n", err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
if _, err = w.Write(jsonResp); err != nil {
log.Printf(fmt.Sprintf("%s: not possible to send responde to the client", r.URL))
}
return
}
err = json.Unmarshal(bytes, &videos)
if err != nil {
log.Printf("error occurred when trying to unmarshal json %v\n", err)
http.Error(w, fmt.Sprintf("error when trying to unmarshal json file %v\n", err), http.StatusBadRequest)
return
}
var user *User
for _, u := range users {
if u.ID == userId {
user = u
break
}
}
if !hasSubscription(*user, []string{"Basic", "Premium"}, productName) {
http.Redirect(w, r, "/forbidden/", http.StatusFound)
return
}
var availableVideos []struct {
Type string `json:"type"`
Url string `json:"url"`
Thumbnail string `json:"thumbnail"`
Title string `json:"title"`
ProductName string `json:"product_name"`
}
if !hasSubscription(*user, []string{"Premium"}, productName) {
for _, v := range videos {
if v.Type == "basic" {
availableVideos = append(availableVideos, v)
}
}
} else {
availableVideos = videos
}
t, err := template.ParseFiles("user_videos.html")
if err != nil {
fmt.Errorf("error occurred when trying to parse the file %w", err)
http.Redirect(w, r, "/page-not-found/", http.StatusFound)
return
}
t.ExecuteTemplate(w, "user_videos.html", availableVideos)
return
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Redirect(w, r, "/page-not-found/", http.StatusFound)
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html")
t, err := template.ParseFiles("index.html", "user_table.html")
if err != nil {
fmt.Errorf("error occurred when trying to parse the file %w", err)
http.Redirect(w, r, "/page-not-found/", http.StatusFound)
return
}
t.ExecuteTemplate(w, "index.html", struct {
Users []*User
}{
Users: users,
})
return
})
mux.HandleFunc("/page-not-found/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "<h1>Page not found</h1>")
})
mux.HandleFunc("/forbidden/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "<h1>Forbidden</h1>")
})
fmt.Println("server running at http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", mux))
}
/**
1. [ ] Criar botao no front para adicionar permissao para o user
2. [ ] Aplicar lib de estilização
3. [ ] Testes automatizados?
*/