Skip to content

feat: changes env variabels around for deployment #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion program-finder/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const app = express();

// Configure CORS
app.use(cors({
origin: 'http://localhost:3000',
origin: [
'http://localhost:3000',
'https://25q1-team3-sand.vercel.app'
],
credentials: true
}));

Expand Down
4 changes: 2 additions & 2 deletions program-finder/backend/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ router.get('/google/callback', async (req, res) => {
);

// Redirect to frontend with token
res.redirect(`${process.env.FRONTEND_URL || 'http://localhost:3000'}/login?token=${token}`);
res.redirect(`${process.env.NEXT_PUBLIC_FRONTEND_URL || 'http://localhost:3000'}/login?token=${token}`);
} catch (error) {
console.error('Google OAuth error:', error);
res.redirect(`${process.env.FRONTEND_URL || 'http://localhost:3000'}/login?error=auth_failed`);
res.redirect(`${process.env.NEXT_PUBLIC_FRONTEND_URL || 'http://localhost:3000'}/login?error=auth_failed`);
}
});

Expand Down
27 changes: 26 additions & 1 deletion program-finder/backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
import app from './app';
import { initializeDb } from './db';

const PORT = process.env.PORT || 3001;
const app = express();

app.use(cors({
origin: process.env.NEXT_PUBLIC_FRONTEND_URL || 'http://localhost:3000',
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
}));

app.use(express.json());

// Add a simple test endpoint
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', message: 'Backend server is running' });
});

// Auth routes
app.use('/api/auth', authRoutes);

// Program routes
app.use('/api', programRoutes);

// Bookmark routes
app.use('/api/bookmarks', bookmarkRoutes);

const PORT = 3001;

// Initialize the database before starting the server
app.listen(PORT, () => {
Expand Down
2 changes: 1 addition & 1 deletion program-finder/frontend/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ app.use(cors());

// Proxy middleware configuration
const apiProxy = createProxyMiddleware({
target: process.env.BACKEND_URL || 'http://localhost:3001',
target: process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001',
changeOrigin: true,
pathRewrite: {
'^/api': '', // Remove /api prefix when forwarding to backend
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';

const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:3001';
const BACKEND_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001';

export async function GET(
request: NextRequest,
Expand Down
47 changes: 27 additions & 20 deletions program-finder/frontend/src/app/programs/ProgramsContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ProgramData } from '../../interfaces/ProgramData';
import { ProgramCard } from '../../components/ProgramCard';
import { SearchBar } from '../../components/SearchBar';
import PageLayout from '../../components/PageLayout';
import config from '@/config';

interface SearchFilters {
ageGroup: string;
Expand All @@ -31,45 +32,51 @@ export default function ProgramsContent() {

useEffect(() => {
fetchPrograms();
}, [initialCategory]);
}, [searchParams]);

const fetchPrograms = async (zip = '', searchFilters: SearchFilters = filters) => {
const fetchPrograms = async () => {
setLoading(true);
setError(null);
setCurrentPage(1); // Reset to first page when searching
try {
const queryParams = new URLSearchParams();
const zip = searchParams?.get('zip');
const keyword = searchParams?.get('keyword');
const distance = searchParams?.get('distance');

if (zip) queryParams.append('zip', zip);
if (searchFilters.ageGroup) queryParams.append('ageGroup', searchFilters.ageGroup);
if (searchFilters.category) queryParams.append('category', searchFilters.category);
if (searchFilters.distance) queryParams.append('distance', searchFilters.distance);
if (keyword) queryParams.append('keyword', keyword);
if (distance) queryParams.append('distance', distance);

const apiUrl = process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:3001';
const url = apiUrl + `/api/programs${queryParams.toString() ? `?${queryParams}` : ''}`;
const url = `${config.apiBaseUrl}/programs${queryParams.toString() ? `?${queryParams}` : ''}`;
console.log('Fetching programs from:', url);

const res = await fetch(url);
if (!res.ok) {
throw new Error(`Failed to fetch programs: ${res.statusText}`);
throw new Error('Failed to fetch programs');
}

const data = await res.json();
if (!Array.isArray(data)) {
throw new Error('Invalid response format');
}
console.log(`Fetched ${data.length} programs from the API`);
setPrograms(data);
} catch (err) {
console.error('Fetch error:', err);
setError(err instanceof Error ? err.message : 'Unable to fetch programs. Please try again later.');
setPrograms([]);
} catch (error) {
console.error('Error fetching programs:', error);
setError('Failed to load programs. Please try again later.');
} finally {
setLoading(false);
}
};

const search = (zip: string, searchFilters: SearchFilters) => {
fetchPrograms(zip, searchFilters);
setFilters(searchFilters);
// Update URL with new search parameters
const params = new URLSearchParams();
if (zip) params.set('zip', zip);
if (searchFilters.category) params.set('category', searchFilters.category);
if (searchFilters.distance) params.set('distance', searchFilters.distance);

// Update URL without page reload
window.history.pushState({}, '', `?${params.toString()}`);

// Fetch programs with new parameters
fetchPrograms();
};

// Get current programs for pagination
Expand All @@ -93,7 +100,7 @@ export default function ProgramsContent() {

<section className="box">
<h3>Search Programs</h3>
<SearchBar onSearch={search} initialZip="" />
<SearchBar onSearch={search} initialZip={searchParams?.get('zip') || ''} />
</section>

{loading && (
Expand Down
2 changes: 1 addition & 1 deletion program-finder/frontend/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const devConfig: Config = {
const prodConfig: Config = {
// These will be replaced in production by environment variables
googleMapsApiKey: process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY || 'AIzaSyBfMtxd8CK-Zi_noMDZ3nFaxf6BTVo_hWc',
apiBaseUrl: process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001/api',
apiBaseUrl: `${process.env.NEXT_PUBLIC_API_BASE_URL}/api` || 'https://program-finder.fly.dev/api',
};

// Use production config when NODE_ENV is 'production', otherwise use development config
Expand Down
4 changes: 2 additions & 2 deletions program-finder/test-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
async function testBackendConnection() {
try {
console.log('Testing connection to backend server...');
const response = await fetch('http://localhost:3001/api/health');
const response = await fetch((process.env.NEXT_PUBLIC_API_BASE_URL + '/api/health') || 'http://localhost:3001/api/health');

if (!response.ok) {
throw new Error(`Failed with status: ${response.status}`);
Expand All @@ -22,7 +22,7 @@ async function testBackendConnection() {
async function testBookmarksEndpoint() {
try {
console.log('\nTesting connection to bookmarks endpoint...');
const response = await fetch('http://localhost:3001/api/bookmarks', {
const response = await fetch((process.env.NEXT_PUBLIC_FRONTEND_URL + '/api/bookmarks') || 'http://localhost:3001/api/bookmarks', {
headers: {
'Authorization': 'Bearer test-token'
}
Expand Down
2 changes: 1 addition & 1 deletion program-finder/test-nextjs-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
async function testNextJsBookmarksApi() {
try {
console.log('Testing connection to Next.js bookmarks API...');
const response = await fetch('http://localhost:3000/api/bookmarks', {
const response = await fetch((process.env.NEXT_PUBLIC_FRONTEND_URL + '/api/bookmarks') || 'http://localhost:3000/api/bookmarks', {
headers: {
'Authorization': 'Bearer test-token'
}
Expand Down