A simple URL shortening service using .NET and Azure.
You can deploy to Azure using the above button. Prior to deployment you should have an existing Azure Storage Account. Copy the connection string to the clipboard; you will be prompted for it during deployment in a field labeled AzureStorageConnectionString.
Once deployed you can add or modify the connection string for
- Click Manage when deployment is finished (or browse to the web app on the Azure Portal
- Click Settings
- Select Application settings
- Click Show Connection Strings
- Add a new Custom connection string named AzureStorage and paste the connection string to an Azure Storage Account
If you do not provide the Azure Storage connection string an in-memory URL Alias repository will be used. In that case short URLs will break when the web app is restarted.
UrlShortener contains the core logic. There are interfaces you can use to customize the specific mechanics:
- IHashScheme - Used to generate the short URL. Should generate an appropriate short "hash" from a long string. A default implementation is provided which is based on Base64 encoding of SHA1. Implementations must also provide a way to iteratetively generate new hashes from the same input in the case of a hash collision.
- IAliasRepository - Dictionary-like persistence of "alias" or "hash" of one string (the key) to another (the value). Used to save mapping between short and long URLs. The default uses Azure Table storage. The fallback uses an in-memory Dictionary.
- IUrlUnwrapper - Responsible for resolving a direct URL to a resource. For example the provided URL might already be a 'short URL', which could lead to multiple redirects or a redirect loop. The default uses WebClient.
- Natural enhancements to this project would be:
- Core logic in the Durwella.UrlShortening project must be unit tested.
- Unit tests should be written using 3 paragaphs corresponding to Arrange, Act and Assert
- Build must remain clean (no warnings, tests passing)
- Code Analysis issues should not be introduced
- Must be deployable to Azure using AzureDeploy.net
URL Shortening: Hashes In Practice
Nice article by Jeff Atwood. Historical yet relevant.
Testing Azure deployment can be done from Azure PowerShell something like this...
Switch-AzureMode -Name AzureResourceManager $site = "uniqueNameARMTest123" $pw = read-host -AsSecureString New-AzureResourceGroup -TemplateFile .\azuredeploy.json -Name $site -DeploymentName $site ` -Verbose -Location "South Central US" -hostingPlanName $site -hostingPlanLocation "South Central US" ` -siteName $site -repoUrl "https://github.com/Durwella/UrlShortening" -branch "master" ` -title $site -adminPassword $pw # Testing... Remove-AzureResourceGroup -Name $site