Feature: Implemented Dynamic Admin Settings for Platform Addresses and Security Guidelines for Private Keys
This commit is contained in:
54
app/api/admin/settings/route.ts
Normal file
54
app/api/admin/settings/route.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// Ensure table exists (Safe initialization)
|
||||
await db.query(`
|
||||
CREATE TABLE IF NOT EXISTS system_settings (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL,
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||
)
|
||||
`);
|
||||
|
||||
const result = await db.query('SELECT * FROM system_settings');
|
||||
|
||||
// Convert to key-value object
|
||||
const settings: Record<string, string> = {};
|
||||
result.rows.forEach(row => {
|
||||
settings[row.key] = row.value;
|
||||
});
|
||||
|
||||
// Fill defaults if empty
|
||||
if (!settings.sol_platform_address) settings.sol_platform_address = process.env.SOL_PLATFORM_ADDRESS || '';
|
||||
if (!settings.evm_platform_address) settings.evm_platform_address = process.env.EVM_PLATFORM_ADDRESS || '';
|
||||
|
||||
return NextResponse.json(settings);
|
||||
} catch (err: any) {
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const body = await req.json();
|
||||
const { sol_platform_address, evm_platform_address } = body;
|
||||
|
||||
const queries = [
|
||||
{ key: 'sol_platform_address', value: sol_platform_address },
|
||||
{ key: 'evm_platform_address', value: evm_platform_address }
|
||||
];
|
||||
|
||||
for (const q of queries) {
|
||||
await db.query(
|
||||
'INSERT INTO system_settings (key, value, updated_at) VALUES ($1, $2, NOW()) ON CONFLICT (key) DO UPDATE SET value = $2, updated_at = NOW()',
|
||||
[q.key, q.value]
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (err: any) {
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user