feat: 切换为MariaDB数据库存储
This commit is contained in:
@@ -1,17 +1,21 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
const DATA_DIR = path.join(process.cwd(), 'data');
|
||||
const DISTRICTS_FILE = path.join(DATA_DIR, 'districts.json');
|
||||
import pool from '@/lib/db';
|
||||
|
||||
export async function GET() {
|
||||
let connection;
|
||||
try {
|
||||
const data = await fs.readFile(DISTRICTS_FILE, 'utf-8');
|
||||
const districts = JSON.parse(data);
|
||||
connection = await pool.getConnection();
|
||||
|
||||
const [rows] = await connection.query<any[]>('SELECT name FROM districts ORDER BY sort_order, id');
|
||||
|
||||
const districts = rows.map((row: any) => row.name);
|
||||
|
||||
connection.release();
|
||||
return NextResponse.json({ districts });
|
||||
} catch (error) {
|
||||
console.error('Get districts error:', error);
|
||||
return NextResponse.json({ districts: [] });
|
||||
} finally {
|
||||
if (connection) connection.release();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user