Skip to content

DotDev262/Haskell-Url_Shortener

Repository files navigation

URL Shortener

This project implements a simple URL shortener using Haskell, with an SQLite database to store URL mappings. It includes both a web interface for interacting with the URL shortener and backend functionality to generate and store shortened URLs. Additionally, it features an in-memory storage option for URLs with optional password protection.

Features

  • Generate Short URLs: Convert long URLs into short, easy-to-remember links.
  • View All Shortened URLs: Display a list of previously shortened URLs.
  • Redirect to Original URL: When a short URL is accessed, users are redirected to the corresponding original URL.
  • Persistent Storage: URL mappings are stored in an SQLite database.
  • In-Memory Storage with Password Protection: Store URLs in memory with optional password protection for added security.
  • CORS Support: Cross-origin resource sharing is enabled for local development.

Requirements

  • Haskell: The project is written in Haskell, so you will need to have the Haskell compiler (ghc) and the build tool (stack) installed.
  • SQLite: SQLite is used as the database backend.
  • Libraries: The project uses the following Haskell libraries:
    • Scotty: Web framework for building the web server.
    • SQLite.Simple: SQLite library for database interactions.
    • Aeson: JSON library for serializing/deserializing data.
    • Crypto.Random: For generating random strings for shortened URLs.
    • Control.Concurrent.STM: For atomic operations on the in-memory URL store.
    • Data.Map: For in-memory storage of URLs.

Getting Started

Follow the steps below to get the project up and running.

1. Clone the Repository

git clone [https://github.com/dotdev262/url-shortener.git](https://github.com/dotdev262/url-shortener.git)
cd url-shortener

2. Install Dependencies

If you don't already have stack installed, you can install it by following the instructions at: https://docs.haskellstack.org/

Run the following command to install the dependencies:

stack setup
stack build

3. Run the Application

Once the dependencies are installed, you can build and run the project with:

stack run

The server will start and listen on http://localhost:3000.

4. Access the Application

Open your browser and navigate to http://localhost:3000 to access the URL shortener. You can:

  • Enter a long URL to shorten it.
  • View a list of previously shortened URLs.
  • Click on any short URL to be redirected to the original URL.
  • Use the in-memory store by submitting a url to /shorten/memory with an optional password.

How It Works

  • Backend:
    • The backend uses SQLite to store URL mappings in a table with columns original_url and short_url. When a user submits a URL, the backend generates a short, random string and stores both the original and short URLs in the database.
    • An in-memory store is also implemented using Data.Map and Control.Concurrent.STM for thread-safe operations. This allows for storing URLs with optional password protection.
  • Frontend: The web interface allows users to input a URL, get a short URL, and view a list of all shortened URLs. It also supports password protected in-memory short urls.
  • URL Generation: The short URL is generated by converting 6 random bytes into a string of lowercase letters (a-z) for the SQLite database. In-memory short urls are generated by adding a number to "xyz" (e.g. xyz1, xyz2, etc.).

Example

  1. Navigate to http://localhost:3000.
  2. Input a long URL, such as https://www.example.com.
  3. Click the "Shorten URL" button.
  4. The server will respond with a short URL like http://localhost:3000/abcd12.
  5. Click the short URL, and you will be redirected to https://www.example.com.
  6. To use the in-memory storage, submit a POST request to /shorten/memory with a URL and optional password.
  7. Access the in-memory short url by using the short url generated from the previous step. If a password was provided, you will be prompted for it.

Code Overview

Modules

  • Main.hs: Contains the web server and route handling using the Scotty framework. Handles both SQLite and in-memory URL storage.
  • Lib.hs: Contains the backend logic, including generating short URLs, interacting with the database, and defining the URLMapping and UrlEntry data types.

Database

The project uses an SQLite database (urls.db) to store URL mappings. The database contains a single table:

  • urls:
    • id: Integer primary key.
    • original_url: The original URL provided by the user.
    • short_url: The generated short URL.

URL Shortening Logic

  • Short URLs for the SQLite database are generated by creating a random 6-character string, composed of lowercase letters.
  • The short URL is stored in the database along with the original URL.
  • When a user visits a short URL, the server looks up the original URL in the database and redirects the user.
  • In-memory short urls are generated by adding a number to "xyz".
  • In-memory urls are stored with an optional password.
  • In-memory urls with passwords require password validation before being redirected.

Contributing

Contributions are welcome! Feel free to submit issues, fork the repository, and submit pull requests.

  1. Fork the repository.
  2. Create your feature branch (git checkout -b feature/my-feature).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature/my-feature).
  5. Create a new pull request.

About

A Haskell Based Url Shortener

Topics

Resources

License

Stars

Watchers

Forks