-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_init.go
119 lines (106 loc) · 3.46 KB
/
handle_init.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
package main
import (
"context"
"net/http"
"time"
"github.com/dustin/go-humanize"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// handleInit는 URL을 통해 query를 할 수 있게 해주는 함수입니다.
func handleInit(w http.ResponseWriter, r *http.Request) {
token, err := GetTokenFromHeader(w, r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
type recipe struct {
RecentlyCreateItems []Item
TopUsingItems []Item
RecentTags []string // 최근 등록된 태그 리스트
Token
Adminsetting Adminsetting
Searchword string
ItemType string
TotalNum int64
AllItemCount string
RecentlyTotalItemNum int64
TopUsingTotalItemNum int64
User User
RootCategoryID string
SubCategoryID string
RootCategories []Category
SubCategories []Category
}
rcp := recipe{}
rcp.Token = token
rcp.ItemType = "" // search sortList all
rcp.TotalNum = 0 // search button totalNum
//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
}
adminsetting, err := GetAdminSetting(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Adminsetting = adminsetting
rcp.User, err = GetUser(client, token.ID) // user 정보 가져옴
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
num, err := GetAllItemsNum(client) // 전체아이템의 개수를 가져옴
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.AllItemCount = humanize.Comma(num) // 숫자를 1000단위마다 comma를 찍음(string형으로 변경)
rcp.RecentlyTotalItemNum = int64(rcp.User.NewsNum)
rcp.TopUsingTotalItemNum = int64(rcp.User.TopNum)
RecentlyTagItems, err := GetRecentlyCreatedItems(client, 20, 1) // 최근생성된 20개의 아이템들을 가져옴
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.RecentTags = ItemsTagsDeduplication(RecentlyTagItems) // 중복 태그를 정리함
RecentlyCreateItems, err := GetRecentlyCreatedItems(client, int64(rcp.User.NewsNum), 1) // 최근생성된 100개의 아이템들을 가져옴
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.RecentlyCreateItems = RecentlyCreateItems
TopUsingItems, err := GetTopUsingItems(client, int64(rcp.User.TopNum), 1) // 사용률이 높은 20개의 아이템들을 가져옴
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.TopUsingItems = TopUsingItems
rcp.RootCategories, err = GetRootCategories(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = TEMPLATES.ExecuteTemplate(w, "initPage", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}