57 lines
2.0 KiB
TypeScript
57 lines
2.0 KiB
TypeScript
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 || '';
|
|
if (!settings.default_fee_percent) settings.default_fee_percent = '1.0';
|
|
|
|
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, default_fee_percent } = body;
|
|
|
|
const queries = [
|
|
{ key: 'sol_platform_address', value: sol_platform_address },
|
|
{ key: 'evm_platform_address', value: evm_platform_address },
|
|
{ key: 'default_fee_percent', value: default_fee_percent || '1.0' }
|
|
];
|
|
|
|
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 });
|
|
}
|
|
}
|