Getting configuration from plan in Sanic app

I have a Sanic app and want to pull app.config

from the drawing as it contains MONGO_URL

and I will push it to the repository class from the drawing.

However, I couldn't find how to get app.config

in the project. I checked Flask solutions as well, but they don't apply to Sanic.

My app.py

:

from sanic import Sanic
from routes.authentication import auth_route
from routes.user import user_route

app = Sanic(__name__)
app.blueprint(auth_route, url_prefix="/auth")
app.blueprint(user_route, url_prefix="/user")

app.config.from_envvar('TWEETBOX_CONFIG')
app.run(host='127.0.0.1', port=8000, debug=True)

      

My auth blueprint

:

import jwt
from sanic import Blueprint
from sanic.response import json, redirect
from domain.user import User
from repository.user_repository import UserRepository
...

auth_route = Blueprint('authentication')
mongo_url = ?????
user_repository = UserRepository(mongo_url)
...

@auth_route.route('/signin')
async def redirect_user(request):
    ...

      

+3


source to share


4 answers


I would suggest a slightly different approach based on the 12 Factor App (a very interesting read that provides good guidance on how to protect and isolate your sensitive information, among other things).

The general idea is to put your sensitive and config variables in a file that will be gitignored and therefore only available locally.

I will try to present the method that I usually use to be as close as possible to the 12 factor guidelines:



  • Create a file .env

    with your project variables in it:

    MONGO_URL=http://no_peeking_this_is_secret:port/
    SENSITIVE_PASSWORD=for_your_eyes_only
    CONFIG_OPTION_1=config_this
    DEBUG=True
    ...
    
          

  • ( Important) Add .env

    and .env.*

    in your file .gitignore

    , thus protecting your confidential information from download to GitHub.

  • Create env.example

    (be careful not to call it with .

    at the beginning, because it will be ignored).
    In this file you can give an example of the expected configuration to be reproducible simply copy, paste, rename to .env

    .

  • In a file named, settings.py

    use decouple.config

    to read your config file in variables:

    from decouple import config
    
    
    MONGO_URL = config('MONGO_URL')
    CONFIG_OPTION_1 = config('CONFIG_OPTION_1', default='')
    DEBUG = config('DEBUG', cast=bool, default=True)
    ...
    
          

  • Now you can use these variables wherever your implementation needs it:

    myblueprint.py

    :

    import settings
    
    ...
    auth_route = Blueprint('authentication')
    mongo_url = settings.MONGO_URL
    user_repository = UserRepository(mongo_url)
    ... 
    
          

As a finisher, I would like to point out that this method is framework (and even language) agnostic , so you can use it on Sanic

as well as Flask

wherever you need it!

+1


source


Flask has a variable called current_app. You can use current_app.config["MONGO_URL"]

.
But I am not familiar with Sanica.



+1


source


Sanic way ...

Inside the view method, you can access the instance app

from the object request

. And hence access your config.

@auth_route.route('/signin')
async def redirect_user(request):
    configuration = request.app.config

      

+1


source


I think you can create config.py to save your config, just like

config.py

config = {
    'MONGO_URL''127.0.0.1:27017'
}

      

and use it in app.py

from config import config

mongo_url = config['MONGO_URL']

      

0


source







All Articles