145 words
1 minute
Writing an Azure Function that serves all traffic to a subdomain
Anubhav Gain
2024-08-04

Writing an Azure Function that serves all traffic to a subdomain#

Azure Functions default to serving traffic from a path like /api/FunctionName - for example https://your-subdomain.azurewebsites.net/api/MyFunction.

If you want to serve an entire website through a single function (e.g. using Datasette) you need that function to we called for any traffic to that subdomain.

Here’s how to do that - to capture all traffic to any path under https://your-subdomain.azurewebsites.net/.

First add the following section to your host.json file:

"extensions": {
"http": {
"routePrefix": ""
}
}

Then add "route": "{*route}" to the function.json file for the function that you would like to serve all traffic. Mine ended up looking like this:

{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "Anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"route": "{*route}",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}

See https://github.com/simonw/azure-functions-datasette for an example that uses this pattern.

Writing an Azure Function that serves all traffic to a subdomain
https://mranv.pages.dev/posts/writing-an-azure-function-that-serves-all-traffic-to-a-subdomain/
Author
Anubhav Gain
Published at
2024-08-04
License
CC BY-NC-SA 4.0