31 lines
795 B
TypeScript
31 lines
795 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { Pool } from "pg";
|
|
|
|
// Database connection - uses environment variables
|
|
const pool = new Pool({
|
|
host: process.env.DB_HOST || "127.0.0.1",
|
|
port: parseInt(process.env.DB_PORT || "5432"),
|
|
user: process.env.DB_USER || "myuser",
|
|
password: process.env.DB_PASSWORD || "mypassword",
|
|
database: process.env.DB_DATABASE || "mydb",
|
|
});
|
|
|
|
export async function GET() {
|
|
try {
|
|
const result = await pool.query(`
|
|
SELECT id, title, price_cents, slug
|
|
FROM products
|
|
ORDER BY id
|
|
LIMIT 100
|
|
`);
|
|
|
|
return NextResponse.json(result.rows);
|
|
} catch (error) {
|
|
console.error("Error fetching products:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch products" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|