The TinyURL project is designed to convert long URLs into short URLs. This allows for easier sharing and management of URLs.
- Create a short URL from a long URL.
- Get the original URL from a short URL.
- Redirect to the original URL from a short URL.
- High availability and scalability using Cassandra.
- Distributed synchronization and group services using Zookeeper.
- Highly configurable using environment variables.
- Unit tests using Jest.
Most of important feature is the ability to generate short URLs in a distributed environment using Zookeeper. This ensures that the short URLs are unique and consistent across multiple instances of the application.
With default configuration, it generates short URLs with a length of 6 characters using the characters a-z
, A-Z
, and 0-9
. 6 characters provide 56,800,235,584 (~56 billion) unique combinations, which should be sufficient for most use cases. However, you can customize the length and characters used to generate the short URLs by changing the environment variables.
Before you begin, ensure you have met the following requirements:
- You have installed the latest version of Docker and Docker Compose.
To get started with the TinyURL project, follow these steps:
- Clone this repository.
- Navigate to the project directory:
cd tinyURL
- Run the following commands to start the development environment:
mkdir cassandra_data zookeeper_data # create persistent data directories for Cassandra and Zookeeper
chmod -R 777 cassandra_data zookeeper_data # set permissions for the data directories, cause we are running the containers as root (Optional, depending on your system)
cp apps/backend/.env.example apps/backend/.env # copy the environment file
### To start the development environment ###
docker-compose -f compose-dev.yaml up --build -d # start the development environment
### To start the production environment ###
docker-compose up --build -d # start the production environment
(Note: Cassandra and Zookeeper will take some time to start up. So, Ignore the connection errors related to the database and Zookeeper until they are up and running. They will go away once the services are up.)
Once the application is running, you can access it through your swagger
http://localhost:5000/docs/api
(development)http://localhost/docs/api
(production - nginx as reverse proxy)
You can also use the following endpoints:
POST /api/v1/create-short-url
: Create a new short URL.GET /api/v1/get/:shortUID
: Get the original URL from a short URL.GET /api/v1/redirect/:shortUID
: Redirect to the original URL from a short URL.
The following environment variables are available for the TinyURL project:
-
app_env
: The environment in which the application is running (development or production). -
app_url
: The base URL for the application. -
app_shortUIDLength
: The length of the short URL. -
app_shortUIDChars
: The characters used to generate the short URL. -
zookeeper_connectionStrings
: The connection strings for Zookeeper. -
zookeeper_poolChunkSize
: The pool chunk size for Zookeeper. -
zookeeper_counterPath
: The path for the counter in Zookeeper. -
cassandra_contactPoints
: Specifies the contact points for Cassandra, including the hostname or IP address and port number. -
cassandra_keyspace
: Specifies the keyspace to be used in Cassandra, which is similar to a database in relational databases. -
cassandra_username
: Specifies the username for authentication with Cassandra. -
cassandra_password
: Specifies the password for authentication with Cassandra. -
cassandra_localDataCenter
: Specifies the local data center for Cassandra, which is used for data replication and fault tolerance. -
cassandra_replicationFactor
: Specifies the replication factor for Cassandra, which determines the number of copies of data to be stored across the cluster. -
cassandra_replicationStrategy
: Specifies the replication strategy for Cassandra, which determines how data is replicated across the cluster. -
cassandra_migrationStrategy
: Specifies the migration strategy for Cassandra, which determines how schema changes are applied to the database.
To run the unit tests for the TinyURL project, follow these steps:
- Navigate to the project directory:
cd tinyURL/apps/backend
- Run the following command to run the unit tests:
npm run test
This command will run the unit tests for the backend app and display the results in the terminal.
- NestJS: A progressive Node.js framework for building efficient, reliable, and scalable server-side applications.
- Cassandra: A distributed NoSQL database that provides high availability and scalability.
- Zookeeper: A centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.
- Jest: A JavaScript testing framework that provides a simple way to write tests.
- TODO
The project has to apps, one for the frontend and one for the backend. The frontend app is located in the apps/fronted
folder, while the backend app is located in the apps/backend
folder.
The apps/backend
folder contains the following files and folders:
src
: Contains the source code for the backend app.Dockerfile
: Defines the Docker image for the backend app.package.json
: Contains the dependencies and scripts for the backend app.tsconfig.json
: Contains the TypeScript configuration for the backend app.nest-cli.json
: Contains the NestJS CLI configuration for the backend app.
The backend/src
folder contains the following files and folders:
main.ts
: Contains the entry point for the backend app.app.module.ts
: Contains the main module for the backend app.app.controller.ts
: Contains the controller for the backend app.app.service.ts
: Contains the service for the backend app.env-config.ts
: Contains the configuration for the backend app.database
: Contains the database configuration and entities for the backend app.tiny-uid
: Contains the utility service for generating short URLs.zookeeper
: Contains the Zookeeper service for the backend app.
Feel free to explore each folder to understand the project's structure and functionality.
- Add frontend app using React.
- Add authentication and authorization using JWT.
- Add rate limiting and request throttling.
- Add logging and monitoring using Winston and Prometheus.
- Add e23 tests using Supertest.
This project generates short URLs in a linear fashion using Zookeeper, which can be a security risk if the short URLs are predictable.
To mitigate this risk:
-
one can use a random number picker to pick the next number from the local server reserved range.
-
Another option is add suffix to the short URL, which can be a random string or a hash of the original URL. This will make the short URLs more secure and less predictable.
Contributions are welcome! If you would like to contribute to the TinyURL project, please follow these guidelines:
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature-name
- Make your changes and commit them:
git commit -m 'Add your commit message'
- Push your changes to the branch:
git push origin feature/your-feature-name
- Submit a pull request.
This README is written with the help of co-pilot.