原文地址:https://sanicframework.org/en/guide/basics/app.html
# path/to/server.py
from sanic import Sanic
app = Sanic("My Hello, world app")
# Raises a warning as deprecated feature in 21.3
app = Sanic("MyApp")
app.db = Database()
# Correct way to attach objects to the application
app = Sanic("MyApp")
app.ctx.db = Database()
# ./path/to/server.py
from sanic import Sanic
app = Sanic("my_awesome_server")
# ./path/to/somewhere_else.py
from sanic import Sanic
app = Sanic.get_app("my_awesome_server")
app = Sanic.get_app(
"non-existing",
force_create=True,
)
Sanic("My only app")
app = Sanic.get_app()
app = Sanic('myapp')
app.config.DB_NAME = 'appdb'
app.config['DB_USER'] = 'appuser'
db_settings = {
'DB_HOST': 'localhost',
'DB_NAME': 'appdb',
'DB_USER': 'appuser'
}
app.config.update(db_settings)
app.config.GOOD = "yay!"
app.config.bad = "boo"