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 ...
Related
I have been scouring the web to help me find the best way to do this and haven't found a proper answer.
I want to create a single web app with flask that contains multiple dashboard pages. The app needs to run on a different subdomain for every user–the user being a different business eg. client1.myapp.com. The functionality will be largely shared across the different clients and thus subdomains. However, I want to define a config file that will look something like this:
client1 = {"show_graph1":True, "show_graph2":False}
client2 = {"show_graph1":False, "show_graph2":True}
So the app would be hosted on a single aws elastic beanstalk instance and serve all these subdomains. The flow would be:
Client1 goes on unique url client1.myapp.com
Client1 logs in to myapp
Myapp recognises that it is on subdomain for client1, fetches the configuration from the config file and configures the dashboard pages accordingly.
I have looked into flask blueprints and from what I've understood this would be the best way to set this up, but I am not clear on how I would dynamically fetch and implement the configuration nor on how will flask simultaneously serve all subdomains at once.
What would be the best application structure to setup this use case with flask?
Any help would be much appreciated.
If your flask app is listening for all connections, you can point as many domains to it as you like. Then in your dashboard views, or globally if you prefer, you can get your configuration based on what domain the app was requested through.
For example:
#app.before_request
def before_request_func():
domain = request.host
g.client_config = get_client_config(domain)
I know similar questions have been asked here or elsewhere, but I feel like I have read it all and still am unclear on how to solve my specific problem - authentication.
I have written a Bokeh app that is interactive, so requires Bokeh server to be run to serve JavaScript. The app is fed by user data from Strava.
I would like to make the app available to other people, so I need to include authentication to Strava account.
For this reason I thought of incorporating the app in a Django project.
From what I have read, there is no "official" way of doing this and my best bet might be spinning the Bokeh app and the Django on their own servers and view the Bokeh app in an iframe HTML element in Django template.
But would it be then possible to make the apps somehow talk to each other, so that the authentication I need is made through Django and passed to Bokeh app? Also, would it be possible in such setup for the Bokeh app data to be fed from Django models?
I am also willing to switch to other frameworks like Flask and Dash if it would be easier in them, but apparently it is not.
For anyone who finds this question later, since I struggled to find a solution myself, I built out a template that serves Dash as an embedded app within Flask.
https://github.com/seanmajorpayne/dash_multi_user_authentication
I made an app(first app) with Django following a tutorial.
And I finally completed a web server with AWS EC2, nginx, uwsgi, mySQL, and Django.
Then I tried to make a new app(second app).
But I found that I put account information(user model) in first app's model.py. Furthermore, I added something like notification functions in first app's model and view etc...
I want to make new app with first app's account and notification, but I am not sure it's possible to split one app to two apps.
I'd like to make a site(project) have three apps which are account apps(including user model, notification, etc.), first app, and second app. Then, I thought second app can use user info like first app. (Is there any better way?)
I just have a questions, how can I split account app from first app without any data loss. Actually I afraid that if I make a problem, it's very hard to restore it. (model, view, url, ...)
My model is following
class Profile(models.Model) # I'd like to split into account app
class Recruit(models.Model) # stay in first app
class Apply(models.Model) # stay in first app
class Comment(models.Model) # stay in first app
I will appreciate if I can get some tips or references.
Thank you.
Create the new app
Move your models to them
Make migrations and fake migrate migrate --fake
Go to database and change the name of tables - Content types - permissions
for help use this one by me GITHUB script to change model names
Inform me if that worked well with you and don't expect it will be straight forward
Don't think about the app as isolated entity. The concept of the app is something that you want to distribute and let other developers reuse. This is not your case.
You refer the site as project and it's normal to have an app to import from another app. I would suggest you call them packages that are part of your projects, yes you add them in your INSTALLED_APPS but at the end are packages.
Try to have a good packages tree where the references are top-down and not crossed referenced.
Remember: you don't build the application for the database, you build it for the Domain, the database is just a Persistence implementation detail.
From this article.
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.
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