From 9864d3f0333b2d3d104c353818e001857c259d28 Mon Sep 17 00:00:00 2001 From: Wira Irawan Date: Mon, 11 May 2026 14:54:25 +0700 Subject: [PATCH] Use internal backend URL for frontend server runtime --- .env.example | 1 + deploy/debian12/README.md | 8 ++++++++ deploy/debian12/app.env.example | 1 + frontend/middleware.ts | 3 +-- frontend/src/app/actions.ts | 2 +- frontend/src/lib/api.ts | 2 +- frontend/src/lib/server-api.ts | 3 +++ 7 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 frontend/src/lib/server-api.ts diff --git a/.env.example b/.env.example index 6721380..f98b9a9 100644 --- a/.env.example +++ b/.env.example @@ -11,6 +11,7 @@ WEBHOOK_VERIFY_TOKEN=replace-with-32-plus-char-random-token WEBHOOK_SHARED_SECRET=replace-with-32-plus-char-random-secret META_WEBHOOK_APP_SECRET= WEBHOOK_ALLOW_UNSIGNED=false +INTERNAL_API_URL=http://127.0.0.1:3001/api NEXT_PUBLIC_API_URL=http://localhost:3000/backend-api MAIL_HOST=mail.example.com MAIL_PORT=465 diff --git a/deploy/debian12/README.md b/deploy/debian12/README.md index 5a09ffa..8f8406b 100644 --- a/deploy/debian12/README.md +++ b/deploy/debian12/README.md @@ -136,6 +136,7 @@ Nilai yang wajib dipakai untuk domain ini: NODE_ENV=production FRONTEND_ORIGIN=https://portal.bizone.id PUBLIC_API_URL=https://portal.bizone.id +INTERNAL_API_URL=http://127.0.0.1:3001/api NEXT_PUBLIC_API_URL=https://portal.bizone.id/api PORT=3001 WEBHOOK_ALLOW_UNSIGNED=false @@ -344,3 +345,10 @@ NEXT_PUBLIC_API_URL=https://portal.bizone.id/backend-api Ini penting agar frontend server-side berbicara ke backend asli, sementara browser tetap bisa memakai route handler Next.js di `/api/*`. +Tambahkan juga: + +```dotenv +INTERNAL_API_URL=http://127.0.0.1:3001/api +``` + +`INTERNAL_API_URL` dipakai oleh server-side frontend, middleware, dan server actions untuk langsung memanggil backend internal tanpa lewat reverse proxy publik. diff --git a/deploy/debian12/app.env.example b/deploy/debian12/app.env.example index 542f403..34182b4 100644 --- a/deploy/debian12/app.env.example +++ b/deploy/debian12/app.env.example @@ -6,6 +6,7 @@ REDIS_URL=redis://127.0.0.1:6379 PORT=3001 FRONTEND_ORIGIN=https://portal.bizone.id PUBLIC_API_URL=https://portal.bizone.id +INTERNAL_API_URL=http://127.0.0.1:3001/api NEXT_PUBLIC_API_URL=https://portal.bizone.id/backend-api JWT_SECRET=UsmlPBa61fKDgTjUR+9sS9f5SKw3OF7X0CjGWoHibg2eF7gQO6sS57pc2Hj8XIv4 diff --git a/frontend/middleware.ts b/frontend/middleware.ts index 0985dbe..9e35ffb 100644 --- a/frontend/middleware.ts +++ b/frontend/middleware.ts @@ -1,6 +1,5 @@ import { NextRequest, NextResponse } from 'next/server'; - -const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001/api'; +import { SERVER_API_URL as API_URL } from './src/lib/server-api'; const AUTH_COOKIE = 'wa_session'; const REFRESH_COOKIE = 'wa_refresh'; const SESSION_REFRESH_LEEWAY_SECONDS = 60; diff --git a/frontend/src/app/actions.ts b/frontend/src/app/actions.ts index 2172fb9..11e8422 100644 --- a/frontend/src/app/actions.ts +++ b/frontend/src/app/actions.ts @@ -10,8 +10,8 @@ import { twoFactorEmailCookieName, } from '../lib/auth'; import { defaultLocale, isLocale, localeCookieName } from '../lib/i18n'; +import { SERVER_API_URL as API_URL } from '../lib/server-api'; -const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001/api'; const secureCookies = process.env.NODE_ENV === 'production'; type FormState = { diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index 5555ff0..2583425 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -1,4 +1,4 @@ -const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001/api'; +import { SERVER_API_URL as API_URL } from './server-api'; async function readJson(response: Response): Promise { const payload = await response.json(); diff --git a/frontend/src/lib/server-api.ts b/frontend/src/lib/server-api.ts new file mode 100644 index 0000000..47af0fc --- /dev/null +++ b/frontend/src/lib/server-api.ts @@ -0,0 +1,3 @@ +export const SERVER_API_URL = + process.env.INTERNAL_API_URL?.trim() || + 'http://127.0.0.1:3001/api';