async def sa_app(db: Gino) -> Quart: app = Quart("test_sa") app.config["DB_DSN"] = "postgres://ssfdust@localhost/my-smorest-testing" db.init_app(app) for func in app.before_first_request_funcs: await func() async with app.app_context(): await db.gino.create_all() yield app await db.gino.drop_all()
async def _app(config): app = Quart(__name__) app.config.update(config) db = Gino(app) class User(db.Model): __tablename__ = 'gino_users' id = db.Column(db.BigInteger(), primary_key=True) nickname = db.Column(db.Unicode(), default='noname') @app.route('/') async def root(): return 'Hello, world!' async def _get_user(ctx, uid: int, method: str) -> dict: q = User.query.where(User.id == uid) if method == '1': return (await q.gino.first_or_404()).to_dict() elif method == '2' and ctx: return (await ctx.connection.first_or_404(q)).to_dict() elif method == '3': return (await db.bind.first_or_404(q)).to_dict() elif method == '4': return (await db.first_or_404(q)).to_dict() else: return (await User.get_or_404(uid)).to_dict() @app.route('/users/<int:uid>') async def get_user(uid): method = request.args.get('method') return jsonify(await _get_user(request, uid, method)) async def _add_user(ctx, nickname: str) -> dict: u = await User.create(nickname=nickname) await u.query.gino.first_or_404() await db.first_or_404(u.query) await db.bind.first_or_404(u.query) if ctx: await ctx.connection.first_or_404(u.query) return u.to_dict() @app.route('/users', methods=['POST']) async def add_user(): return jsonify(await _add_user( request, (await request.form).get('name'))) @app.websocket('/ws') async def ws(): while True: data = json.loads(await websocket.receive()) action = data.get('action') if action == 'add': new_user = await _add_user(None, data.get('name')) await websocket.send(json.dumps(new_user)) elif action == 'get': try: user = await _get_user( None, int(data.get('id')), data.get('method')) except NotFound: await websocket.send(json.dumps({'error': 'not found'})) else: await websocket.send(json.dumps(user)) else: await websocket.send(json.dumps({'error': 'Invalid JSON'})) e = await gino.create_engine(PG_URL) try: try: await db.gino.create_all(e) await yield_(app) finally: await db.gino.drop_all(e) finally: await e.close()
async def _app(config): app = Quart(__name__) app.config.update(config) app.config.update( { "DB_KWARGS": dict( max_inactive_connection_lifetime=_MAX_INACTIVE_CONNECTION_LIFETIME, ), } ) db = Gino(app) class User(db.Model): __tablename__ = "gino_users" id = db.Column(db.BigInteger(), primary_key=True) nickname = db.Column(db.Unicode(), default="noname") @app.route("/") async def root(): conn = await request.connection.get_raw_connection() # noinspection PyProtectedMember assert conn._holder._max_inactive_time == _MAX_INACTIVE_CONNECTION_LIFETIME return "Hello, world!" async def _get_user(ctx, uid: int, method: str) -> dict: q = User.query.where(User.id == uid) if method == "1": return (await q.gino.first_or_404()).to_dict() elif method == "2" and ctx: return (await ctx.connection.first_or_404(q)).to_dict() elif method == "3": return (await db.bind.first_or_404(q)).to_dict() elif method == "4": return (await db.first_or_404(q)).to_dict() else: return (await User.get_or_404(uid)).to_dict() @app.route("/users/<int:uid>") async def get_user(uid): method = request.args.get("method") return jsonify(await _get_user(request, uid, method)) async def _add_user(ctx, nickname: str) -> dict: u = await User.create(nickname=nickname) await u.query.gino.first_or_404() await db.first_or_404(u.query) await db.bind.first_or_404(u.query) if ctx: await ctx.connection.first_or_404(u.query) return u.to_dict() @app.route("/users", methods=["POST"]) async def add_user(): return jsonify(await _add_user(request, (await request.form).get("name"))) @app.websocket("/ws") async def ws(): while True: data = json.loads(await websocket.receive()) action = data.get("action") if action == "add": new_user = await _add_user(None, data.get("name")) await websocket.send(json.dumps(new_user)) elif action == "get": try: user = await _get_user( None, int(data.get("id")), data.get("method") ) except NotFound: await websocket.send(json.dumps({"error": "not found"})) else: await websocket.send(json.dumps(user)) else: await websocket.send(json.dumps({"error": "Invalid JSON"})) e = await gino.create_engine(PG_URL) try: try: await db.gino.create_all(e) yield app finally: await db.gino.drop_all(e) finally: await e.close()
"""main entry point for the app""" from gino.ext.quart import Gino from quart import Quart from quart_cors import cors app = Quart(__name__) # load config app.config.from_pyfile('settings.py') # set cors app = cors(app) # set the database handler db = Gino(app) # import and register blueprints from routes import simple_app # noqa, prevent circular imports app.register_blueprint(simple_app)
#!/usr/bin/env python # -*- coding: utf-8 -*- from quart import Quart from gino.ext.quart import Gino db = Gino()