custom domain routing to flask server with custom domain always showing in address bar

  • Last Update :
  • Techknowledgy :

How to associate a custom domain name, such as contoso.com, with your Azure Cloud Service or Web Apps in Azure App Service in order to provide a more recognizable domain name for your users., How to associate a custom domain name, such as contoso.com, with your Azure Cloud Service or Web Apps in Azure App Service in order to provide a more recognizable domain name for your users. ,How to secure communication (SSL) with your Azure web app or an application (SSL) in Azure Cloud Service., How to secure communication (SSL) with your Azure web app or an application (SSL) in Azure Cloud Service.

Get - AzureDeployment - ServiceName yourservicename | Select Url
[NewRequest]
Subject = "CN=mysite.com"
Exportable = TRUE
KeyLength = 2048
KeySpec = 1
KeyUsage = 0xA0
MachineKeySet = True
ProviderName = "Microsoft RSA SChannel Cryptographic Provider"
ProviderType = 12
RequestType = CMC[EnhancedKeyUsageExtension]
OID = 1.3 .6 .1 .5 .5 .7 .3 .1
certreq - new\ path\ to\ myrequest.txt\ path\ to\ create\ myrequest.csr
certreq - accept - user mycert.cer
openssl req - new - nodes - keyout myserver.key - out server.csr - newkey rsa: 2048
Country Name(2 letter code)
State or Province Name(full name)[]: Washington
Locality Name(eg, city)[]: Redmond
Organization Name(eg, company)[]: Microsoft
Organizational Unit Name(eg, section)[]: Azure
Common Name(eg, YOUR name)[]: www.microsoft.com
Email Address[]:

   Please enter the following 'extra'
attributes to be sent with your certificate request

A challenge password[]:

Suggestion : 2
@app.route("/login", methods = ["GET", "POST"])
def login_func():
   ...
PREFIX = "/my-app"

@app.route(f "{PREFIX}/login", methods = ["GET", "POST"])
def login_func():
   ...
@app.route("/my-app/login", methods = ["GET", "POST"])
def login_func():
   ...
from werkzeug.middleware.dispatcher
import DispatcherMiddleware
from werkzeug.wrappers
import Response

app.wsgi_app = DispatcherMiddleware(
   Response('Not Found', status = 404), {
      '/my-app': app.wsgi_app
   }
)
location / my - app / {
   proxy_pass http: //127.0.0.1:8000/;
      #...
}
location / my - app / {
   proxy_pass http: //127.0.0.1:8000/my-app/;
      #... ^ ^ ^ ^ ^ ^ ^
}

Suggestion : 3

Flask 0.7 introduces the concept of URL processors. The idea is that you might have a bunch of resources with common parts in the URL that you don’t always explicitly want to provide. For instance you might have a bunch of URLs that have the language code in it but you don’t want to have to handle it in every single function yourself.,This is an awful lot of repetition as you have to handle the language code setting on the g object yourself in every single function. Sure, a decorator could be used to simplify this, but if you want to generate URLs from one function to another you would have to still provide the language code explicitly which can be annoying.,The reverse of that function are url_value_preprocessor()s. They are executed right after the request was matched and can execute code based on the URL values. The idea is that they pull information out of the values dictionary and put it somewhere else:,That way you no longer have to do the lang_code assignment to g in every function. You can further improve that by writing your own decorator that prefixes URLs with the language code, but the more beautiful solution is using a blueprint. Once the 'lang_code' is popped from the values dictionary and it will no longer be forwarded to the view function reducing the code to this:

from flask import Flask, g

app = Flask(__name__)

@app.route('/<lang_code>/')
   def index(lang_code):
   g.lang_code = lang_code
   ...

   @app.route('/<lang_code>/about')
      def about(lang_code):
      g.lang_code = lang_code
      ...
@app.url_defaults
def add_language_code(endpoint, values):
   if 'lang_code' in values or not g.lang_code:
   return
if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):
   values['lang_code'] = g.lang_code
@app.url_value_preprocessor
def pull_lang_code(endpoint, values):
   g.lang_code = values.pop('lang_code', None)
from flask import Flask, g

app = Flask(__name__)

@app.url_defaults
def add_language_code(endpoint, values):
if 'lang_code' in values or not g.lang_code:
return
if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):
values['lang_code'] = g.lang_code

@app.url_value_preprocessor
def pull_lang_code(endpoint, values):
g.lang_code = values.pop('lang_code', None)

@app.route('/<lang_code>/')
   def index():
   ...

   @app.route('/<lang_code>/about')
      def about():
      ...
from flask import Blueprint, g

bp = Blueprint('frontend', __name__, url_prefix='/<lang_code>')

   @bp.url_defaults
   def add_language_code(endpoint, values):
   values.setdefault('lang_code', g.lang_code)

   @bp.url_value_preprocessor
   def pull_lang_code(endpoint, values):
   g.lang_code = values.pop('lang_code')

   @bp.route('/')
   def index():
   ...

   @bp.route('/about')
   def about():
   ...

Suggestion : 4

If you’ve gone through the Flask tutorial, the syntax in this code block might look familiar to you. @app.route is a decorator used to match URLs to view functions in Flask apps.,This code block shows an example using our custom decorator and the @login_required decorator from the Flask-Login extension. We can use multiple decorators by stacking them.,Advanced patterns for views and routing View decorators Authentication Caching Custom decorators URL Converters Built-in converters Custom converters Summary ,We can develop custom view decorators to help us organize our code and stick to DRY (Don’t Repeat Yourself) coding principals.

@decorator_function
def decorated():
   pass
# app.py

from Flask
import render_template
from flask.ext.login
import login_required, current_user

@app.route('/')
def index():
   return render_template("index.html")

@app.route('/dashboard')
@login_required
def account():
   return render_template("account.html")
# app.py

from flask.ext.cache
import Cache
from flask
import Flask

app = Flask()

# We 'd normally include configuration settings in this call
cache = Cache(app)

@app.route('/')
@cache.cached(timeout = 60)
def index(): [...] # Make a few database calls to get the information we need
return render_template(
   'index.html',
   latest_posts = latest_posts,
   recent_users = recent_users,
   recent_photos = recent_photos
)
# myapp / util.py

from functools
import wraps
from datetime
import datetime

from flask
import flash, redirect, url_for

from flask.ext.login
import current_user

def check_expired(func):
   @wraps(func)
def decorated_function( * args, ** kwargs):
   if datetime.utcnow() > current_user.account_expires:
   flash("Your account has expired. Update your billing info.")
return redirect(url_for('account_billing'))
return func( * args, ** kwargs)

return decorated_function
# This code:
   @foo
@bar
def one():
   pass

r1 = one()

# is the same as this code:
   def two():
   pass

two = foo(bar(two))
r2 = two()

r1 == r2 # True
# myapp / views.py

from flask
import render_template

from flask.ext.login
import login_required

from.import app
from.util
import check_expired

@app.route('/use_app')
@login_required
@check_expired
def use_app():
   ""
"Use our amazing app."
""
#[...]
return render_template('use_app.html')

@app.route('/account/billing')
@login_required
def account_billing():
   ""
"Update your billing info."
""
#[...]
return render_template('account/billing.html')

Suggestion : 5

Last updated November 04, 2021

To add a custom domain with a subdomain, use the domains:add Heroku CLI command:

$ heroku domains: add www.example.com
Adding www.example.com to⬢ example - app...done▸ Configure your app 's DNS provider to point to the DNS Target▸
whispering - willow - 5678. herokudns.com.▸For help, see https: //devcenter.heroku.com/articles/custom-domains

   The domain www.example.com has been enqueued
for addition▸ Run heroku domains: wait 'www.example.com'
to wait
for completion

You can confirm that your DNS is configured correctly with the host command, assuming your DNS changes have propagated:

$ host www.example.com
www.example.com is an alias
for whispering - willow - 5678. herokudns.com.
   ...

Root domains must be added in addition to any subdomains. The process for adding root domains is the same in the Heroku CLI:

$ heroku domains: add example.com
Adding example.com to⬢ example - app...done▸ Configure your app 's DNS provider to point to the DNS Target▸
whispering - willow - 5678. herokudns.com.▸For help, see https: //devcenter.heroku.com/articles/custom-domains

   The domain example.com has been enqueued
for addition▸ Run heroku domains: wait 'example.com'
to wait
for completion

Remove a domain with domains:remove:

$ heroku domains: remove www.example.com
Removing www.example.com from⬢ example - app...done

Use the heroku domains command to view an app’s current Heroku domain, custom domains, and DNS targets:

$ heroku domains
   ===
   example Heroku Domain
example.herokuapp.com

   ===
   example Custom Domains
Domain Name DNS Target
-- -- -- -- -- -- -- - -- -- -- -- -- -- -- -- -- -- -- -- --
example.com hidden - sierra - 7936. herokudns.com
www.example.com whispering - willow - 5678. herokudns.com