A super simple Azure (v3) Function URL shortener service, which uses Azure table storage as its back-end.
Entries can be added using Azure Storage Explorer or automatically using a CMS handler (prismic.io currently supported).
See Quickstart: Create an Azure Storage table in the Azure portal for basic setup.
Function currently uses a single partition, the key
is the (case sensitive) short URL and we'll redirect to the Url
value or the DefaultRedirect
setting if key is not found. Leading and trailing forward /
and backward \
slashes are removed from keys and should not be present in the table.
Short domains should be short, but as custom domains for functions depend upon CNAMEs (and the RFC doesn't permit a CNAME to reference root), getting an ultra short domain on a function is tricky. There are no doubt lots of ways to solve this problem (all including additional components), but Frontdoor isn't too tricky to set up:
- Register a domain, add it to Azure DNS and get your registrar pointing at the provided name servers
- Add an apex domain cert to a KeyVault (Frontdoor supports auto. cert. generation, but not for bare domains, they also have a limited CA list to be aware of)
- Allow the Frontdoor service access to your KeyVault (see Tutorial: Configure HTTPS on a Front Door custom domain
- Create a Frontdoor instance with defaults
- Add the custom domain to Frontdoor (see Tutorial: Add a custom domain to your Front Door)
- Enable custom domain HTTPS and select use own certificate, select the certificate added to KeyVault earlier
- Add your function as a back-end pool: disable the health probe if needed (handled elsewhere and incurs cost), leave alone if you want your function to be kept alive
- Add an HTTPS only routing rule for your short domain /* to the Function back-end pool
- Add a http->https redirect for your short domain
- With a bit of luck, that's it!
The function can be set up to pull in documents and build the storage table through a back-end CMS, assuming the appropriate cms settings are provided. The desired CMS handler can be specified through the CmsType
configuration entry, it's associated settings within a comma delimited list within CmsSettings
and a private/special URL within CmsRefreshKey
. CMS handlers can be built through the CmsHandler
base class and the following pre-built handlers are included:
Sample local.settings.json
for our https://ct.lol domain.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"AzureWebJobsDisableHomepage": true,
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"ShortUrl.DefaultRedirect": "https://cloudthing.com",
"ShortUrl.CmsType": "Prismic",
"ShortUrl.CmsSettings": "repo=launchparty,ref=master,type=ct-short-url",
"ShortUrl.CmsRefreshKey": "<some secret>",
"ShortUrl.StorageAccount": "ctlol",
"ShortUrl.StorageKey": "<a storage account key>",
"ShortUrl.TableName": "ctlol",
"ShortUrl.PartitionKey": "ctlol"
},
}
On deployment, relevant configuration will need setting with Azure, (see Application Settings) for options.
- The function uses a simple in-memory cache with a TTL of five minutes, aligned with the non-durable function disposition timer
- Upon refresh from CMS, short urls are inserted or overwritten only, therefore it doesn't currently support removal/un-publish of URLs
Workarounds: add an empty value to your cms or use storage explorer to delete the entry (both will redirect to default)
If anyone finds this remotley useful, are welcome.