Matt cfa7e88ed2
Some checks failed
Build and Push Docker Images / docker (push) Failing after 2m20s
Remove all build-time variables and secure Wren API
BREAKING CHANGE: All environment variables are now runtime-configurable

Changes:
- Removed ALL build-time NEXT_PUBLIC_* variables from Dockerfile and CI/CD
- Created server-side proxy routes for Wren API (/api/wren/*)
- Refactored wrenClient.ts to use proxy endpoints (reduced from 400+ to 200 lines)
- Updated checkoutClient.ts and emailClient.ts to remove NEXT_PUBLIC_ fallbacks
- Hardcoded metadataBase in layout.tsx (no longer depends on env var)
- Updated .env.local to use runtime-only variables (WREN_API_TOKEN, NocoDB config)

Security improvements:
- Wren API token never exposed to browser
- All secrets stay server-side
- No sensitive data baked into build

Configuration:
- Wren API: Set WREN_API_TOKEN in docker-compose or .env
- NocoDB: Set NOCODB_* variables in docker-compose or .env
- No Gitea secrets/variables needed for build (only registry credentials)

Docker build is now truly environment-agnostic - same image works in
any environment with different runtime configuration.
2025-11-03 11:03:42 +01:00

80 lines
2.1 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
/**
* POST /api/wren/offset-orders
* Proxy endpoint to create offset orders with Wren API
* This keeps the WREN_API_TOKEN secure on the server
*
* Request body:
* {
* tons: number,
* portfolioId: number,
* dryRun?: boolean,
* source?: string,
* note?: string
* }
*/
export async function POST(request: NextRequest) {
try {
const apiToken = process.env.WREN_API_TOKEN;
if (!apiToken) {
console.error('WREN_API_TOKEN is not configured');
return NextResponse.json(
{ error: 'Wren API is not configured' },
{ status: 500 }
);
}
// Parse request body
const body = await request.json();
const { tons, portfolioId, dryRun = false, source, note } = body;
// Validate required fields
if (!tons || !portfolioId) {
return NextResponse.json(
{ error: 'Missing required fields: tons and portfolioId' },
{ status: 400 }
);
}
// Create offset order payload
const orderPayload: any = {
tons,
portfolio_id: portfolioId,
dry_run: dryRun,
};
if (source) orderPayload.source = source;
if (note) orderPayload.note = note;
// Make request to Wren API
const response = await fetch('https://www.wren.co/api/offset_orders', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(orderPayload),
});
if (!response.ok) {
const errorText = await response.text();
console.error('Wren API error:', response.status, errorText);
return NextResponse.json(
{ error: 'Failed to create offset order with Wren' },
{ status: response.status }
);
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Error creating Wren offset order:', error);
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to create offset order' },
{ status: 500 }
);
}
}