Shortlytic is a URL Shortener API designed to convert long URLs into shortened, more manageable links.
- Introduction
- Features
- Installation
- Database Configuration
- Usage
- Dependencies
- Configuration
- Troubleshooting
- Contributors
- License
Shortlytic provides a simple API for shortening long URLs, making them easier to share and manage.
- Shorten long URLs into concise links.
- Retrieve original URLs from shortened links.
- Track usage statistics for shortened URLs.
To set up Shortlytic locally:
-
Clone the repository:
git clone https://github.com/sommye-ctr/shortlytic.git
-
Navigate to the project directory:
cd shortlytic
-
Build the project using Maven:
./mvnw clean install
-
Run the application:
./mvnw spring-boot:run
Shortlytic uses PostgreSQL as the primary database. Follow these steps to set up the database:
-
Install PostgreSQL
Install PostgreSQL on your system if not already installed. You can download it from the official PostgreSQL website. -
Create a Database
Use the following SQL queries to set up the required database schema and functions for Shortlytic:CREATE TABLE users ( user_id UUID PRIMARY KEY DEFAULT gen_random_uuid(), username VARCHAR(255) NOT NULL UNIQUE, email VARCHAR(255) NOT NULL UNIQUE, password_hash VARCHAR(255) NOT NULL, created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE urls ( id SERIAL PRIMARY KEY, user_id UUID REFERENCES users(user_id) ON DELETE CASCADE, url TEXT NOT NULL, short_code VARCHAR(255) NOT NULL UNIQUE, created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, expiry_at TIMESTAMPTZ, password_protected BOOLEAN DEFAULT FALSE, password VARCHAR(255) ); CREATE TABLE analytics ( id SERIAL PRIMARY KEY, url_id INT REFERENCES urls(id) ON DELETE CASCADE, clicked_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, ip_address INET, country VARCHAR(50), device_type VARCHAR(50) ); CREATE OR REPLACE FUNCTION generate_unique_short_code(length INT) RETURNS TEXT AS $$ DECLARE new_code TEXT; exists_count INT; BEGIN LOOP new_code := ( SELECT string_agg(substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' FROM trunc(random() * 36 + 1)::int FOR 1), '') FROM generate_series(1, length) ); SELECT COUNT(*) INTO exists_count FROM public.urls WHERE short_code = new_code; EXIT WHEN exists_count = 0; END LOOP; RETURN new_code; END; $$ LANGUAGE plpgsql;
-
Configure the Database Connection
Update theapplication.properties
file in thesrc/main/resources
directory with your database credentials:spring.datasource.url=jdbc:postgresql://localhost:5432/shortlytic spring.datasource.username=your-username spring.datasource.password=your-password spring.jpa.hibernate.ddl-auto=update
Replace
your-username
andyour-password
with your PostgreSQL username and password. -
Test the Database Connection
Run the application and ensure that it connects to the PostgreSQL database without errors.
Once the application is running, you can interact with the API endpoints to shorten URLs and retrieve original links.
Shortlytic is built with Java and utilizes the following dependencies:
- Spring Boot
- Maven
Configuration details can be adjusted in the application.properties
file located in the src/main/resources
directory.
For common issues and their solutions, please refer to the issues section of the repository.
This project is licensed under the MIT License. See the LICENSE file for details.