A backend service for a LeetCode-like platform, featuring user management, RESTful APIs, PostgreSQL integration, and Redis caching for improved performance.
- 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)
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
└── ...
You can run the backend locally using one of the following methods:
-
Clone the repository:
git clone <repo-url> cd backend
-
Install dependencies:
pnpm install # or npm install -g pnpm && pnpm install
-
Set up environment variables:
- Create a
.envfile 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)
- Create a
-
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
- You must have a PostgreSQL server running.
-
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
- You must have a Redis server running.
You can use an existing instance, or start one with Docker:
-
Generate Prisma client:
pnpm prisma generate
-
Build and start the server:
pnpm build && pnpm start # or for development pnpm dev
- The backend will be available at http://localhost:5001
- Redis will be available at localhost:6379
-
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. -
Build the backend Docker image:
docker build -t leetcode-backend . -
Run the backend container:
docker run --env-file .env -p 5001:5001 leetcode-backend
-
Create a
.envfile in the root directory (see.env.example). -
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.
| 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 |
pnpm build– Build the TypeScript projectpnpm start– Start the backend (after build)pnpm dev– Start in development mode (watch & auto-restart)pnpm prettier– Format code
- 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.