2015-09-02 12:23:01 +03:00
|
|
|
"""`@di.inject()` decorator and Flask view example."""
|
2015-08-05 17:01:23 +03:00
|
|
|
|
|
|
|
import sqlite3
|
2015-09-01 19:08:35 +03:00
|
|
|
import flask
|
|
|
|
import dependency_injector as di
|
2015-08-05 17:01:23 +03:00
|
|
|
|
|
|
|
|
2015-10-23 15:04:04 +03:00
|
|
|
database = di.Singleton(sqlite3.connect,
|
|
|
|
':memory:',
|
2015-09-01 19:08:35 +03:00
|
|
|
timeout=30,
|
|
|
|
detect_types=True,
|
|
|
|
isolation_level='EXCLUSIVE')
|
2015-08-05 17:01:23 +03:00
|
|
|
|
2015-09-01 19:08:35 +03:00
|
|
|
app = flask.Flask(__name__)
|
2015-08-05 17:01:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/')
|
2015-10-23 15:04:04 +03:00
|
|
|
@di.inject(database)
|
2015-10-23 18:07:52 +03:00
|
|
|
@di.inject(flask.request)
|
|
|
|
def hello(request, database):
|
2015-08-05 17:01:23 +03:00
|
|
|
"""Example Flask view."""
|
2015-10-23 18:07:52 +03:00
|
|
|
print request
|
2015-08-05 17:01:23 +03:00
|
|
|
one = database.execute('SELECT 1').fetchone()[0]
|
|
|
|
return 'Query returned {0}, db connection {1}'.format(one, database)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run()
|
|
|
|
|
|
|
|
# Example output of "GET / HTTP/1.1" is:
|
|
|
|
# Query returned 1, db connection <sqlite3.Connection object at 0x1057e4030>
|