Initial commit

This commit is contained in:
decolua
2026-01-05 09:58:59 +07:00
commit 3857598de4
159 changed files with 14537 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
import { NextResponse } from "next/server";
import { deleteApiKey, isCloudEnabled } from "@/lib/localDb";
import { getConsistentMachineId } from "@/shared/utils/machineId";
import { syncToCloud } from "@/app/api/sync/cloud/route";
// DELETE /api/keys/[id] - Delete API key
export async function DELETE(request, { params }) {
try {
const { id } = await params;
const deleted = await deleteApiKey(id);
if (!deleted) {
return NextResponse.json({ error: "Key not found" }, { status: 404 });
}
// Auto sync to Cloud if enabled
await syncKeysToCloudIfEnabled();
return NextResponse.json({ message: "Key deleted successfully" });
} catch (error) {
console.log("Error deleting key:", error);
return NextResponse.json({ error: "Failed to delete key" }, { status: 500 });
}
}
/**
* Sync API keys to Cloud if enabled
*/
async function syncKeysToCloudIfEnabled() {
try {
const cloudEnabled = await isCloudEnabled();
if (!cloudEnabled) return;
const machineId = await getConsistentMachineId();
await syncToCloud(machineId);
} catch (error) {
console.log("Error syncing keys to cloud:", error);
}
}

59
src/app/api/keys/route.js Normal file
View File

@@ -0,0 +1,59 @@
import { NextResponse } from "next/server";
import { getApiKeys, createApiKey, isCloudEnabled } from "@/lib/localDb";
import { getConsistentMachineId } from "@/shared/utils/machineId";
import { syncToCloud } from "@/app/api/sync/cloud/route";
// GET /api/keys - List API keys
export async function GET() {
try {
const keys = await getApiKeys();
return NextResponse.json({ keys });
} catch (error) {
console.log("Error fetching keys:", error);
return NextResponse.json({ error: "Failed to fetch keys" }, { status: 500 });
}
}
// POST /api/keys - Create new API key
export async function POST(request) {
try {
const body = await request.json();
const { name } = body;
if (!name) {
return NextResponse.json({ error: "Name is required" }, { status: 400 });
}
// Always get machineId from server
const machineId = await getConsistentMachineId();
const apiKey = await createApiKey(name, machineId);
// Auto sync to Cloud if enabled
await syncKeysToCloudIfEnabled();
return NextResponse.json({
key: apiKey.key,
name: apiKey.name,
id: apiKey.id,
machineId: apiKey.machineId,
}, { status: 201 });
} catch (error) {
console.log("Error creating key:", error);
return NextResponse.json({ error: "Failed to create key" }, { status: 500 });
}
}
/**
* Sync API keys to Cloud if enabled
*/
async function syncKeysToCloudIfEnabled() {
try {
const cloudEnabled = await isCloudEnabled();
if (!cloudEnabled) return;
const machineId = await getConsistentMachineId();
await syncToCloud(machineId);
} catch (error) {
console.log("Error syncing keys to cloud:", error);
}
}