Skip to content

Instantly share code, notes, and snippets.

@tachyondecay
tachyondecay / README.md
Last active February 12, 2024 06:11
Host matching in blueprints at reg time + free static endpoint creation

What This Does

Let's say you want to serve 2 domains, example1.com and example2.com, from the same app (i.e., using host matching). You also want your static assets for each domain to be served from those domains, e.g., example1.com/static/ and example2.com/static/. You can almost achieve this in Flask, but not quite.

This code extends Flask's Blueprint class to make registering hostnames onto a blueprint painless and ensure static assets appear to be served from the appropriate hostname.

Default Flask Behaviour

By default, Flask supports host matching at the app-level by passing host_matching=True and a hostname for static_host when initializing the app. However, there are drawbacks to the current support, especially when using blueprints:

  1. To match all routes on a blueprint to a host, you must pass the host parameter to every route decorator for that blueprint.
  2. The app's static files will always be served