Skip to content

Latest commit

 

History

History
237 lines (170 loc) · 4.67 KB

File metadata and controls

237 lines (170 loc) · 4.67 KB

Phase 1 Setup Instructions

Quick Start Guide

Follow these steps to get the Order Matching Engine backend running locally.

1. Prerequisites

Ensure you have these installed:

2. Install Dependencies

Open a terminal in the project directory and run:

npm install

This will install all required packages including TypeScript, Express, Prisma, Redis client, and more.

3. Setup Environment Variables

Copy the example environment file:

cp .env.example .env

Edit the .env file with your configuration:

NODE_ENV=development
PORT=3000

# Update with your PostgreSQL credentials
DATABASE_URL="postgresql://postgres:your_password@localhost:5432/trading_engine?schema=public"

# Generate a secure random string for production
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production

# Default is fine for local Redis
REDIS_URL=redis://localhost:6379

Important: Change your_password to your actual PostgreSQL password.

4. Create PostgreSQL Database

Open PostgreSQL command line:

# Using psql
psql -U postgres

Create the database:

CREATE DATABASE trading_engine;
\q

5. Run Database Migrations

Generate Prisma Client and create database tables:

npm run prisma:generate
npm run prisma:migrate

You should see output confirming the migration was successful and all tables were created.

6. Start Redis

On Windows (WSL or Redis for Windows):

redis-server

On macOS (using Homebrew):

brew services start redis

On Linux:

sudo systemctl start redis

Verify Redis is running:

redis-cli ping
# Should output: PONG

7. Start the Development Server

npm run dev

You should see:

🚀 Server running on port 3000
📊 Environment: development
Redis connected successfully
🔗 Health check: http://localhost:3000/health

8. Test the API

Open a new terminal and test the health endpoint:

curl http://localhost:3000/health

You should get:

{
  "status": "ok",
  "timestamp": "2026-01-21T..."
}

9. Register Your First User

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

Save the accessToken from the response for making authenticated requests.

10. Create Your First Order

Replace YOUR_ACCESS_TOKEN with the token from step 9:

curl -X POST http://localhost:3000/orders \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -d '{
    "symbol": "AAPL",
    "side": "BUY",
    "price": 150.00,
    "quantity": 10
  }'

Troubleshooting

"Port 3000 is already in use"

Change the PORT in your .env file to another port like 3001.

"Database connection error"

  • Verify PostgreSQL is running: pg_isready
  • Check your DATABASE_URL in .env
  • Ensure the database trading_engine exists

"Redis connection error"

  • Verify Redis is running: redis-cli ping
  • Check your REDIS_URL in .env

"Prisma Client not generated"

Run: npm run prisma:generate

TypeScript errors

Make sure all dependencies are installed: npm install


Development Tools

View Database Records

npm run prisma:studio

Opens a web UI at http://localhost:5555 to view and edit database records.

Check Code Quality

npm run lint
npm run format

Build for Production

npm run build
npm start

Next Steps

Now that Phase 1 is running:

  1. ✅ You can register users and authenticate
  2. ✅ Create buy/sell orders
  3. ✅ View portfolios and open orders
  4. ✅ Orders are being queued in Redis

Coming in Phase 2:

  • Order matching engine implementation
  • Real-time trade execution
  • WebSocket support for live updates
  • Order cancellation

Useful Commands Reference

Command Description
npm run dev Start development server with hot reload
npm run build Compile TypeScript to JavaScript
npm start Run production build
npm run prisma:generate Generate Prisma Client
npm run prisma:migrate Run database migrations
npm run prisma:studio Open database GUI
npm run lint Check code quality
npm run format Format code with Prettier

Congratulations! Your Phase 1 backend is now running. 🎉

Refer to README.md for full documentation and API_ROUTES.md for complete API reference.