.
This commit is contained in:
1 parent
7b36e335b3
commit
434e5c91b7
9 files changed
+164
-49
No files matched your search
@@ -137,7 +137,11 @@ jobs:
|
||||
|
||||
- name: Restart container
|
||||
env:
|
||||
DATABASE_URL: postgresql://managment_web:hD3H9seTqX1GJMu1mDJBGDYr@185.36.145.12:5432/ManagmentWebSite_Db
|
||||
# Postgres runs on the Docker host. From inside a bridge network,
|
||||
# the public IP often times out (hairpin/NAT). Reach it via the
|
||||
# host gateway instead — requires Postgres listening on 0.0.0.0
|
||||
# (or docker0) and pg_hba allowing the Docker subnet.
|
||||
DATABASE_URL: postgresql://managment_web:hD3H9seTqX1GJMu1mDJBGDYr@host.docker.internal:5432/ManagmentWebSite_Db
|
||||
run: |
|
||||
set -euo pipefail
|
||||
docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
|
||||
@@ -147,6 +151,7 @@ jobs:
|
||||
--name "$CONTAINER_NAME"
|
||||
--restart unless-stopped
|
||||
--network "$NETWORK_NAME"
|
||||
--add-host=host.docker.internal:host-gateway
|
||||
-e NODE_ENV=production
|
||||
-e PORT=3000
|
||||
-e HOSTNAME=0.0.0.0
|
||||
@@ -158,6 +163,22 @@ jobs:
|
||||
sleep 2
|
||||
docker logs "$CONTAINER_NAME" 2>&1 | tail -n 30
|
||||
|
||||
# Fail the job early if the app cannot reach Postgres from the container
|
||||
if ! docker exec "$CONTAINER_NAME" node -e "
|
||||
const { Pool } = require('pg');
|
||||
const pool = new Pool({
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
connectionTimeoutMillis: 5000,
|
||||
});
|
||||
pool.query('SELECT 1')
|
||||
.then(() => { console.log('DB_OK'); return pool.end(); })
|
||||
.catch(async (e) => { console.error('DB_FAIL', e.message); try { await pool.end(); } catch {} process.exit(1); });
|
||||
"; then
|
||||
echo "::error::Container cannot reach Postgres via DATABASE_URL"
|
||||
docker logs "$CONTAINER_NAME" 2>&1 | tail -n 50
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Deployed ${CONTAINER_NAME} on ${NETWORK_NAME}"
|
||||
echo "Point Nginx Proxy Manager: ${PUBLIC_HOST} → http://${CONTAINER_NAME}:3000"
|
||||
|
||||
|
||||
+4
-1
@@ -12,7 +12,10 @@ services:
|
||||
- PORT=3000
|
||||
- HOSTNAME=0.0.0.0
|
||||
- NEXT_PUBLIC_SITE_URL=${NEXT_PUBLIC_SITE_URL:-https://edu.mstaut.ir}
|
||||
- DATABASE_URL=${DATABASE_URL:-postgresql://managment_web:hD3H9seTqX1GJMu1mDJBGDYr@185.36.145.12:5432/ManagmentWebSite_Db}
|
||||
# Prefer host.docker.internal when Postgres is on the same machine as Docker.
|
||||
- DATABASE_URL=${DATABASE_URL:-postgresql://managment_web:hD3H9seTqX1GJMu1mDJBGDYr@host.docker.internal:5432/ManagmentWebSite_Db}
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
# Nginx Proxy Manager: edu.mstaut.ir → http://aut-management-faculty:3000
|
||||
expose:
|
||||
- "3000"
|
||||
|
||||
@@ -37,6 +37,19 @@ DATABASE_URL=postgresql://managment_web:hD3H9seTqX1GJMu1mDJBGDYr@185.36.145.12:5
|
||||
|
||||
فایل محلی: `.env.local` (در gitignore است).
|
||||
|
||||
### Docker (همان سرور Postgres)
|
||||
|
||||
اگر اپ داخل کانتینر bridge و Postgres روی host باشد، از داخل کانتینر به IP عمومی
|
||||
(`185.36.145.12`) اغلب **timeout** میخورید. در `deploy.yml` / `docker-compose.yml`
|
||||
از `host.docker.internal` (+ `--add-host=host.docker.internal:host-gateway`) استفاده میشود.
|
||||
|
||||
پیشنیاز روی host:
|
||||
|
||||
- `listen_addresses` در Postgres شامل اینترفیس docker/host-gateway باشد (مثلاً `*`)
|
||||
- در `pg_hba.conf` شبکهٔ Docker مجاز باشد (مثلاً `172.16.0.0/12`)
|
||||
|
||||
برای تست از داخل کانتینر: `docker exec <web> node scripts/test-db-connection.mjs` با همان `DATABASE_URL`.
|
||||
|
||||
## تست سریع اتصال
|
||||
|
||||
```bash
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 20 KiB |
@@ -18,11 +18,18 @@ export default async function MainLayout({
|
||||
}>) {
|
||||
const t = await getTranslations();
|
||||
const year = new Date().getFullYear();
|
||||
const [courses, programs, banners] = await Promise.all([
|
||||
getCourses(),
|
||||
getPrograms(),
|
||||
getCampaignBanners(),
|
||||
]);
|
||||
let courses: Awaited<ReturnType<typeof getCourses>> = [];
|
||||
let programs: Awaited<ReturnType<typeof getPrograms>> = [];
|
||||
let banners: Awaited<ReturnType<typeof getCampaignBanners>> = [];
|
||||
try {
|
||||
[courses, programs, banners] = await Promise.all([
|
||||
getCourses(),
|
||||
getPrograms(),
|
||||
getCampaignBanners(),
|
||||
]);
|
||||
} catch (err) {
|
||||
console.error("[main/layout] CMS fetch failed:", err);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
+68
-37
@@ -35,49 +35,80 @@ async function getLabels(): Promise<HomeLabels> {
|
||||
}
|
||||
|
||||
async function HeroSection() {
|
||||
const [featured, labels] = await Promise.all([
|
||||
getFeaturedArticle(),
|
||||
getLabels(),
|
||||
]);
|
||||
return <HomeHero featured={featured} eyebrow={labels.ideasEyebrow} />;
|
||||
try {
|
||||
const [featured, labels] = await Promise.all([
|
||||
getFeaturedArticle(),
|
||||
getLabels(),
|
||||
]);
|
||||
return <HomeHero featured={featured} eyebrow={labels.ideasEyebrow} />;
|
||||
} catch (err) {
|
||||
console.error("[home] HeroSection failed:", err);
|
||||
const labels = await getLabels();
|
||||
return <HomeHero featured={null} eyebrow={labels.ideasEyebrow} />;
|
||||
}
|
||||
}
|
||||
|
||||
async function EventsSection() {
|
||||
const [events, labels] = await Promise.all([getEvents(), getLabels()]);
|
||||
return (
|
||||
<HomeEvents
|
||||
events={events}
|
||||
title={labels.eventsTitle}
|
||||
seeAllLabel={labels.seeAllEvents}
|
||||
/>
|
||||
);
|
||||
try {
|
||||
const [events, labels] = await Promise.all([getEvents(), getLabels()]);
|
||||
return (
|
||||
<HomeEvents
|
||||
events={events}
|
||||
title={labels.eventsTitle}
|
||||
seeAllLabel={labels.seeAllEvents}
|
||||
/>
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("[home] EventsSection failed:", err);
|
||||
const labels = await getLabels();
|
||||
return (
|
||||
<HomeEvents
|
||||
events={[]}
|
||||
title={labels.eventsTitle}
|
||||
seeAllLabel={labels.seeAllEvents}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function RestSection() {
|
||||
const [programs, intersections, stats, labels] = await Promise.all([
|
||||
getPrograms(),
|
||||
getTopics(),
|
||||
getSiteStats(),
|
||||
getLabels(),
|
||||
]);
|
||||
const carouselPrograms = programs.filter((p) =>
|
||||
[
|
||||
"management-core",
|
||||
"evening-management",
|
||||
"executive",
|
||||
"leadership-fellows",
|
||||
"business-analytics",
|
||||
"finance",
|
||||
].includes(p.slug),
|
||||
);
|
||||
return (
|
||||
<HomeRest
|
||||
labels={labels}
|
||||
carouselPrograms={carouselPrograms}
|
||||
intersections={intersections}
|
||||
stats={stats}
|
||||
/>
|
||||
);
|
||||
try {
|
||||
const [programs, intersections, stats, labels] = await Promise.all([
|
||||
getPrograms(),
|
||||
getTopics(),
|
||||
getSiteStats(),
|
||||
getLabels(),
|
||||
]);
|
||||
const carouselPrograms = programs.filter((p) =>
|
||||
[
|
||||
"management-core",
|
||||
"evening-management",
|
||||
"executive",
|
||||
"leadership-fellows",
|
||||
"business-analytics",
|
||||
"finance",
|
||||
].includes(p.slug),
|
||||
);
|
||||
return (
|
||||
<HomeRest
|
||||
labels={labels}
|
||||
carouselPrograms={carouselPrograms}
|
||||
intersections={intersections}
|
||||
stats={stats}
|
||||
/>
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("[home] RestSection failed:", err);
|
||||
const labels = await getLabels();
|
||||
return (
|
||||
<HomeRest
|
||||
labels={labels}
|
||||
carouselPrograms={[]}
|
||||
intersections={[]}
|
||||
stats={[]}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default function HomePage() {
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getCourses } from "@/lib/data/courses";
|
||||
import { errorResponse } from "@/lib/api/response";
|
||||
|
||||
export async function GET() {
|
||||
const data = await getCourses();
|
||||
return NextResponse.json({ data });
|
||||
try {
|
||||
const data = await getCourses();
|
||||
return NextResponse.json({ data });
|
||||
} catch (err) {
|
||||
console.error("[api/courses]", err);
|
||||
return errorResponse("Database unavailable", 503);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,13 @@ export default async function ProfessionalLayout({
|
||||
}>) {
|
||||
const t = await getTranslations();
|
||||
const year = new Date().getFullYear();
|
||||
const [courses, programs] = await Promise.all([getCourses(), getPrograms()]);
|
||||
let courses: Awaited<ReturnType<typeof getCourses>> = [];
|
||||
let programs: Awaited<ReturnType<typeof getPrograms>> = [];
|
||||
try {
|
||||
[courses, programs] = await Promise.all([getCourses(), getPrograms()]);
|
||||
} catch (err) {
|
||||
console.error("[professional/layout] CMS fetch failed:", err);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
+30
-2
@@ -5,6 +5,10 @@ const { Pool } = pg;
|
||||
/**
|
||||
* Shared Postgres pool for server-side use.
|
||||
* Connection details: docs/03-database.md
|
||||
*
|
||||
* In Docker on the same host as Postgres, prefer
|
||||
* `host.docker.internal` (see deploy.yml / docker-compose.yml).
|
||||
* Connecting to the public IP from a bridge network often times out.
|
||||
*/
|
||||
export const pool = new Pool({
|
||||
connectionString:
|
||||
@@ -12,12 +16,36 @@ export const pool = new Pool({
|
||||
"postgresql://managment_web:hD3H9seTqX1GJMu1mDJBGDYr@185.36.145.12:5432/ManagmentWebSite_Db",
|
||||
max: 5,
|
||||
idleTimeoutMillis: 30000,
|
||||
connectionTimeoutMillis: 10000,
|
||||
// Fail fast so a dead DB does not pin every request for 10s+
|
||||
connectionTimeoutMillis: 4000,
|
||||
});
|
||||
|
||||
pool.on("error", (err) => {
|
||||
console.error("[db] idle client error:", err.message);
|
||||
});
|
||||
|
||||
export async function query<T extends pg.QueryResultRow = pg.QueryResultRow>(
|
||||
text: string,
|
||||
params?: unknown[],
|
||||
) {
|
||||
return pool.query<T>(text, params);
|
||||
try {
|
||||
return await pool.query<T>(text, params);
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
console.error("[db] query failed:", message, { text: text.slice(0, 120) });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/** Soft-fail helper for public pages — empty result instead of crashing the RSC tree. */
|
||||
export async function queryRows<T extends pg.QueryResultRow = pg.QueryResultRow>(
|
||||
text: string,
|
||||
params?: unknown[],
|
||||
): Promise<T[]> {
|
||||
try {
|
||||
const res = await query<T>(text, params);
|
||||
return res.rows;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user