-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrestapiItem.go
executable file
·186 lines (178 loc) · 4.66 KB
/
restapiItem.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
package main
import (
"context"
"encoding/json"
"errors"
"net"
"net/http"
"strconv"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func postHandleAPIItem(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
}
// token 체크
_, _, err = TokenHandlerV2(r, client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
i := Item{}
var unmarshalErr *json.UnmarshalTypeError
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
err = decoder.Decode(&i)
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 = i.CheckError()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = addItemV2(client, i)
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)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
// handleAPI2RenderSize 함수는 아이템에 RenderSize를 설정한다.
func handleAPI2SetRenderSize(w http.ResponseWriter, r *http.Request) {
type Recipe struct {
ID string `json:"id"`
Size string `json:"size"`
UserID string `json:"userid"`
}
rcp := Recipe{}
client, err := initMongoClient()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(context.Background())
rcp.UserID, _, err = TokenHandlerV2(r, client)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
_, _, err = net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
r.ParseForm()
id := r.FormValue("id")
if id == "" {
http.Error(w, "need id", http.StatusBadRequest)
return
}
rcp.ID = id
size := r.FormValue("size")
if id == "" {
http.Error(w, "need size", http.StatusBadRequest)
return
}
if !regexpImageSize.MatchString(size) {
http.Error(w, "Please enter in the format of 2048x1152", http.StatusBadRequest)
return
}
rcp.Size = size
err = SetImageSizeV2(client, rcp.ID, "rendersize", rcp.Size)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// json 으로 결과 전송
data, err := json.Marshal(rcp)
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)
}
// handleAPISetOverscanRatio 함수는 아이템의 OverscanRatio 값을 설정한다.
func handleAPISetOverscanRatio(w http.ResponseWriter, r *http.Request) {
type Recipe struct {
ID string `json:"id"`
OverscanRatio float64 `json:"overscanratio"`
UserID string `json:"userid"`
}
rcp := Recipe{}
client, err := initMongoClient()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(context.Background())
rcp.UserID, _, err = TokenHandlerV2(r, client)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
_, _, err = net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
r.ParseForm()
id := r.FormValue("id")
if id == "" {
http.Error(w, "need id", http.StatusBadRequest)
return
}
rcp.ID = id
ratio, err := strconv.ParseFloat(r.FormValue("ratio"), 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rcp.OverscanRatio = ratio
err = SetOverscanRatioV2(client, rcp.ID, rcp.OverscanRatio)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// json 으로 결과 전송
data, err := json.Marshal(rcp)
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)
}