Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving config from a blueprint in Sanic app

I have a Sanic application, and want to retrieve app.config from a blueprint as it holds MONGO_URL, and I will pass it to a repository class from the blueprint.

However, I could not find how to get app.config in a blueprint. I have also checked Flask solutions, but they are not applicable 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):
    ...
like image 230
skynyrd Avatar asked Apr 11 '17 19:04

skynyrd


1 Answers

The Sanic way...

Inside a view method, you can access the app instance from the request object. And, therefore access your configuration.

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

2021-10-10 Update

There are two newer ways to get to the configuration values (or, perhaps more accuratlely getting the application instance from which you can get the configuration). The first version might be more on point to answering the question of how to get to the config from the blueprint. However, the second option is probably the preferred method since it is precisely intended for this kind of use.

Alternative #1

Blueprints have access to the Sanic applications they are attached to beginning with v21.3.

Therefore, if you have a blueprint object, you can trace that back to the application instance, and therefore also the config value.

app = Sanic("MyApp")
bp = Blueprint("MyBlueprint")

app.blueprint(bp)

assert bp.apps[0] is app

The Blueprint.apps property is a set because it is possible to attach a single blueprint to multiple applications.

Alternative #2

Sanic has a built-in method for retrieving an application instance from the global scope beginning in v20.12. This means that once an application has been instantiated, you can retrieve it using: Sanic.get_app().

app = Sanic("MyApp")

assert Sanic.get_app() is app

This method will only work if there is a single Sanic instance available. If you have multiple application instances, you will need to use the optional name argument:

app1 = Sanic("MyApp")
app2 = Sanic("MyOtherApp")

assert Sanic.get_app("MyApp") is app1
like image 198
Adam Hopkins Avatar answered Sep 18 '22 23:09

Adam Hopkins