Jon Asbury
12/18/2025, 5:57 PMZe'ev Manilovich
12/18/2025, 6:04 PMJon Asbury
12/19/2025, 7:07 PMpermit support automation2
12/19/2025, 7:07 PMJon Asbury
12/19/2025, 7:34 PMimport express from 'express';
import cors from 'cors';
import { createProxyMiddleware, Options } from 'http-proxy-middleware';
const PROXY_PORT = process.env.PROXY_PORT || 8080; // 8080 hooks into CloudRun so use it
const app = express();
// Enable CORS - Restrict to allowed origins
const allowedOrigins = [
'<http://localhost:3000>',
...
];
app.use(express.json());
// Ignore favicon.ico requests
app.get('/favicon.ico', (req, res) => {
res.status(204).end();
});
// CORS configuration
app.use(cors({
origin: (origin, callback) => {
console.log('origin', origin);
// Allow requests with no origin (like mobile apps or curl requests)
if (!origin) return callback(null, true);
// Check if origin is in allowed list
if (allowedOrigins.includes(origin)) {
console.log('origin is allowed', origin);
callback(null, true);
} else {
console.log('origin is not allowed', origin);
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'Origin', 'X-Requested-With', 'Accept']
}));
// Get environment variables
const PDP_URL = process.env.PDP_URL;
const PDP_API_KEY = process.env.PDP_API_KEY;
// Validate environment variables
if (!PDP_URL) {
throw new Error("PDP_URL environment variable is not set");
} else {
// console.log(`PDP_URL set to ${PDP_URL}.`);
}
if (!PDP_API_KEY) {
throw new Error("PDP_API_KEY environment variable is not set");
} else {
// console.log(`PDP_API_KEY set to ${PDP_API_KEY.substring(0, 10)}.`);
}
// Health check endpoint - must be defined BEFORE proxy middleware
app.all('/test', (req, res) => {
res.json({
status: 'ok',
message: 'Proxy is up and running',
timestamp: new Date().toISOString(),
proxyTarget: PDP_URL ? `${PDP_URL.substring(0, 20)}...` : 'not configured'
});
});
// Configure proxy middleware
const proxyOptions: Options = {
target: PDP_URL,
changeOrigin: true,
onProxyReq: (proxyReq, req, res) => {
// Inject Bearer token into the Authorization header
proxyReq.setHeader('Authorization', `Bearer ${PDP_API_KEY}`);
// Log the proxied request
console.log(`Proxying ${req.method} ${req.url} to ${PDP_URL}${req.url}`);
},
onProxyRes: (proxyRes, req, res) => {
// Return response in unmodified format
// The proxy middleware handles this automatically, but we log it
console.log(`Received ${proxyRes.statusCode} from ${PDP_URL}${req.url}`);
},
onError: (err, req, res) => {
console.error('Proxy error:', err);
if (!res.headersSent) {
res.status(500).json({ error: 'Proxy error', message: err.message });
}
},
};
// Apply proxy middleware to all routes (except /test which is handled above)
app.use('*', createProxyMiddleware(proxyOptions));
app.use(express.json());
// Start server - Cloud Run provides PORT environment variable
const server = app.listen(PROXY_PORT as number, '0.0.0.0', () => {
console.log(`Proxy server listening on port ${PROXY_PORT}`);
console.log(`Forwarding requests to ${PDP_URL}`);
});Natalia Edelson
12/19/2025, 7:36 PMJon Asbury
12/19/2025, 7:37 PM