feat: store worldlabs import token in db
This commit is contained in:
parent
078a115cfd
commit
c4aafe9413
@ -8,6 +8,7 @@ import {
|
||||
sanitizeWorldLabsApiKey,
|
||||
sanitizeWorldLabsBaseUrl,
|
||||
} from "@/lib/worldlabs/server";
|
||||
import { getWorldLabsKeyPoolConfig } from "@/lib/worldlabs/key-pool-config";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const maxDuration = 60;
|
||||
@ -105,13 +106,14 @@ function maskKey(value: string) {
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const configuredToken = getConfiguredImportToken();
|
||||
const dbConfig = await getWorldLabsKeyPoolConfig();
|
||||
const configuredToken = dbConfig.importToken || getConfiguredImportToken();
|
||||
if (!configuredToken) {
|
||||
return Response.json(
|
||||
{
|
||||
success: false,
|
||||
error:
|
||||
"服务端未配置 WORLDLABS_KEY_POOL_IMPORT_TOKEN,无法使用专用导入接口",
|
||||
"服务端未配置 World Labs key 池导入令牌,无法使用专用导入接口",
|
||||
},
|
||||
{ status: 503 }
|
||||
);
|
||||
|
||||
46
lib/worldlabs/key-pool-config.ts
Normal file
46
lib/worldlabs/key-pool-config.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import "server-only";
|
||||
|
||||
import { getSystemSetting } from "@/lib/db/queries";
|
||||
|
||||
export const WORLDLABS_KEY_POOL_CONFIG_KEY = "worldlabs_key_pool_config";
|
||||
|
||||
export type WorldLabsKeyPoolConfig = {
|
||||
importToken: string;
|
||||
};
|
||||
|
||||
export const defaultWorldLabsKeyPoolConfig: WorldLabsKeyPoolConfig = {
|
||||
importToken: "",
|
||||
};
|
||||
|
||||
function normalizeImportToken(value: unknown) {
|
||||
return String(value || "")
|
||||
.replace(/[\u2028\u2029]/g, "")
|
||||
.replace(/[\u0000-\u001F\u007F]/g, "")
|
||||
.trim();
|
||||
}
|
||||
|
||||
export function normalizeWorldLabsKeyPoolConfig(
|
||||
value: unknown
|
||||
): WorldLabsKeyPoolConfig {
|
||||
if (!value || typeof value !== "object") {
|
||||
return defaultWorldLabsKeyPoolConfig;
|
||||
}
|
||||
|
||||
const raw = value as Partial<WorldLabsKeyPoolConfig>;
|
||||
return {
|
||||
importToken: normalizeImportToken(raw.importToken),
|
||||
};
|
||||
}
|
||||
|
||||
export async function getWorldLabsKeyPoolConfig(): Promise<WorldLabsKeyPoolConfig> {
|
||||
try {
|
||||
const setting = await getSystemSetting({
|
||||
key: WORLDLABS_KEY_POOL_CONFIG_KEY,
|
||||
});
|
||||
return setting?.value
|
||||
? normalizeWorldLabsKeyPoolConfig(setting.value)
|
||||
: defaultWorldLabsKeyPoolConfig;
|
||||
} catch (_error) {
|
||||
return defaultWorldLabsKeyPoolConfig;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user