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.
- 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.
- 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.
Follow the steps below to get the project up and running.
git clone [https://github.com/dotdev262/url-shortener.git](https://github.com/dotdev262/url-shortener.git)
cd url-shortenerIf 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 buildOnce the dependencies are installed, you can build and run the project with:
stack runThe server will start and listen on http://localhost:3000.
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/memorywith an optional password.
- Backend:
- The backend uses SQLite to store URL mappings in a table with columns
original_urlandshort_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.MapandControl.Concurrent.STMfor thread-safe operations. This allows for storing URLs with optional password protection.
- The backend uses SQLite to store URL mappings in a table with columns
- 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.).
- Navigate to
http://localhost:3000. - Input a long URL, such as
https://www.example.com. - Click the "Shorten URL" button.
- The server will respond with a short URL like
http://localhost:3000/abcd12. - Click the short URL, and you will be redirected to
https://www.example.com. - To use the in-memory storage, submit a POST request to
/shorten/memorywith a URL and optional password. - 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.
Main.hs: Contains the web server and route handling using theScottyframework. Handles both SQLite and in-memory URL storage.Lib.hs: Contains the backend logic, including generating short URLs, interacting with the database, and defining theURLMappingandUrlEntrydata types.
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.
- 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.
Contributions are welcome! Feel free to submit issues, fork the repository, and submit pull requests.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/my-feature). - Commit your changes (
git commit -am 'Add new feature'). - Push to the branch (
git push origin feature/my-feature). - Create a new pull request.