bagsy/apps/api/server.ts
bananabas 88701de549 chore: clean up project structure
- Remove duplicate iOS app folder (ShopList/ → consolidated to Bagsy/)
- Remove build artifacts from git tracking
- Remove stale documentation files
- Remove old React Native mobile code
- Add PROJECT_STATUS.md for clear project overview

iOS app is now clearly organized under apps/ios/Bagsy/ with single Xcode project (Bagsy.xcodeproj)
2026-04-13 17:16:11 +01:00

114 lines
3.3 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Custom Next.js server with WebSocket support and local network discovery
* Run with: node --require ts-node/register server.ts
* Or add to package.json: "dev": "ts-node server.ts"
*/
import { createServer } from 'http';
import { parse } from 'url';
import { networkInterfaces } from 'os';
import next from 'next';
import { initializeWebSocketServer } from './lib/websocket';
const dev = process.env.NODE_ENV !== 'production';
const hostname = process.env.HOSTNAME || 'localhost';
const port = parseInt(process.env.PORT || '3001', 10);
/**
* Get local IP address for the current machine
* Prioritizes IPv4 addresses on the local network
*/
function getLocalIpAddress(): string | null {
const interfaces = networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name] || []) {
// Skip internal and non-IPv4 addresses
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return null;
}
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url || '', true);
await handle(req, res, parsedUrl);
} catch (err) {
console.error('Error handling request:', err);
res.statusCode = 500;
res.end('Internal server error');
}
});
// Initialize WebSocket server
initializeWebSocketServer(server);
server.listen(port, (err?: Error) => {
if (err) throw err;
const localIp = getLocalIpAddress();
const localhostUrl = `http://localhost:${port}`;
console.log('\n' + '='.repeat(60));
console.log('🛍️ Bagsy API Server');
console.log('='.repeat(60));
console.log('\n📍 Local URLs:');
console.log(` • Localhost: ${localhostUrl}`);
if (localIp) {
console.log(` • Local Network: http://${localIp}:${port}`);
console.log(`\n ℹ️ Use the Local Network URL to connect from mobile app on same WiFi`);
}
console.log(`\n🌐 WebSocket: ws://localhost:${port}/api/ws`);
if (localIp) {
console.log(` ws://${localIp}:${port}/api/ws`);
}
console.log('\n' + '='.repeat(60));
console.log('📱 Mobile App Configuration:');
console.log('='.repeat(60));
console.log('\nSet EXPO_PUBLIC_API_URL in apps/mobile/.env to:');
console.log(` EXPO_PUBLIC_API_URL=${localIp ? `http://${localIp}:${port}` : localhostUrl}`);
console.log('\n' + '='.repeat(60));
console.log('🚀 Deployment Guide:');
console.log('='.repeat(60));
console.log(`
Production Deployment:
1. Build the app:
npm run build
2. Deploy to a cloud provider:
- Vercel (recommended for Next.js):
npx vercel deploy
- Railway:
railway link
railway deploy
- Render:
render.com - create new Web Service
- Heroku (legacy):
git push heroku main
3. Set environment variables on your host:
- BETTER_AUTH_SECRET (use: openssl rand -base64 32)
- BETTER_AUTH_URL (e.g., https://api.yourdomain.com)
- DATABASE_URL (PostgreSQL connection string)
- OPENROUTER_API_KEY (for AI suggestions)
4. Update mobile app with production API URL:
EXPO_PUBLIC_API_URL=https://api.yourdomain.com
For more details, see: DEPLOYMENT_GUIDE.md
`);
console.log('='.repeat(60) + '\n');
});
});