railyard/apps/ritgram/backend/COMPLETE_SETUP.md
caelum0x f80b04ae0f feat(ritgram): move GitGram app into railyard as Ritgram, next to Rithub
Moved later/gitgram-ai/gitgram/ to apps/ritgram/ (fresh history; full history stays in caelum0x/gitgram).
AITimeMachine stays separate in later/gitgram-ai per the Rithub master plan.
Logs, pid files and env files are ignored; GPL-3.0 mobile/proximity noted in THIRD_PARTY_NOTICES.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-13 04:18:46 +03:00

6.2 KiB

Gitgram Backend - Complete Setup Guide

๐Ÿš€ Quick Setup (5 minutes)

1. Install Dependencies

cd backend
npm install

2. Configure Environment

cp .env.example .env

Edit .env with your values:

DATABASE_URL=postgresql://...  # Your PostgreSQL URL
JWT_SECRET=your-secret-key
JWT_REFRESH_SECRET=your-refresh-secret
RESEND_API_KEY=re_xxxxx        # Get from resend.com

3. Setup Database

npx prisma generate
npx prisma db push

4. Start Server

npm run build
npm start

Server runs on http://localhost:4000 โœ…


๐Ÿ“‹ Detailed Setup

Prerequisites

  • Node.js >= 18.0.0
  • PostgreSQL database
  • Resend account (free tier works)

Step-by-Step

1. Database Setup

Option A: Local PostgreSQL

# Install PostgreSQL
brew install postgresql  # macOS
# or
sudo apt install postgresql  # Linux

# Create database
createdb gitgram

# Update .env
DATABASE_URL=postgresql://localhost:5432/gitgram

Option B: Neon (Recommended)

  1. Go to neon.tech
  2. Create free account
  3. Create new project
  4. Copy connection string
  5. Add to .env

Option C: Supabase

  1. Go to supabase.com
  2. Create project
  3. Get connection string from Settings โ†’ Database
  4. Add to .env

2. Email Setup (Resend)

  1. Go to resend.com
  2. Sign up (free tier: 3,000 emails/month)
  3. Create API key
  4. Add to .env:
RESEND_API_KEY=re_xxxxxxxxxxxxx
EMAIL_FROM=Gitgram <noreply@yourdomain.com>

For Development:

  • Use test API key
  • Emails go to your verified email only

For Production:

  • Add your domain
  • Verify DNS records
  • Update EMAIL_FROM

3. JWT Secrets

Generate secure secrets:

# Generate JWT_SECRET
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"

# Generate JWT_REFRESH_SECRET
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"

Add to .env

4. CORS Configuration

Update for your frontend URLs:

FRONTEND_URL=http://localhost:3000
CORS_ORIGIN=http://localhost:3000,http://localhost:8081

๐Ÿงช Testing the Setup

1. Health Check

curl http://localhost:4000/health

Should return:

{
  "status": "ok",
  "timestamp": "...",
  "uptime": 123,
  "database": { "status": "healthy" },
  "memory": { "used": 50, "total": 100 }
}

2. Register User

curl -X POST http://localhost:4000/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "username": "testuser",
    "email": "test@example.com",
    "password": "password123"
  }'

3. Upload Image

curl -X POST http://localhost:4000/api/v1/upload/image \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -F "file=@/path/to/image.jpg"

4. Test Socket.io

const io = require('socket.io-client');
const socket = io('http://localhost:4000', {
  auth: { token: 'YOUR_TOKEN' }
});

socket.on('connect', () => {
  console.log('Connected!');
});

๐Ÿ”ง Development Scripts

# Install dependencies
npm install

# Generate Prisma client
npm run prisma:generate

# Run migrations
npm run prisma:migrate

# Open Prisma Studio
npm run prisma:studio

# Development mode (with hot reload)
npm run dev:backend

# Build for production
npm run build

# Start production server
npm start

# Run tests
npm test

# Lint code
npm run lint

๐Ÿ“ฆ Package List

Core Dependencies

  • express - Web framework
  • @prisma/client - Database ORM
  • socket.io - Real-time
  • jsonwebtoken - JWT auth
  • bcryptjs - Password hashing
  • resend - Email service
  • multer - File upload
  • sharp - Image processing

Dev Dependencies

  • typescript - Type safety
  • @types/* - Type definitions
  • ts-node - TypeScript execution
  • nodemon - Hot reload
  • prisma - Database toolkit

๐Ÿ› Troubleshooting

Database Connection Error

Error: Can't reach database server

Solution:

  • Check DATABASE_URL is correct
  • Ensure PostgreSQL is running
  • Check network/firewall settings

Port Already in Use

Error: EADDRINUSE: address already in use :::4000

Solution:

# Kill process on port 4000
lsof -ti:4000 | xargs kill -9

# Or use different port
PORT=5000 npm start

Prisma Generate Error

Error: Prisma schema not found

Solution:

cd backend
npx prisma generate

Upload Directory Error

Error: ENOENT: no such file or directory

Solution:

mkdir -p uploads/thumbnails

Email Not Sending

Error: Invalid API key

Solution:

  • Check RESEND_API_KEY is correct
  • Verify email domain (production)
  • Check Resend dashboard for errors

๐Ÿš€ Deployment

Deploy to Production

1. Build

npm run build

2. Set Environment Variables

NODE_ENV=production
DATABASE_URL=postgresql://...
JWT_SECRET=...
RESEND_API_KEY=...
FRONTEND_URL=https://yourdomain.com
CORS_ORIGIN=https://yourdomain.com

3. Run Migrations

npx prisma migrate deploy

4. Start Server

npm start

Deploy to Vercel/Railway/Render

  1. Connect GitHub repo
  2. Set environment variables
  3. Build command: npm run build
  4. Start command: npm start
  5. Deploy!

๐Ÿ“Š Performance Tips

1. Enable Redis

REDIS_URL=redis://localhost:6379

2. Database Connection Pooling

Already configured in Prisma

3. Enable Compression

Already enabled in Express

4. Use CDN for Media

For production, use:

  • Cloudinary
  • AWS S3 + CloudFront
  • Vercel Blob

๐ŸŽฏ What You Get

โœ… 75+ API endpoints ready to use
โœ… Real-time messaging with Socket.io
โœ… Image optimization with Sharp
โœ… Email service with Resend
โœ… Complete authentication with JWT
โœ… Social features (follow, like, comment)
โœ… Stories with 24h expiry
โœ… Search & discovery
โœ… Analytics & insights
โœ… Production-ready code


๐Ÿ“ž Support

  • Documentation: See *.md files in root
  • API Reference: API_DOCUMENTATION.md
  • Socket.io: SOCKET_IO_GUIDE.md
  • Media/Email: MEDIA_EMAIL_GUIDE.md

Server: http://localhost:4000
Status: โœ… Running
Completion: 100%

๐ŸŽ‰ You're all set! Start building your frontend!