从 Sanic 应用程序的蓝图中检索配置
Retrieving config from a blueprint in Sanic app
我有一个 Sanic 应用程序,想从蓝图中检索 app.config
,因为它包含 MONGO_URL
,我会将它从蓝图中传递到存储库 class。
但是,我找不到如何在蓝图中获得 app.config
。我也查过Flask的解决方案,但是它们不适用于Sanic。
我的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)
我的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):
...
Flask 中有一个名为current_app 的变量。您可以使用 current_app.config["MONGO_URL"]
.
但是我对Sanic不熟悉
我想你可以创建一个 config.py 来保存你的配置,就像
config.py
config = {
'MONGO_URL':'127.0.0.1:27017'
}
并在app.py
中使用
from config import config
mongo_url = config['MONGO_URL']
我会建议一种略有不同的方法,基于 12 Factor App(非常有趣的阅读,除其他外,它提供了一个关于如何保护和隔离您的敏感信息的很好的指南)。
一般的想法是将您的敏感变量和配置变量放在一个文件中,该文件将被 gitignore,因此只能在本地使用。
我将尝试介绍我倾向于使用的方法,以便尽可能接近 12 因素指南:
创建一个 .env
文件,其中包含您的项目变量:
MONGO_URL=http://no_peeking_this_is_secret:port/
SENSITIVE_PASSWORD=for_your_eyes_only
CONFIG_OPTION_1=config_this
DEBUG=True
...
(重要) 在您的 .gitignore
文件中添加 .env
和 .env.*
,从而保护您的敏感信息已上传至 GitHub.
创建一个env.example
(注意不要在开头用.
命名它,因为它会被忽略)。
在该文件中,您可以放置一个预期配置的示例,以便只需 copy, paste, rename to .env
.
即可重现
在名为 settings.py
的文件中,使用 decouple.config
将您的配置文件读入变量:
from decouple import config
MONGO_URL = config('MONGO_URL')
CONFIG_OPTION_1 = config('CONFIG_OPTION_1', default='')
DEBUG = config('DEBUG', cast=bool, default=True)
...
现在您可以在实施所需的任何地方使用这些变量:
myblueprint.py
:
import settings
...
auth_route = Blueprint('authentication')
mongo_url = settings.MONGO_URL
user_repository = UserRepository(mongo_url)
...
作为终结者,我想指出这个方法是框架(甚至语言)不可知所以你可以使用它出现在 Sanic
和 Flask
以及您需要它的任何地方!
Sanic 方式...
在视图方法中,您可以从 request
对象访问 app
实例。并且,因此访问您的配置。
@auth_route.route('/signin')
async def redirect_user(request):
configuration = request.app.config
2021-10-10更新
有两种更新的方法来获取配置值(或者,也许更准确地获取您可以从中获取配置的应用程序实例)。第一个版本可能更准确地回答了如何从蓝图 获取配置的问题。但是,第二个选项可能是 首选 方法,因为它正是为这种用途而设计的。
选择#1
从 v21.3 开始,蓝图可以访问它们附加到 的 Sanic 应用程序。
因此,如果您有蓝图对象,则可以将其追溯到应用程序实例,因此也可以追溯到配置值。
app = Sanic("MyApp")
bp = Blueprint("MyBlueprint")
app.blueprint(bp)
assert bp.apps[0] is app
Blueprint.apps
属性 是 set
因为可以将单个蓝图附加到多个应用程序。
选择#2
从 v20.12 开始,Sanic 有一个从全局范围 检索应用程序实例的内置方法。这意味着应用程序实例化后,您可以使用以下方法检索它:Sanic.get_app()
.
app = Sanic("MyApp")
assert Sanic.get_app() is app
此方法仅在有单个 Sanic
实例可用时才有效。如果您有多个应用程序实例,则需要使用可选的 name
参数:
app1 = Sanic("MyApp")
app2 = Sanic("MyOtherApp")
assert Sanic.get_app("MyApp") is app1
我有一个 Sanic 应用程序,想从蓝图中检索 app.config
,因为它包含 MONGO_URL
,我会将它从蓝图中传递到存储库 class。
但是,我找不到如何在蓝图中获得 app.config
。我也查过Flask的解决方案,但是它们不适用于Sanic。
我的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)
我的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):
...
Flask 中有一个名为current_app 的变量。您可以使用 current_app.config["MONGO_URL"]
.
但是我对Sanic不熟悉
我想你可以创建一个 config.py 来保存你的配置,就像
config.py
config = {
'MONGO_URL':'127.0.0.1:27017'
}
并在app.py
中使用from config import config
mongo_url = config['MONGO_URL']
我会建议一种略有不同的方法,基于 12 Factor App(非常有趣的阅读,除其他外,它提供了一个关于如何保护和隔离您的敏感信息的很好的指南)。
一般的想法是将您的敏感变量和配置变量放在一个文件中,该文件将被 gitignore,因此只能在本地使用。
我将尝试介绍我倾向于使用的方法,以便尽可能接近 12 因素指南:
创建一个
.env
文件,其中包含您的项目变量:MONGO_URL=http://no_peeking_this_is_secret:port/ SENSITIVE_PASSWORD=for_your_eyes_only CONFIG_OPTION_1=config_this DEBUG=True ...
(重要) 在您的
.gitignore
文件中添加.env
和.env.*
,从而保护您的敏感信息已上传至 GitHub.创建一个
env.example
(注意不要在开头用.
命名它,因为它会被忽略)。
在该文件中,您可以放置一个预期配置的示例,以便只需copy, paste, rename to .env
. 即可重现
在名为
settings.py
的文件中,使用decouple.config
将您的配置文件读入变量:from decouple import config MONGO_URL = config('MONGO_URL') CONFIG_OPTION_1 = config('CONFIG_OPTION_1', default='') DEBUG = config('DEBUG', cast=bool, default=True) ...
现在您可以在实施所需的任何地方使用这些变量:
myblueprint.py
:import settings ... auth_route = Blueprint('authentication') mongo_url = settings.MONGO_URL user_repository = UserRepository(mongo_url) ...
作为终结者,我想指出这个方法是框架(甚至语言)不可知所以你可以使用它出现在 Sanic
和 Flask
以及您需要它的任何地方!
Sanic 方式...
在视图方法中,您可以从 request
对象访问 app
实例。并且,因此访问您的配置。
@auth_route.route('/signin')
async def redirect_user(request):
configuration = request.app.config
2021-10-10更新
有两种更新的方法来获取配置值(或者,也许更准确地获取您可以从中获取配置的应用程序实例)。第一个版本可能更准确地回答了如何从蓝图 获取配置的问题。但是,第二个选项可能是 首选 方法,因为它正是为这种用途而设计的。
选择#1
从 v21.3 开始,蓝图可以访问它们附加到 的 Sanic 应用程序。
因此,如果您有蓝图对象,则可以将其追溯到应用程序实例,因此也可以追溯到配置值。
app = Sanic("MyApp")
bp = Blueprint("MyBlueprint")
app.blueprint(bp)
assert bp.apps[0] is app
Blueprint.apps
属性 是 set
因为可以将单个蓝图附加到多个应用程序。
选择#2
从 v20.12 开始,Sanic 有一个从全局范围 检索应用程序实例的内置方法。这意味着应用程序实例化后,您可以使用以下方法检索它:Sanic.get_app()
.
app = Sanic("MyApp")
assert Sanic.get_app() is app
此方法仅在有单个 Sanic
实例可用时才有效。如果您有多个应用程序实例,则需要使用可选的 name
参数:
app1 = Sanic("MyApp")
app2 = Sanic("MyOtherApp")
assert Sanic.get_app("MyApp") is app1