Skip to content

Commit 8819b03

Browse files
committed
fix: dockerfile
Signed-off-by: Animesh Pathak <[email protected]>
1 parent b93d6a4 commit 8819b03

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ COPY package.json package-lock.json* ./
99

1010
# Install dependencies
1111
RUN npm install --legacy-peer-deps
12-
RUN npm ci
1312
# Copy the rest of the application files
1413
COPY . .
1514

@@ -22,12 +21,11 @@ FROM builder AS runner
2221
# Set the working directory
2322
WORKDIR /app
2423
COPY --from=builder /app/public ./public
25-
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
2624
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
2725

2826
# Expose the application port
2927
EXPOSE 3100
3028

3129
# Start the application on port 3100
3230
ENV PORT=3100
33-
CMD ["npm", "run", "dev"]
31+
CMD ["npm", "run", "dev"]

src/app/api/generateWallpaper/route.ts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import { NextResponse } from 'next/server';
22
import axios from 'axios';
33

4+
// Define allowed origins
5+
const ALLOWED_ORIGINS = [
6+
'http://localhost:3000',
7+
'https://wallpaper-ai-navy.vercel.app'
8+
];
9+
410
export async function POST(request: Request) {
11+
// Check the origin of the request
12+
const origin = request.headers.get('origin') || '';
13+
const isAllowedOrigin = ALLOWED_ORIGINS.includes(origin);
14+
515
try {
616
const { prompt, n, size } = await request.json();
717

@@ -15,6 +25,7 @@ export async function POST(request: Request) {
1525
n,
1626
size,
1727
};
28+
1829
// Make the POST request to the image generation API
1930
const response = await axios.post(apiUrl, body, {
2031
headers: {
@@ -23,16 +34,53 @@ export async function POST(request: Request) {
2334
}
2435
});
2536

37+
// Prepare the response with CORS headers
38+
const responseHeaders = {
39+
'Access-Control-Allow-Origin': isAllowedOrigin ? origin : '',
40+
'Access-Control-Allow-Methods': 'POST, OPTIONS',
41+
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
42+
};
43+
2644
// Return the image URL from the API response
27-
return NextResponse.json({ images: response.data.data.map((img: any) => img.url) });
45+
return NextResponse.json(
46+
{ images: response.data.data.map((img: any) => img.url) },
47+
{ headers: responseHeaders }
48+
);
2849
} catch (error) {
2950
console.error('Error generating images:', error);
30-
return NextResponse.json({ error: 'Failed to generate images' }, { status: 500 });
51+
52+
// Include CORS headers in error response as well
53+
const responseHeaders = {
54+
'Access-Control-Allow-Origin': isAllowedOrigin ? origin : '',
55+
'Access-Control-Allow-Methods': 'POST, OPTIONS',
56+
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
57+
};
58+
59+
return NextResponse.json(
60+
{ error: 'Failed to generate images' },
61+
{
62+
status: 500,
63+
headers: responseHeaders
64+
}
65+
);
3166
}
3267
}
3368

69+
// Handle OPTIONS requests for CORS preflight
70+
export async function OPTIONS(request: Request) {
71+
const origin = request.headers.get('origin') || '';
72+
const isAllowedOrigin = ALLOWED_ORIGINS.includes(origin);
73+
74+
// Prepare CORS headers for preflight request
75+
const responseHeaders = {
76+
'Access-Control-Allow-Origin': isAllowedOrigin ? origin : '',
77+
'Access-Control-Allow-Methods': 'POST, OPTIONS',
78+
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
79+
'Access-Control-Max-Age': '86400', // Cache preflight response for 24 hours
80+
};
3481

35-
// // Validate environment variables
36-
// const endpoint = process.env.AZURE_OPENAI_ENDPOINT;
37-
// const key = process.env.AZURE_OPENAI_KEY;
38-
// const apiVersion = '2024-02-01';
82+
return new NextResponse(null, {
83+
status: 204,
84+
headers: responseHeaders
85+
});
86+
}

src/components/ImageGenerator.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const ImageGenerator = () => {
3333
setError(null);
3434

3535
try {
36-
const response = await fetch('/api/generateWallpaper', {
36+
const response = await fetch('https://wallpaper-ai-latest.onrender.com/api/generateWallpaper', {
3737
method: 'POST',
3838
headers: {
3939
'Content-Type': 'application/json',

0 commit comments

Comments
 (0)