Skip to content

An azure function URL shortening service that uses table storage for it's database and can pull content from a remote CMS such as prismic.io

License

Notifications You must be signed in to change notification settings

cp7crash/azfunc-shorturl

Repository files navigation

Overview

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).

Table Storage Setup

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.

Custom Apex Domain Setup

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:

  1. Register a domain, add it to Azure DNS and get your registrar pointing at the provided name servers
  2. 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)
  3. Allow the Frontdoor service access to your KeyVault (see Tutorial: Configure HTTPS on a Front Door custom domain
  4. Create a Frontdoor instance with defaults
  5. Add the custom domain to Frontdoor (see Tutorial: Add a custom domain to your Front Door)
  6. Enable custom domain HTTPS and select use own certificate, select the certificate added to KeyVault earlier
  7. 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
  8. Add an HTTPS only routing rule for your short domain /* to the Function back-end pool
  9. Add a http->https redirect for your short domain
  10. With a bit of luck, that's it!

CMS Refresh Setup

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:

  1. Prismic.io

Sample Settings

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.

Important Behaviors

  • 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)

Contributions

If anyone finds this remotley useful, are welcome.

License: GPL v3

About

An azure function URL shortening service that uses table storage for it's database and can pull content from a remote CMS such as prismic.io

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages