This project implements a RESTful API for a subway system, addressing two main challenges: route planning and fare management.
- Node.js (v14+)
- Docker and Docker Compose
-
Clone the repository:
git clone https://github.com/emad-siddiq/subway-system.git cd subway-system -
Build and start the application using Docker:
docker-compose up --buildThis will start both the backend service and the PostgreSQL database.
-
The server will be running on
http://localhost:3000
To run the application in development mode:
NODE_ENV=development docker-compose up
This will start the application using ts-node-dev, which will watch for file changes and automatically restart the server.
Run tests:
npm test
To stop the application and remove the containers, volumes, and images:
docker-compose down -v --rmi all
.
├── src/
│ ├── controllers/
│ ├── db/
│ ├── models/
│ ├── routes/
│ ├── utils/
│ └── server.ts
├── docker-compose.yml
├── Dockerfile
├── package.json
├── tsconfig.json
└── README.md
The application uses the following environment variables:
NODE_ENV: Set to 'development' or 'production'DB_HOST: Database host (default: 'db')DB_USER: Database user (default: 'user')DB_PASSWORD: Database password (default: 'password')DB_NAME: Database name (default: 'subway_system')DB_NAME_TEST: Test database name (default: 'subway_system_test')DB_PORT: Database port (default: 5432)DB_PORT_TEST: Test database port (default: 5433)
These can be set in the docker-compose.yml file or as environment variables when running the application.
- POST
/train-line - Body:
{ "name": string, "stations": string[], "fare": number (optional) } - Example:
curl -X POST http://localhost:3000/train-line \ -H "Content-Type: application/json" \ -d '{"name": "1", "stations": ["Canal", "Houston", "Christopher", "14th"]}'
- GET
/route - Query params:
origin(string),destination(string) - Example:
curl "http://localhost:3000/route?origin=Houston&destination=23rd"
- POST
/card - Body:
{ "number": string, "amount": number } - Example:
curl -X POST http://localhost:3000/card \ -H "Content-Type: application/json" \ -d '{"number": "1234", "amount": 20.00}'
- POST
/station/{station_name}/enter - Body:
{ "card_number": string } - Example:
curl -X POST http://localhost:3000/station/Houston/enter \ -H "Content-Type: application/json" \ -d '{"card_number": "1234"}'
- POST
/station/{station_name}/exit - Body:
{ "card_number": string } - Example:
curl -X POST http://localhost:3000/station/14th/exit \ -H "Content-Type: application/json" \ -d '{"card_number": "1234"}'
Note: The server is assumed to be running on http://localhost:3000. Adjust the URL in the examples if your server is running on a different address or port.
- Language: TypeScript
- Database: PostgreSQL
- Containerization: Docker
To create standalone executables for different platforms:
-
Install
pkgglobally:npm install -g pkg -
Build the TypeScript project:
npm run build -
Generate the binaries:
pkg . --out-path=./bin
This will create standalone executables for Linux, macOS, and Windows in the bin directory. Run the appropriate binary for your system.
Note: When running the standalone binary, you'll need to ensure that a PostgreSQL database is available and the correct environment variables are set for the database connection.