Simplify to just use environment variables
- Remove unnecessary Lambda function and API route - Remove backend deployment from amplify.yml - Just use process.env.YOUTUBE_API_KEY directly - Keep it simple - no need for complex secret management 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
2524e0cb27
commit
33d4a1119a
8 changed files with 3517 additions and 4521 deletions
|
@ -1,10 +1,4 @@
|
||||||
version: 1
|
version: 1
|
||||||
backend:
|
|
||||||
phases:
|
|
||||||
build:
|
|
||||||
commands:
|
|
||||||
- npm ci
|
|
||||||
- npx ampx pipeline-deploy --branch $AWS_BRANCH --app-id $AWS_APP_ID
|
|
||||||
frontend:
|
frontend:
|
||||||
phases:
|
phases:
|
||||||
preBuild:
|
preBuild:
|
||||||
|
@ -12,9 +6,6 @@ frontend:
|
||||||
- npm ci
|
- npm ci
|
||||||
build:
|
build:
|
||||||
commands:
|
commands:
|
||||||
- echo "Build started on `date`"
|
|
||||||
- echo "Available environment variables:"
|
|
||||||
- printenv | grep -E "(YOUTUBE|AMPLIFY)" || echo "No relevant env vars found"
|
|
||||||
- npm run build
|
- npm run build
|
||||||
artifacts:
|
artifacts:
|
||||||
baseDirectory: .next
|
baseDirectory: .next
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { defineBackend } from '@aws-amplify/backend';
|
import { defineBackend } from '@aws-amplify/backend';
|
||||||
import { auth } from './auth/resource';
|
import { auth } from './auth/resource';
|
||||||
import { data } from './data/resource';
|
import { data } from './data/resource';
|
||||||
import { youtubeApi } from './functions/youtube-api/resource';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
|
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
|
||||||
|
@ -9,5 +8,4 @@ import { youtubeApi } from './functions/youtube-api/resource';
|
||||||
defineBackend({
|
defineBackend({
|
||||||
auth,
|
auth,
|
||||||
data,
|
data,
|
||||||
youtubeApi,
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
import type { APIGatewayProxyHandler } from 'aws-lambda';
|
|
||||||
|
|
||||||
export const handler: APIGatewayProxyHandler = async (event) => {
|
|
||||||
const apiKey = process.env.YOUTUBE_API_KEY;
|
|
||||||
|
|
||||||
if (!apiKey) {
|
|
||||||
return {
|
|
||||||
statusCode: 500,
|
|
||||||
body: JSON.stringify({ error: 'YouTube API key not configured' }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const videoIds = JSON.parse(event.body || '{}').videoIds || [];
|
|
||||||
|
|
||||||
if (!videoIds.length) {
|
|
||||||
return {
|
|
||||||
statusCode: 400,
|
|
||||||
body: JSON.stringify({ error: 'Video IDs are required' }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const url = `https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${videoIds.join(',')}&key=${apiKey}`;
|
|
||||||
const response = await fetch(url);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`YouTube API error: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
return {
|
|
||||||
statusCode: 200,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
return {
|
|
||||||
statusCode: 500,
|
|
||||||
body: JSON.stringify({ error: 'Failed to fetch YouTube data' }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -1,8 +0,0 @@
|
||||||
import { defineFunction, secret } from '@aws-amplify/backend';
|
|
||||||
|
|
||||||
export const youtubeApi = defineFunction({
|
|
||||||
name: 'youtube-api',
|
|
||||||
environment: {
|
|
||||||
YOUTUBE_API_KEY: secret('YOUTUBE_API_KEY'),
|
|
||||||
},
|
|
||||||
});
|
|
7906
package-lock.json
generated
7906
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -1,35 +0,0 @@
|
||||||
import { NextRequest, NextResponse } from 'next/server';
|
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
|
||||||
try {
|
|
||||||
const body = await request.json();
|
|
||||||
const { videoIds } = body;
|
|
||||||
|
|
||||||
if (!videoIds || !Array.isArray(videoIds)) {
|
|
||||||
return NextResponse.json({ error: 'Video IDs are required' }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
// In a real implementation, you'd get the Amplify function URL from amplify_outputs.json
|
|
||||||
// For now, let's fall back to direct API call with environment variable
|
|
||||||
const apiKey = process.env.YOUTUBE_API_KEY;
|
|
||||||
|
|
||||||
if (!apiKey) {
|
|
||||||
return NextResponse.json({ error: 'YouTube API key not configured' }, { status: 500 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = `https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${videoIds.join(',')}&key=${apiKey}`;
|
|
||||||
|
|
||||||
const response = await fetch(url);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`YouTube API error: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
return NextResponse.json(data);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('YouTube API error:', error);
|
|
||||||
return NextResponse.json({ error: 'Failed to fetch YouTube data' }, { status: 500 });
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -33,14 +33,10 @@ const fallbackData: Video[] = [
|
||||||
];
|
];
|
||||||
|
|
||||||
async function getYouTubeVideos(ids: string[]): Promise<Video[]> {
|
async function getYouTubeVideos(ids: string[]): Promise<Video[]> {
|
||||||
// Try multiple ways to get the API key
|
const apiKey = process.env.YOUTUBE_API_KEY;
|
||||||
const apiKey = process.env.YOUTUBE_API_KEY ||
|
|
||||||
process.env.NEXT_PUBLIC_YOUTUBE_API_KEY ||
|
|
||||||
process.env.AMPLIFY_YOUTUBE_API_KEY || '';
|
|
||||||
|
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
console.warn("YOUTUBE_API_KEY environment variable not set. Using hardcoded video titles as fallback.");
|
console.warn("YOUTUBE_API_KEY environment variable not set. Using hardcoded video titles as fallback.");
|
||||||
console.log("Available env vars:", Object.keys(process.env).filter(k => k.includes('YOUTUBE') || k.includes('AMPLIFY')).join(', ') || 'none');
|
|
||||||
return fallbackData;
|
return fallbackData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,23 +32,25 @@ const fallbackData: Video[] = [
|
||||||
];
|
];
|
||||||
|
|
||||||
async function getYouTubeVideos(ids: string[]): Promise<Video[]> {
|
async function getYouTubeVideos(ids: string[]): Promise<Video[]> {
|
||||||
try {
|
const apiKey = process.env.YOUTUBE_API_KEY;
|
||||||
// Use the Amplify function endpoint
|
|
||||||
const response = await fetch('/api/youtube', {
|
if (!apiKey) {
|
||||||
method: 'POST',
|
console.warn("YOUTUBE_API_KEY environment variable not set. Using hardcoded video titles as fallback.");
|
||||||
headers: {
|
return fallbackData;
|
||||||
'Content-Type': 'application/json',
|
}
|
||||||
},
|
|
||||||
body: JSON.stringify({ videoIds: ids }),
|
|
||||||
next: { revalidate: 3600 }
|
|
||||||
});
|
|
||||||
|
|
||||||
|
const url = `https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${ids.join(',')}&key=${apiKey}`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(url, { next: { revalidate: 3600 } }); // Revalidate every hour
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`API error: ${response.status}`);
|
const errorData = await response.json();
|
||||||
|
console.error("YouTube API Error:", errorData.error.message);
|
||||||
|
console.log("Falling back to hardcoded video data.");
|
||||||
|
return fallbackData;
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
if (!data.items || data.items.length === 0) {
|
if (!data.items || data.items.length === 0) {
|
||||||
console.warn("YouTube API returned no items for the given video IDs. Falling back to hardcoded data.");
|
console.warn("YouTube API returned no items for the given video IDs. Falling back to hardcoded data.");
|
||||||
return fallbackData;
|
return fallbackData;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue