Skip to content

rajmane84/AlgoCode_Backend

Repository files navigation

LeetCode Clone Backend

A backend service for a LeetCode-like platform, featuring user management, RESTful APIs, PostgreSQL integration, and Redis caching for improved performance.


Table of Contents


Features

  • User authentication and management
  • RESTful API endpoints
  • PostgreSQL integration with Prisma ORM
  • Redis caching for user data, authentication, and problem management
  • Session management and data persistence
  • (Add more features as your app grows)

Project Structure

backend/
  ├── src/
  │   ├── config/
  │   │   └── redis.ts          # Redis client configuration
  │   ├── controllers/
  │   ├── middlewares/
  │   ├── models/
  │   ├── routes/
  │   ├── services/             # Business logic with Redis caching
  │   ├── utils/
  │   └── index.ts
  ├── prisma/
  ├── public/
  ├── Dockerfile
  ├── docker-compose.yaml
  ├── package.json
  └── ...

Local Development

You can run the backend locally using one of the following methods:

Manual Setup

  1. Clone the repository:

    git clone <repo-url>
    cd backend
  2. Install dependencies:

    pnpm install
    # or
    npm install -g pnpm && pnpm install
  3. Set up environment variables:

    • Create a .env file in the root directory. Example:
      PORT=5001
      DATABASE_URL=postgres://leetcode:leetcode@localhost:5432/leetcode_db
      REDIS_HOST=localhost
      REDIS_PORT=6379
      # Add other variables as needed (see .env.example)
      
  4. Start PostgreSQL:

    • You must have a PostgreSQL server running.
      You can use an existing instance, or start one with Docker:
      docker run --name leetcode-postgres -e POSTGRES_USER=leetcode -e POSTGRES_PASSWORD=leetcode -e POSTGRES_DB=leetcode_db -p 5432:5432 -d postgres:16.9-alpine
  5. Start Redis:

    • You must have a Redis server running. You can use an existing instance, or start one with Docker:
      docker run --name leetcode-redis -p 6379:6379 -d redis
  6. Generate Prisma client:

    pnpm prisma generate
  7. Build and start the server:

    pnpm build && pnpm start
    # or for development
    pnpm dev

Docker

  1. Start PostgreSQL and Redis containers:

    docker run --name leetcode-postgres -e POSTGRES_USER=leetcode -e POSTGRES_PASSWORD=leetcode -e POSTGRES_DB=leetcode_db -p 5432:5432 -d postgres:16.9-alpine
    
    docker run --name leetcode-redis -p 6379:6379 -d redis:7.2-alpine

    Or use your own PostgreSQL and Redis instances and update the environment variables in .env.

  2. Build the backend Docker image:

    docker build -t leetcode-backend .
  3. Run the backend container:

    docker run --env-file .env -p 5001:5001 leetcode-backend

Docker Compose

  1. Create a .env file in the root directory (see .env.example).

  2. Start all services:

    docker-compose up --build
  • This will start the backend, PostgreSQL, and Redis instances.
  • The backend will be available at http://localhost:5001.
  • PostgreSQL will be available inside the Docker network as postgres:5432.
  • Redis will be available inside the Docker network as my-redis:6379.

Environment Variables

Variable Description Example
PORT Port for the backend server 5001
DATABASE_URL PostgreSQL connection string postgres://leetcode:leetcode@localhost:5432/leetcode_db
REDIS_HOST Redis server host localhost
REDIS_PORT Redis server port 6379
(others) See your .env file for more

Scripts

  • pnpm build – Build the TypeScript project
  • pnpm start – Start the backend (after build)
  • pnpm dev – Start in development mode (watch & auto-restart)
  • pnpm prettier – Format code

Additional Notes

  • Use pnpm for dependency management (recommended).
  • For code formatting, run pnpm prettier.
  • Redis is used for caching user data, authentication tokens, and problem data to improve API response times.
  • The Redis client is automatically initialized when the server starts.
  • For any issues, please check the logs or open an issue.
  • For production, review and secure your environment variables.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published