This is an example app for a URL shortener called "Shorties".
- Laravel 10
- php 8.3
- Docker / Sail
- Composer
Follow these steps to get Shorties running locally.
First thing is to clone this repo. Make sure to replace ./shorties
with your chosen path.
git clone https://github.com/bad-mushroom/shorties ./shorties
We're using the Laravel 10 framework here so there's some dependencies we need to pull in.
cd ./shorties
composer install
Composer should have copied .env.example
to .env
but if that processed didn't run you can do it manually.
# Windows
copy .env.example .env
# Mac | Linux
cp .env.example .env
This guide will assume you have Docker installed.
For ease of development, Shorties is using Laravel's Sail wrapper for Docker. You should not need to configure and environment variables though you do need to set an encryption key. For now, just "up" the containers.
Note: Make sure you don't have any other containers running as they may interfere.
vendor/bin/sail up -d
Set your encryption key for Laravel
vendor/bin/sail artisan key:generate
There are a couple of tables (in addition to Laravel's defaults) that need to be added.
vendor/bin/sail artisan migrate
All set! You should be able to run Shorties locally by visiting http://localhost
There's a couple of automated tests you can run to ensure core functionality is working.
Feature Tests for verifying overall, end-to-end, behavior:
vendor/bin/sail test tests/Feature/ShortyTest.php
and Unit Tests for testing individual methods:
vendor/bin/sail test tests/Unit/Services/ShortyTest.php
Most of the heavy lifting is being done by a service class called Shorty
in app\Services\Shorty\
. I choose this approach to limit complexity in the implementing code and isolating the functionality so it can easily be expanded on with minimal impact to the code base.
There's a facade for accessing the service, also called Shorty
.
Shorty::create()
- to create a new short URLShorty::lookup()
- to lookup an existing short URL- Etc... see class for details
I wanted to keep the looking up of the short code and redirect simple for performance and not introduce its own controller. I created a single route that takes a "short code" parameter:
There are some configuration options in config/shorties.php
that can be overridden via .env variables. These values could easily have been added as class properties but consolidating them here gives us a place to expand customization for future features.
For a quick "it looks fine" experience, I went with Bootstrap 5 and kept the out-of-the box styles.
The /analytics
endpoint can also be accessed as an API request by passing an Accept: application/json
header:
curl --location 'http://localhost/analytics' --header 'Accept: application/json'
In the project root there's an example.csv
with some valid and invalid URLs to test with.
Overall this was a fun project to work on and I appreciate the reviewer's time!
- Add authentication to allow user-owned short URLs (since right now they are all "public") ๐ฅ
- Give short codes their own domain instead of everything shared on localhost. shor.ty ๐ค
- Doing a final review, I realized I did not paginate the analytics data. :sad:
- Through out the code I commented where improvements and "nice to haves" could be added
- Would be nice to have seeded some sample data to work with