Skip to content

Commit

Permalink
添加key轮询制
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyfabin committed Oct 18, 2023
1 parent bbbd118 commit 051b8bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 5 additions & 1 deletion app/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ export async function auth(req: NextRequest) {
const apiKey = serverConfig.apiKey;
if (apiKey) {
console.log("[Auth] use system api key");
req.headers.set("Authorization", `Bearer ${apiKey}`);
// 分割key
const keys = apiKey.split(',');
// 随机选择一个key
const randomKey = keys[Math.floor(Math.random() * keys.length)];
req.headers.set("Authorization", `Bearer ${randomKey}`);
} else {
console.log("[Auth] admin did not provide an api key");
}
Expand Down
6 changes: 4 additions & 2 deletions app/api/dist/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function parseApiKey(bearToken) {
function auth(req) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var authToken, _b, accessCode, token, hashedCode, serverConfig, message, date, localTime, apiKey;
var authToken, _b, accessCode, token, hashedCode, serverConfig, message, date, localTime, apiKey, keys, randomKey;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
Expand Down Expand Up @@ -154,7 +154,9 @@ function auth(req) {
apiKey = serverConfig.apiKey;
if (apiKey) {
console.log("[Auth] use system api key");
req.headers.set("Authorization", "Bearer " + apiKey);
keys = apiKey.split(',');
randomKey = keys[Math.floor(Math.random() * keys.length)];
req.headers.set("Authorization", "Bearer " + randomKey);
}
else {
console.log("[Auth] admin did not provide an api key");
Expand Down

0 comments on commit 051b8bb

Please sign in to comment.