// KV 映射表,value 值为当前 workers 绑定的 KV 名称
const KV_MAP = {
'kv1': 'KV_NAME:当前 workers 绑定的 KV 名称'
// 可以添加更多 KV 映射
};
const html = `
KV 管理系统
`;
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
const authCode = request.headers.get('X-Auth-Code');
const kvName = request.headers.get('X-KV-Name');
// 验证授权码的专门接口
if (url.pathname === '/verify-auth') {
if (authCode === env.AUTH_CODE) {
return new Response(JSON.stringify({ message: '授权成功' }), {
headers: { 'Content-Type': 'application/json' }
});
}
return new Response(JSON.stringify({ message: '授权码错误' }), {
status: 401,
headers: { 'Content-Type': 'application/json' }
});
}
// 验证授权码
if (url.pathname !== '/' && authCode !== env.AUTH_CODE) {
return new Response(JSON.stringify({ message: '授权码错误', code: 401 }), {
status: 401,
headers: { 'Content-Type': 'application/json' }
});
}
// 获取实际的 KV 绑定
const actualKVBinding = kvName ? KV_MAP[kvName] : null;
if (url.pathname !== '/' && !actualKVBinding) {
return new Response(JSON.stringify({ message: '无效的 KV 选择' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
// 路由处理
if (url.pathname === '/') {
return new Response(html, {
headers: { 'Content-Type': 'text/html;charset=UTF-8' }
});
}
if (url.pathname === '/kv') {
if (request.method === 'GET') {
// 列出所有 keys
const keys = await env[actualKVBinding].list();
return new Response(JSON.stringify({ keys: keys.keys }), {
headers: { 'Content-Type': 'application/json' }
});
}
if (request.method === 'POST') {
// 添加或更新 KV
const { key, value } = await request.json();
if (!key || value === undefined || value === '') {
return new Response(JSON.stringify({ message: 'Key 和 Value 不能为空' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
try {
// 如果是字符串形式的 JSON,先解析再存储
const valueToStore = typeof value === 'string' ?
JSON.stringify(JSON.parse(value)) :
JSON.stringify(value);
await env[actualKVBinding].put(key, valueToStore);
return new Response(JSON.stringify({ message: '保存成功' }), {
headers: { 'Content-Type': 'application/json' }
});
} catch (e) {
// 如果不是 JSON,直接存储原始值
await env[actualKVBinding].put(key, value);
return new Response(JSON.stringify({ message: '保存成功' }), {
headers: { 'Content-Type': 'application/json' }
});
}
}
}
if (url.pathname.startsWith('/kv/') && request.method === 'GET') {
const key = url.pathname.replace('/kv/', '');
const value = await env[actualKVBinding].get(key);
try {
// 尝试解析存储的值
const parsedValue = JSON.parse(value);
return new Response(JSON.stringify({ value: JSON.stringify(parsedValue) }), {
headers: { 'Content-Type': 'application/json' }
});
} catch (e) {
// 如果不是 JSON,返回原始值
return new Response(JSON.stringify({ value }), {
headers: { 'Content-Type': 'application/json' }
});
}
}
if (url.pathname.startsWith('/kv/') && request.method === 'DELETE') {
const key = url.pathname.replace('/kv/', '');
await env[actualKVBinding].delete(key);
return new Response(JSON.stringify({ message: '删除成功' }), {
headers: { 'Content-Type': 'application/json' }
});
}
return new Response('Not Found', { status: 404 });
},
};