Replies: 8 comments 1 reply
-
I'm not clear what the request is here. You should already be able to do this with |
Beta Was this translation helpful? Give feedback.
-
Maybe you're saying that |
Beta Was this translation helpful? Give feedback.
-
I want some routes to be available to example.org only, others to somesite.com only, and some to both. Didn't find any example of how to do it with Blueprints. What's |
Beta Was this translation helpful? Give feedback.
-
Sorry, meant a_bp = Blueprint()
b_bp = Blueprint()
shared_bp = Blueprint()
app.register_blueprint(a_bp, host="a")
app.register_blueprint(b_bp, host="b")
app.register_blueprint(shared_bp) Something along those lines. Sorry, I don't really use this feature, so I don't have a ready made environment to spin up to answer your specific question. |
Beta Was this translation helpful? Give feedback.
-
i have the following example: """
attempt at an MVCE for Flask with domain blueprints (Flask 3.x)
python3 flaskapp-with-blueprint-example-1.py
"""
from flask import Blueprint, Flask, request
def create_app():
app = Flask(__name__)
first_bp = Blueprint('first', __name__)
second_bp = Blueprint('second', __name__)
shared_bp = Blueprint('shared', __name__)
@first_bp.route('/')
def first_home():
return f"""route meant only for first.localhost"""
@second_bp.route('/')
def second_home():
return f"""route meant only for second.localhost"""
@shared_bp.route('/shared')
def shared_page():
return f"""this route should work with both domains"""
app.register_blueprint(first_bp, host="first.localhost")
app.register_blueprint(second_bp, host="second.localhost")
app.register_blueprint(shared_bp)
return app
if __name__ == "__main__":
app = create_app()
app.run(
debug=True,
host='0.0.0.0',
port=6123,
# if running it inside of my docker container:
# ssl_context=(
# "/etc/apache2/ssl/SSLforMyHosts-certificate.pem",
# "/etc/apache2/ssl/SSLforMyHosts-key.pem",
# ),
)
"""adding these lines to your /etc/hosts file should be enough to get up and running:
127.0.0.1 first.localhost
127.0.0.1 second.localhost
""" it runs, but not as expected:
i thought maybe i need this to set
but then every route becomes 404 Not Found. i don't understand what no offense but the documentation could be clearer. |
Beta Was this translation helpful? Give feedback.
-
@fabswt I have been using host matching in production successfully for years. You can see how I’m doing it on this public repo. I did end up subclassing some of the blueprint-related classes to achieve two things. First, I can set a host on an entire blueprint instead of per-route (David's example above suggests this was already possible, so maybe I missed that when I was setting this up years ago); second, I have host matching on static endpoints (which it sounds like you want too). You’re welcome to look at how I did that and borrow it, although I make no warranty to whether it’s “good.” So far it has worked for me. If you have questions, please feel free to ask me here, or you can ping me on the Pallets Discord (same username). |
Beta Was this translation helpful? Give feedback.
-
@davidism any chance of getting feedback as to whether: a. my Minimal, Reproducible Example is wrong or b. Blueprints actually do not support domains out of the box. ? @tachyondecay thank you for the comment, appreciated. I actually stumbled upon your repo while trying to get things to work. Still trying to get things working with out-of-the-box features and a minimal example, but will definitely revisit your code if that doesn't work out. |
Beta Was this translation helpful? Give feedback.
-
for what it's worth, i just gave up. the minimal example i gave should be moved into an issue and/or the docs should be updated as they only mention subdomains and not domains. |
Beta Was this translation helpful? Give feedback.
-
Currently, Blueprints support a
url_prefix
and asubdomain
option. I would like to see support for adomain
option.I need to host the same codebase on two different sites, but with some of the routes only available to one domain and others only to the other, and roughly 3/4 of routes available on both sites.
I don't want to split the codebase into two because 95% of the code is the same. Checking for
request.host
on every route (even with a route decorator) is cumbersome. Having this at the level of Blueprints would make the most sense.There's a way to support multiple domains but it does not allow for the sharing of routes.
Beta Was this translation helpful? Give feedback.
All reactions