I am trying to set up a subdomain on a flask server, which has a server hosted on Heroku and a custom domain hosted on GoDaddy. I have verified that my subdomain is working locally. The subdomain is a separate blueprint in my app. My setup in flask is:
blueprint = Blueprint('blueprint', __name__, template_folder="templates", subdomain="blueprint")
#blueprint.route('/')
def index():
return "Hello Mate"
and then
app.config['SERVER_NAME'] = os.environ['MY_SERVER_NAME']
from blueprint.views import blueprint
app.register_blueprint(blueprint)
On my local machine, I set up a custom record in my hosts file (/etc/hosts) to test the subdomain. The file has the entries:
127.0.0.1 virtual.local
127.0.0.1 blueprint.virtual.local
If I navigate to blueprint.virtual.local:5000, I see the intended result (a page that just says Hello Mate. I believe this proves my subdomain settings are set up properly, at least within flask.
I push my code to my heroku app, and this is where I start running into problems. My heroku site has a custom domain associated with it from before. I start by adding an entry for the new subdomain. Running heroku domains in the terminal gives me:
=== myapp Domain Names
blueprint.mysite.com
www.mysite.com
myapp.herokuapp.com
mysite.com
The first issue I am running into is that I can only either view my site on the heroku URL or the custom domain. This is a result of app.config['SERVER_NAME'] (which I set to get my subdomain working) being linked to either the heroku URL or my custom URL. When it is set to the heroku URL, I can only see the site when I visit it at that URL, and when I go to my custom domain, I get a 404 error. This is reversed when I switch the value of the SERVER_NAME.
The second issue is that I cannot get my subdomain to work with GoDaddy on Heroku. In GoDaddy, I create a CNAME record that points my subdomain (blueprint) to my heroku site (myapp.herokuapp.com). Is this correct? I get a 404 error whenever I visit the subdomain on my custom domain (blueprint.mysite.com). I believe this is related to the first issue, but I am not sure. Am I missing any steps?
Any advice on the proper way to set this up, so that I can use Flask subdomains on Heroku, being hosted on a custom domain on GoDaddy? Thanks in advance!
I suspect you're confusing Flask Blueprints and Heroku apps. A flask app (and its containing git repository, in this case) is one and only one Heroku app (a single domain, or subdomian... but crucially, only one of them).
A Flask Blueprint is a way of organizing individual sections of a single Flask app to be more modular.
To create Heroku Apps at awesome.darrellsilver.com and sauce.darrellsilver.com you should set up two independent Flask Apps, in two independent Git repos.
For what it is worth, I had 404 issues when I switched to an SSL endpoint using Flask on Heroku. All I had to do was change the app.config "SERVER_NAME" to the new "www.CUSTOMENDPOINTNAME.com" address from the "CUSTOMENDPOINTNAME.herokuapp.com" which was there previously.
Related
I had worked with the Flask application which functions as an admin panel and an API. My admin panel includes login page and bunch of admin stuff in it. So I don't want to expose it to the internet.Admin panel should be only accessible from the intranet however my API should be accessible from the internet.
I have two machines.One is a local machine and other one will be hosted at the AWS. The problem is that the code will be same in the two machines however one will serve as an API and other will serve as an Admin panel.
My supervisor told me that I can use "Flask blueprints" to achieve what I am trying to do but I want to be sure before starting to implement.
Can Flask blueprints solve this problem or are there any other options?
(One thing comes to mind is to separate the API from the admin panel into two different Flask apps. Which is easy to do and solves everything. However I am unable to do that right now. )
Image of what I am trying to do
You can use before_app_request on your blueprint and check if your client ip starts with 192.168 or 10.0 or 172.16
from flask import abort
#blueprint_1.before_request
def check_network():
if not request.remote_addr.startswith("192.168") or ...:
abort(404)
Edit : According to Blueprint documentation before_app_request Such a function is executed before each request, even if outside of a blueprint.
use before_request instead (it will be executed before request on blueprint_1 Blueprint ... i edited my code ...
I have two django application which are on same server on port 80 and 9002. i.e. urls are www.abc.com and www.abc.com:9002
Both share same database postgresql for authentication. I want to share the share the session data between them so that user logged in to one application can log in automatically in another application.
I read these answers : Multiple Django apps, shared authentication and How to get distinct Django apps on same subdomain to share session cookie?
And did this in my both django application:
Used the same secret key in both.
Added these lines:
SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'
SESSION_COOKIE_NAME = 'abc'
SESSION_COOKIE_DOMAIN = '.abc.com'
But still I am unable to achieve the purpose.
How to share the session cookie between two django apps so that i can have shared authentication?
Other than you have to apply these settings to both applications,
the only thing missing with your approach is the SESSION_COOKIE_DOMAIN.
You set it to '.abc.com', which means it will work if your app has domain name: www.abc.com and somesubdomain.abc.com.
But your second app in this case www.abc.com:9002, by including the port it doesn't share the same TLD with www.abc.com. So, django thinks www.abc.com:9002 and www.abc.com are very different domain and not from the same root .abc.com.
If I'm working on this, there are several possible approach:
Combine both app into one single root django app. Django app were modular anyway, so you could create one single ROOT_URL_CONF and DJANGO_SETTINGS_MODULE to specify how these two apps works in the same domain. You could, for example, append a different prefix url for each app.
You use load balancer, or reverse proxy, such as nginx or haproxy to assign different subdomain for each app, then deploy each app in a different port. Let's say, the end result is you have the first django app deployed on first.abc.com and the second app in second.abc.com (All with port 80 in the frontend), then it will share the same session. Just remember that in the backend you need to assign the actual port that the app uses.
Additional notes from mine. In production settings, you also want to add ALLOWED_HOSTS settings and include .abc.com in the list.
I have a simple portfolio website with some html and css files in the root directory of the site hosted by Dreamhost. I also have a Django app that I'd like to place in a subdomain of this same website. However, Heroku will be serving the django app. I'm confused about how to organize and configure the whole portfolio/django website. How would the system work using two different hosts? Should I integrate the static portfolio site into the django project? Or do I keep them completely seperate and have them live on their own servers? Sorry if my question doesn't make sense. I'm very confused.
As far as the internet's concerned, a subdomain is a completely separate website. You can point a subdomain at whatever address you like; the internet doesn't care that it's a completely separate host. You can host your system however you like: both on Dreamhost, both on Heroku, or one on each. The latter setup is the most complex, so we'll walk through that one here.
Let's say your site is example.com and you want the portfolio site to be portfolio.example.com. If your app's running on Heroku, it'll have a name similar to yourportfolio.herokuapp.com. So we need to do two things: tell Heroku that your app is served from portfolio.example.com, and tell the DNS system to point from your subdomain from Heroku.
Pointing the subdomain to Heroku
Presuming your domain name is hosted on Dreamhost, go to the Domains section of the control panel, then Manage Domains. Under example.com is a link called DNS. You need to add a custom CNAME record; set name to portfolio, type to CNAME, and value to yourportfolio.herokuapp.com.. CNAMEs are a way of setting up aliases on the web; they mean "this site is also known as foo".
Telling Heroku to serve your app
Within your Heroku project, run heroku domains:add portfolio.example.com.
Heroku has documentation about subdomains here, which is a useful overview of the process as well as giving details of more complex setups.
Let's say I have a Flask app running. When someone goes to any page, or makes any sort of request on the page, I want that request to be copied to another Flask app. Is there an already existing Flask plugin that would allow me to do so?
By copy I mean this:
My app is test.com. I have another Flask app running on a private machine on a private IP. When I get a GET request on test.com, I want the same GET request to be sent to the Flask app on the private app.
As others have said in the comments, the best kind of proxy is that provided by your web server. However sometimes you actually need your web application to do the proxying, in that case see this answer: Proxying to another web service with Flask
I have a Flask application hosted on Heroku, and the Heroku instance (say, "helloworld.herokuapp.com") has a custom domain name, say "www.helloworld.com".
When I access the app at the native heroku URL, sessions work perfectly fine. When I access it at www.helloworld.com, they don't work. I assume that this is because the session cookie that Flask is signing is for the wrong domain.
I tried assigning app.SESSION_COOKIE_DOMAIN and app.SERVER_NAME to 'helloworld.com', but it still only signs the session cookies for helloworld.herokuapp.com.
Is there any way I can force the session cookies to sign as my custom domain?
After much testing and many permutations of SESSION_COOKIE_DOMAIN and SERVER_NAME, I concluded that the problem was with Heroku. Something about the way Heroku currently routes/hooks up to custom domains breaks domain cookies.
I verified this by moving to EC2...now everything works.