-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_restapi.go
857 lines (817 loc) · 23.6 KB
/
handle_restapi.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
package main
import (
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func handleAPIDeleteItem(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
id := q.Get("id")
if id == "" {
http.Error(w, "URL에 id를 입력해주세요", http.StatusBadRequest)
return
}
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//accesslevel 체크
accesslevel, err := GetAccessLevelFromHeader(r, client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if accesslevel != "admin" {
http.Error(w, "삭제 권한이 없는 계정입니다", http.StatusUnauthorized)
return
}
// 실제 데이터 삭제
err = RmData(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// DB에서 데이터 삭제
err = RmItem(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = RmFavoriteItem(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
data, err := json.Marshal(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
func handleAPIPostItem(w http.ResponseWriter, r *http.Request) {
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//accesslevel 체크
accesslevel, err := GetAccessLevelFromHeader(r, client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if accesslevel != "default" && accesslevel != "manager" && accesslevel != "admin" {
http.Error(w, "등록 권한이 없는 계정입니다", http.StatusUnauthorized)
return
}
// 아이템 생성
i := Item{}
i.ID = primitive.NewObjectID()
// 아이템 정보 Parsing
itemtype := r.FormValue("itemtype")
if itemtype == "" {
http.Error(w, "itemtype을 설정해주세요", http.StatusBadRequest)
return
}
title := r.FormValue("title")
if title == "" {
http.Error(w, "title을 설정해주세요", http.StatusBadRequest)
return
}
author := r.FormValue("author")
if author == "" {
http.Error(w, "author를 설정해주세요", http.StatusBadRequest)
return
}
description := r.FormValue("description")
if description == "" {
http.Error(w, "description을 설정해주세요", http.StatusBadRequest)
return
}
tags := Str2List(r.FormValue("tags"))
if len(tags) == 0 {
http.Error(w, "tags를 설정해주세요", http.StatusBadRequest)
return
}
attributes, err := StringToMap(r.FormValue("attributes"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
i.ItemType = itemtype
i.Title = title
i.Author = author
i.Description = description
i.Tags = tags
i.Attributes = attributes
i.Status = "ready"
i.Logs = append(i.Logs, "아이템이 생성되었습니다.")
// admin setting에서 rootpath를 가져와 경로를 생성한다.
rootpath, err := GetRootPath(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
objIDpath, err := idToPath(i.ID.Hex())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
i.InputThumbnailImgPath = rootpath + objIDpath + "/originalthumbimg/"
i.InputThumbnailClipPath = rootpath + objIDpath + "/originalthumbmov/"
i.OutputThumbnailPngPath = rootpath + objIDpath + "/thumbnail/thumbnail.png"
i.OutputThumbnailMp4Path = rootpath + objIDpath + "/thumbnail/thumbnail.mp4"
i.OutputThumbnailOggPath = rootpath + objIDpath + "/thumbnail/thumbnail.ogg"
i.OutputThumbnailMovPath = rootpath + objIDpath + "/thumbnail/thumbnail.mov"
i.OutputDataPath = rootpath + objIDpath + "/data/"
// 아이템 추가
err = i.CheckError()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = AddItem(client, i)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 아이템에 파일 업데이트
if itemtype == "alembic" {
uploadAlembicFile(w, r, i.ID.Hex())
}
if itemtype == "blender" {
uploadBlenderFile(w, r, i.ID.Hex())
}
if itemtype == "footage" {
uploadFootageFile(w, r, i.ID.Hex())
}
if itemtype == "fusion360" {
uploadFusion360File(w, r, i.ID.Hex())
}
if itemtype == "hdri" {
uploadHDRIFile(w, r, i.ID.Hex())
}
if itemtype == "houdini" {
uploadHoudiniFile(w, r, i.ID.Hex())
}
if itemtype == "hwp" {
uploadHwpFile(w, r, i.ID.Hex())
}
if itemtype == "katana" {
uploadKatanaFile(w, r, i.ID.Hex())
}
if itemtype == "lut" {
uploadLutFile(w, r, i.ID.Hex())
}
if itemtype == "max" {
uploadMaxFile(w, r, i.ID.Hex())
}
if itemtype == "maya" {
uploadMayaFile(w, r, i.ID.Hex())
}
if itemtype == "modo" {
uploadModoFile(w, r, i.ID.Hex())
}
if itemtype == "nuke" {
uploadNukeFile(w, r, i.ID.Hex())
}
if itemtype == "openvdb" {
uploadOpenVDBFile(w, r, i.ID.Hex())
}
if itemtype == "pdf" {
uploadPdfFile(w, r, i.ID.Hex())
}
if itemtype == "ppt" {
uploadPptFile(w, r, i.ID.Hex())
}
if itemtype == "sound" {
uploadSoundFile(w, r, i.ID.Hex())
}
if itemtype == "texture" {
uploadClipFile(w, r, i.ID.Hex())
}
if itemtype == "unreal" {
uploadUnrealFile(w, r, i.ID.Hex())
}
if itemtype == "usd" {
uploadUSDFile(w, r, i.ID.Hex())
}
// Response
item, err := GetItem(client, i.ID.Hex())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(item)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
func handleAPIGetItem(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
id := q.Get("id")
if id == "" {
http.Error(w, "URL에 id를 입력해주세요", http.StatusBadRequest)
return
}
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
i, err := GetItem(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(i)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
func handleAPIPutItem(w http.ResponseWriter, r *http.Request) {
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//accesslevel 체크
accesslevel, err := GetAccessLevelFromHeader(r, client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if accesslevel != "manager" && accesslevel != "admin" {
http.Error(w, "need permission", http.StatusUnauthorized)
return
}
vars := mux.Vars(r)
id := vars["id"]
if id == "" {
http.Error(w, "need id", http.StatusBadRequest)
return
}
item := Item{}
var unmarshalErr *json.UnmarshalTypeError
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
err = decoder.Decode(&item)
if err != nil {
if errors.As(err, &unmarshalErr) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
err = SetItem(client, item)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(item)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
// handleAPISearch 는 아이템을 검색하는 함수입니다.
func handleAPISearch(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Post Only", http.StatusMethodNotAllowed)
return
}
r.ParseForm()
itemtype := r.FormValue("itemtype")
if itemtype == "" {
http.Error(w, "itemtype을 설정해주세요", http.StatusBadRequest)
return
}
searchword := r.FormValue("searchword")
if searchword == "" {
http.Error(w, "searchword를 설정해주세요", http.StatusBadRequest)
return
}
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
item, err := Search(client, itemtype, searchword)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(item)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
func handleAPIAdminSetting(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
admin, err := GetAdminSetting(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(admin)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
return
}
http.Error(w, "Not Supported Method", http.StatusMethodNotAllowed)
}
func handleAPIUsingRate(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
r.ParseForm()
itemtype := r.FormValue("itemtype")
if itemtype == "" {
http.Error(w, "itemtype을 입력해주세요", http.StatusBadRequest)
return
}
id := r.FormValue("id")
if id == "" {
http.Error(w, "id를 입력해주세요", http.StatusBadRequest)
return
}
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
usingrate, err := UpdateUsingRate(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(usingrate)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
return
}
http.Error(w, "Not Supported Method", http.StatusMethodNotAllowed)
}
// handleAPIRecentItem 는 최근생성된 아이템들을 반환하는 함수임니다.
func handleAPIRecentItem(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
r.ParseForm()
recentlypage, err := strconv.ParseInt(r.FormValue("recentlypage"), 10, 64)
if err != nil {
http.Error(w, "recentlypage를 입력해주세요", http.StatusBadRequest)
return
}
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
usingrate, err := GetRecentlyCreatedItems(client, 4, recentlypage) // 해당페이지(recentlypage)의 4개 아이템을 가져온다.
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(usingrate)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
return
}
http.Error(w, "Not Supported Method", http.StatusMethodNotAllowed)
}
// handleAPITopUsingItem 는 많이 사용되는 아이템들을 반환하는 함수임니다.
func handleAPITopUsingItem(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
r.ParseForm()
topusingpage, err := strconv.ParseInt(r.FormValue("usingpage"), 10, 64)
if err != nil {
http.Error(w, "usingpage를 입력해주세요", http.StatusBadRequest)
return
}
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
usingrate, err := GetTopUsingItems(client, 4, topusingpage) // 해당페이지(topusingpage)의 4개 아이템을 가져온다.
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(usingrate)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
return
}
http.Error(w, "Not Supported Method", http.StatusMethodNotAllowed)
}
// handleAPIFavoriteAsset는 FavoriteAssetIds에 아이템 id를 추가하거나 제거하는 함수다.
func handleAPIFavoriteAsset(w http.ResponseWriter, r *http.Request) {
// mongoDB Client 생성
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// accesslevel 체크
accesslevel, err := GetAccessLevelFromHeader(r, client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if accesslevel != "default" && accesslevel != "manager" && accesslevel != "admin" {
http.Error(w, "즐겨찾기 수정 권한이 없습니다", http.StatusUnauthorized)
return
}
if r.Method == http.MethodGet {
// Get : Get FavoriteAssetIDs
// 전송받은 데이터 parsing
q := r.URL.Query()
userid := q.Get("userid")
if userid == "" {
http.Error(w, "URL에 userid를 입력해주세요", http.StatusBadRequest)
return
}
// Delete itemid from FavoriteAssetIds of User
user := User{}
user, err = GetUser(client, userid)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
favoriteAssetIds := user.FavoriteAssetIDs
reponseIds := make(map[string][]string)
reponseIds["favoriteAssetIds"] = favoriteAssetIds
data, err := json.Marshal(reponseIds)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Response
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
return
} else if r.Method == http.MethodPost {
// POST : FavoriteAssetIDs 자료구조에 itemid를 추가
// 전송받은 데이터 parsing
itemid := r.FormValue("itemid")
if itemid == "" {
http.Error(w, "itemid를 설정해주세요", http.StatusBadRequest)
return
}
userid := r.FormValue("userid")
if userid == "" {
http.Error(w, "userid를 설정해주세요", http.StatusBadRequest)
}
// Add itemid to FavoriteAssetIds of User
user := User{}
user, err = GetUser(client, userid)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
for i := 0; i < len(user.FavoriteAssetIDs); i++ {
if itemid == user.FavoriteAssetIDs[i] {
http.Error(w, "즐겨찾기 목록에 이미 존재하는 itemid입니다", http.StatusBadRequest)
return
}
}
user.FavoriteAssetIDs = append(user.FavoriteAssetIDs, itemid)
err = SetUser(client, user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Response
user, err = GetUser(client, userid)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
return
} else if r.Method == http.MethodDelete {
// DELETE : FavoriteAssetsId 자료구조에 itemid를 추가
// 전송받은 데이터 parsing
q := r.URL.Query()
itemid := q.Get("itemid")
if itemid == "" {
http.Error(w, "URL에 itemid를 입력해주세요", http.StatusBadRequest)
return
}
userid := q.Get("userid")
if userid == "" {
http.Error(w, "URL에 userid를 입력해주세요", http.StatusBadRequest)
return
}
// Delete itemid from FavoriteAssetIds of User
user := User{}
user, err = GetUser(client, userid)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
deleteBool := false
for i := 0; i < len(user.FavoriteAssetIDs); i++ {
if itemid == user.FavoriteAssetIDs[i] {
user.FavoriteAssetIDs = append(user.FavoriteAssetIDs[:i], user.FavoriteAssetIDs[i+1:]...)
deleteBool = true
}
}
if !deleteBool {
http.Error(w, "즐겨찾기에 존재하지 않는 itemid입니다", http.StatusBadRequest)
return
}
err = SetUser(client, user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Response
user, err = GetUser(client, userid)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
return
}
}
// handleAPIInitPassword 함수는 rest API를 이용하여 사용자의 비밀번호를 초기화하는 함수이다.
func handleAPIInitPassword(w http.ResponseWriter, r *http.Request) {
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//accesslevel 체크
accesslevel, err := GetAccessLevelFromHeader(r, client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if accesslevel != "admin" {
http.Error(w, "사용자의 패스워드 초기화 권한이 없는 계정입니다", http.StatusUnauthorized)
return
}
adminSetting, err := GetAdminSetting(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
encryptedPW, err := Encrypt(adminSetting.InitPassword)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
u := User{}
var unmarshalErr *json.UnmarshalTypeError
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
err = decoder.Decode(&u)
if err != nil {
if errors.As(err, &unmarshalErr) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
if u.ID == "" {
http.Error(w, "need id", http.StatusBadRequest)
return
}
user, err := GetUser(client, u.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
user.Password = encryptedPW
user.CreateToken()
err = SetUser(client, user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}