-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
249 lines (236 loc) · 7.62 KB
/
server.js
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
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require("cors");
const clientID = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
const axios = require("axios");
const qs = require("querystring");
const cache = require("memory-cache");
const Redis = require("ioredis");
const port = process.env.PORT;
const redis = new Redis({
port: process.env.REDIS_PORT, // Redis port
host: process.env.REDIS_HOST, // Redis host
username: process.env.REDIS_USERNAME, // needs Redis >= 6
password: process.env.REDIS_PASSWORD,
db: 0, // Defaults to 0
});
app.use(express.json());
app.use(
cors({
origin: [
`http://localhost:3000`,
`https://woowow.vercel.app`,
"https://woo-wow.vercel.app",
],
methods: ["GET", "POST", "DELETE"],
credentialfpwjses: true,
})
);
async function getAccessToken() {
if (!cache.get("token")) {
console.log("token not found");
try {
const response = await axios.post(
"https://oauth.battle.net/token",
qs.stringify({
grant_type: "client_credentials",
}),
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Basic ${Buffer.from(
`${clientID}:${clientSecret}`
).toString("base64")}`,
},
}
);
const { access_token, expires_in } = response.data;
cache.put("token", access_token, expires_in * 1000);
} catch (error) {
console.log("getToken fail");
console.log(error);
}
} else {
console.log("token was already");
}
}
getAccessToken().then(() => {
const accessToken = cache.get("token");
console.log(`✨token = ${accessToken}`);
});
app.get("/", (req, res) => {
res.status(200).json("루트입니다");
return;
});
app.get(`/api/character`, async (req, res) => {
const charactername = req.query.charactername;
const redisCache = JSON.parse(
await redis.get(`character_card_${charactername}`)
);
if (redisCache) {
res.status(200).json(redisCache);
return;
}
await getAccessToken();
const accessToken = cache.get("token");
const encodedName = encodeURIComponent(charactername);
if (!accessToken) {
console.log(`accessToken 을 찾을 수 없습니다.`);
}
console.log(`❌ Redis ${charactername} 캐릭터 캐시없음`);
try {
const characterResponse = await axios.get(
`https://kr.api.blizzard.com/profile/wow/character/makgora/${encodedName}?namespace=profile-classic1x-kr&locale=ko_KR&access_token=${accessToken}`
);
const characterResponseData = characterResponse.data;
await redis.set(
`character_card_${charactername}`,
JSON.stringify(characterResponseData),
"EX",
60 * 1
);
res.status(200).json(characterResponseData);
return;
} catch (error) {
console.log("에러발생");
console.log(error);
res.status(500).send(error);
}
});
app.get(`/api/characterinfo`, async (req, res) => {
const charactername = req.query.charactername;
const redisCache = JSON.parse(
await redis.get(`character_info_${charactername}`)
);
if (redisCache) {
res.status(200).json(redisCache);
return;
}
await getAccessToken();
const accessToken = cache.get("token");
const encodedName = encodeURIComponent(charactername);
if (!accessToken) {
console.log(`accessToken 을 찾을 수 없습니다.`);
}
console.log(`❌ Redis ${charactername} 캐릭터정보 캐시없음`);
try {
let characterResponseSession;
try {
const characterResponse = await axios.get(
`https://kr.api.blizzard.com/profile/wow/character/makgora/${encodedName}?namespace=profile-classic1x-kr&locale=ko_KR&access_token=${accessToken}`
);
characterResponseSession = characterResponse;
} catch (error) {
console.log("기본정보 요청에러");
}
let equimentResponseSession;
try {
const equimentResponse = await axios.get(
`https://kr.api.blizzard.com/profile/wow/character/makgora/${encodedName}/equipment?namespace=profile-classic1x-kr&access_token=${accessToken}&locale=ko_KR`
);
equimentResponseSession = equimentResponse;
} catch (error) {
console.log("착용장비 요청에러");
}
let stasticsResponseSession;
try {
const stasticsResponse = await axios.get(
`https://kr.api.blizzard.com/profile/wow/character/makgora/${encodedName}/statistics?namespace=profile-classic1x-kr&access_token=${accessToken}&locale=ko_KR`
);
stasticsResponseSession = stasticsResponse;
} catch (error) {
console.log("스태스틱스 요청에러");
}
const equimentResponseData = equimentResponseSession.data;
const characterResponseData = characterResponseSession.data;
const equimentItems = equimentResponseData.equipped_items;
const stasticsResponseData = stasticsResponseSession.data;
const equimentItemsAddURL = equimentItems.map(async (item, idx) => {
const response = await axios.get(
`https://kr.api.blizzard.com/data/wow/media/item/${item.media.id}?namespace=static-1.14.4_50753-classic1x-kr&access_token=${accessToken}`
);
const imgURL = response.data.assets[0].value;
item.media.url = imgURL;
return item;
});
const resolveEquimentItemsAddURL = await Promise.all(equimentItemsAddURL);
characterResponseData.equipment.items = resolveEquimentItemsAddURL;
characterResponseData.statistics.data = stasticsResponseData;
await redis.set(
`character_info_${charactername}`,
JSON.stringify(characterResponseData),
"EX",
60 * 4
);
res.status(200).json(characterResponseData);
return;
} catch (error) {
console.log("에러발생");
console.log(error);
res.status(500).send(error);
}
});
app.get("/api/guild", async (req, res) => {
const redisCache = await redis.get(`guild_count`);
if (redisCache) {
res.status(200).json(redisCache);
return;
}
await getAccessToken();
const accessToken = cache.get("token");
if (!accessToken) {
console.log(`accessToken 을 찾을 수 없습니다.`);
res.status(500).send("accessToken is denine");
return;
}
console.log(`❌ Redis 길드인원 캐시없음`);
try {
const response = await axios.get(
`https://kr.api.blizzard.com/data/wow/guild/makgora/%EC%99%81%ED%83%80%EB%B2%84%EC%8A%A4`,
{
params: {
namespace: "profile-classic1x-kr",
locale: "ko_kr",
access_token: accessToken,
},
}
);
if (response.status === 404) {
res.status(500).json("error");
return;
}
await redis.set(`guild_count`, response.data.member_count, "EX", 30);
res.status(200).json(response.data.member_count);
return;
} catch (error) {
console.log(`길드인원 호출실패`);
res.status(500).json(error);
return;
}
});
//
app.post("/api/worldbuff", async (req, res) => {
console.log("월법 개시시도");
const { adminKey, buffData } = req.body;
const redisAdminkey = await redis.get("adminKey");
if (adminKey === redisAdminkey) {
console.log(`키 일치`);
console.log(adminKey, buffData);
redis.set("Redis_worldbuffData", buffData, "EX", 60 * 60 * 10);
res.status(200).json({ message: "월법" });
} else {
console.log(`키가 일치하지 않습니다.`);
res.status(500).send("Inconsistency key");
}
});
app.get("/api/worldbuff", async (req, res) => {
console.log("월드버프 요청됨");
const wouldbuff = await redis.get("Redis_worldbuffData");
res.status(200).json(wouldbuff);
return;
});
app.listen(port, () => {
console.log(`server running on ${port}`);
});