Shortener is a, Slim framework-based, URL shortening microservice that encapsulates third-party services to provide said functionality.
Currently, two providers are supported, Google and Bitly.
These instructions will get you the project up and running on your local machine for development and testing purposes.
- Apache2
- PHP >= 7.0.0
Finally, Shortener utilizes Composer to manage dependencies. So make sure you have Composer installed on your machine.
-
Add a new entry in your
hosts
file. Hosts file can be found in/etc
directory in Linux\OSX.127.0.0.1 www.shortener.dev
-
Move the project in the
/var/www/html
directory and add a new virtual host in your apache2.<VirtualHost *:80> <Directory /var/www/html/shortener/public> AllowOverride All </Directory> ServerName www.shortener.dev ServerAlias shortener.dev DocumentRoot /var/www/html/shortener/public </VirtualHost>
-
Reload the apache2 configuration
systemctl reload apache2
-
You might have to give some permissions if your local machine is running on Linux\OSX.
sudo chmod 777 -R logs cache
In order to start using the Google Shortener API you must obtain first a Google API key.
Moreover, uou will need to create a Bitly Access token.
After getting the required credentials update the key
entries in config/providers.php.
'google' => [
'url' => 'https://www.googleapis.com/urlshortener/v1/url',
'key' => 'YOUR_API_KEY'
],
Run the tests from the project root.
vendor/bin/phpunit tests
Shortener utilizes various components on top of the Slim framework. Each component has a single responsibility.
The public/index.php file is the entry point of the application. This is the place where the composer autoloader and the bootstrapping are registered. Finally, once we have the application ready we can handle the incoming request.
The bootstrap/app.php file is where we bootstrap the framework. There are some important steps happening here that will get the application ready for use.
- Create the Slim application
- Set the container configuration
- Register various dependencies, defined in bootstrap/dependencies.php
- Register routes, defined in app/routes.php
- Register controller, located in
app/Controllers
directory
This is the place where we get to define how each service, required by the application, is registered to the container.
Each service is a class responsible for choosing the desired implementation, instantiating it and finally returning it.
As we mentioned earlier these implementations are registered to the container in the bootstrap/dependencies.php
file.
Currently the services provided are:
- The ConfigurationService for loading config files
- The LoggingService for reporting errors
- The ValidationService for validating request data
- The HttpClientService for sending HTTP requests
- The ErrorHandlingService for handling exceptions
- The ShorteningService for registering shortening providers
- The CachingService for caching responses from external services
This is the place where the providers of third-party shortening services live. Each provider defines the logic for
building the request, hitting the API of the external service and returning the result back to the caller.
This communication is achieved by the HttpClient registered via the HttpClientService
. When a request to an external
shortening service fails a GuzzleException
is thrown.
Currently the third-party services provided are:
- The GoogleProvider for Google service
- The BitlyProvider for Bitly service
There is only one middleware within this application and it is responsible for validating the request data. This
middleware is attached to the single route provided, and uses the Validator registered via the ValidationService
.
When a request fails to pass validation a ValidationException
is thrown.
The Handler class is responsible for reporting and rendering the exceptions. The reporting
uses the Logger registered via LoggingService
while the rendering creates an HTTP response for the
ValidationException
and the GuzzleException
. Under the hood we are registering, via the ErrorHandlingService
,
a custom error handler to override the default error handler of the framework.
The only available controller is the ShorteningController
. Every request hitting this controller has already been
validated. Moreover, the appropriate provider has already been instantiated via the ShorteningService
.
The only step left is to return the response back to the user.
This package is released under the MIT License.