Skip to content

Latest commit

Β 

History

History
159 lines (124 loc) Β· 4.48 KB

File metadata and controls

159 lines (124 loc) Β· 4.48 KB

Plotter API

A scalable REST API built with Fastify, TypeScript, MongoDB, and clean architecture for React Native applications with Google authentication.

πŸš€ Features

  • Google Authentication - Secure authentication using Google ID tokens
  • Group Management - Role-based group system (owner, admin, participant)
  • Real-time Notifications - WebSocket support for live updates
  • Auto-generated Documentation - OpenAPI/Swagger documentation from Zod schemas
  • Clean Architecture - Domain-driven design with clear separation of concerns
  • Type Safety - Full TypeScript coverage with Zod validation
  • Scalable - MongoDB with Redis caching and horizontal scaling support

πŸ› οΈ Tech Stack

  • Framework: Fastify with TypeScript
  • Database: MongoDB with Mongoose ODM
  • Caching: Redis
  • Authentication: JWT with Clerk
  • Validation: Zod schemas
  • Documentation: OpenAPI/Swagger
  • Real-time: WebSockets
  • External Services: SendGrid (email), Twilio (SMS), Google Cloud Storage

πŸ“‹ Prerequisites

  • Node.js 18+
  • MongoDB
  • Redis
  • Google Cloud Console project (for OAuth)

πŸš€ Quick Start

  1. Clone and install dependencies

    git clone <repository-url>
    cd plotter-api
    npm install
  2. Set up environment variables

    cp env.example .env
    # Edit .env with your configuration
  3. Required Environment Variables

    JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
    GOOGLE_CLIENT_ID=your-google-client-id.apps.googleusercontent.com
    MONGODB_URI=mongodb://localhost:27017/plotter-api
    REDIS_URL=redis://localhost:6379
  4. Start development server

    npm run dev
  5. View API documentation Open http://localhost:3000/documentation

πŸ“– API Endpoints

Authentication

  • POST /auth/google - Authenticate with Google ID token
  • GET /auth/me - Get current user information

Groups

  • GET /groups - List user's groups
  • POST /groups - Create a new group
  • GET /groups/:id - Get group details
  • PUT /groups/:id - Update group (admin+)
  • DELETE /groups/:id - Delete group (owner only)
  • GET /groups/:id/members - List group members
  • POST /groups/:id/members - Add member (admin+)
  • PUT /groups/:id/members/:userId - Update member role (admin+)
  • DELETE /groups/:id/members/:userId - Remove member (admin+)

Users

  • GET /users/me - Get current user profile
  • PUT /users/me - Update current user profile

Files

  • POST /files/upload - Upload file to Google Cloud Storage
  • GET /files/:id - Get file information

WebSocket

  • GET /ws - WebSocket connection for real-time updates

πŸ—οΈ Architecture

src/
β”œβ”€β”€ app.ts                    # Main Fastify app setup
β”œβ”€β”€ server.ts                 # Server entry point
β”œβ”€β”€ plugins/                  # Auto-loaded plugins
β”‚   β”œβ”€β”€ auth.ts              # Authentication & authorization
β”‚   β”œβ”€β”€ cors.ts              # CORS configuration
β”‚   β”œβ”€β”€ database.ts          # MongoDB & Redis connections
β”‚   β”œβ”€β”€ swagger.ts           # OpenAPI documentation
β”‚   └── websocket.ts         # WebSocket setup
β”œβ”€β”€ routes/                   # Auto-loaded routes
β”‚   β”œβ”€β”€ auth/                # Authentication endpoints
β”‚   β”œβ”€β”€ groups/              # Group management
β”‚   β”œβ”€β”€ users/               # User management
β”‚   └── files/               # File operations
β”œβ”€β”€ domain/                   # Domain entities
β”œβ”€β”€ application/              # Business logic
β”œβ”€β”€ infrastructure/           # External services
β”œβ”€β”€ presentation/             # Schemas & controllers
└── config/                   # Configuration
  1. Subsequent Requests:
    • Include JWT in Authorization header: Bearer <token>
    • Backend validates JWT and extracts user/group/role info

πŸ”§ Development

# Development with hot reload
npm run dev

# Build for production
npm run build

# Start production server
npm start

# Run tests
npm test

# Lint code
npm run lint

πŸš€ Deployment

The API is designed to be deployed on Railway or similar platforms:

  1. Connect your repository to Railway
  2. Set environment variables
  3. Deploy automatically

πŸ“Š Monitoring

  • Health check: GET /health
  • Logs: Structured logging with Pino
  • Error tracking: Built-in error handling

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request