def __init__(self): self._app = web.Application(middlewares=[error_handler])
from gino.ext.aiohttp import Gino # Database Configuration PG_URL = "postgresql://{user}:{password}@{host}:{port}/{database}".format( host=os.getenv("DB_HOST", "localhost"), port=os.getenv("DB_PORT", 5432), user=os.getenv("DB_USER", "postgres"), password=os.getenv("DB_PASSWORD", ""), database=os.getenv("DB_DATABASE", "postgres"), ) # Initialize Gino instance db = Gino() # Initialize aiohttp app app = web.Application(middlewares=[db]) db.init_app(app, dict(dsn=PG_URL)) # Definition of table class User(db.Model): __tablename__ = "simple_aiohttp_demo_users" id = db.Column(db.BigInteger(), primary_key=True) nickname = db.Column(db.Unicode(), default="unnamed") # Definition of routes routes = web.RouteTableDef()
def start_web(conf: config.Config, slots: cgroup.SlotManager) -> None: async def shutdown() -> None: print("letting runner do cleanup") await runner.cleanup() def sigusr1_handler() -> None: print("Received SIGUSR1, shutting down...") loop.create_task(shutdown()) async def stop_loop(app: web.Application) -> None: print("shutdown") loop.stop() db = DB(conf) async def start_runner(runner: web.AppRunner, conf: config.Config) -> None: await db.init() await runner.setup() site: Optional[Union[web.TCPSite, web.UnixSite, web.SockSite]] = None if conf.port is not None: print(f"Starting HTTP server on localhost:{conf.port}") site = web.TCPSite(runner, 'localhost', conf.port) elif conf.socket is not None: print(f"Starting UNIX socket server on {conf.socket}") site = web.UnixSite(runner, conf.socket) elif conf.socket_fd is not None: print(f"Starting UNIX socket server on FD {conf.socket_fd}") sock = socket.socket(fileno=conf.socket_fd) site = web.SockSite(runner, sock) assert site is not None, "Invalid config, no listening address" await site.start() app = web.Application() templates_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates") aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(templates_dir)) eval_sem = asyncio.BoundedSemaphore(conf.max_workers) handle_is = get_eval_handler(eval_sem, conf, slots, db, InterfaceMode.IS | InterfaceMode.Priviledged) app.router.add_get("/is", handle_is) app.router.add_post("/is", handle_is) handle_hint = get_eval_handler(eval_sem, conf, slots, db, InterfaceMode.Null) app.router.add_get("/hint", handle_hint) app.router.add_post("/hint", handle_hint) handle_internal = get_eval_handler(eval_sem, conf, slots, db, InterfaceMode.Priviledged) app.router.add_get("/internal", handle_internal) app.router.add_post("/internal", handle_internal) app.router.add_get( "/update-qdir/{course_id}", # noqa: FS003 get_handle_update(conf)) app.router.add_post( "/update-qdir/{course_id}", # noqa: FS003 get_handle_update(conf)) handle_admin = get_handle_admin(conf) app.router.add_get("/admin/{user}/", handle_admin) # noqa: FS003 app.router.add_get("/admin/{user}/{course_id}/", handle_admin) # noqa: FS003, E501 app.router.add_post("/admin/{user}/{course_id}/", handle_admin) # noqa: FS003, E501 app.router.add_get("/admin/{user}/{course_id}/{page}", handle_admin) # noqa: FS003, E501 app.router.add_post("/admin/{user}{course_id}/{page}", handle_admin) # noqa: FS003, E501 runner = web.AppRunner(app, handle_signals=True) app.on_cleanup.append(stop_loop) loop = asyncio.get_event_loop() loop.add_signal_handler(signal.SIGUSR1, sigusr1_handler) try: loop.run_until_complete(start_runner(runner, conf)) except Exception: print("ERROR starting server", file=sys.stderr) traceback.print_exc() sys.exit(1) print("started, loaded following configuration:") conf.dump(sys.stdout) try: loop.run_forever() finally: loop.close()
async def request_handler(context: TurnContext) -> web.Response: if context.request.type == 'message': return await handle_message(context) elif context.request.type == 'conversationUpdate': return await handle_conversation_update(context) else: return await unhandled_activity() async def messages(req: web.web_request) -> web.Response: body = await req.json() activity = Activity().deserialize(body) auth_header = req.headers[ 'Authorization'] if 'Authorization' in req.headers else '' try: return await ADAPTER.process_request(activity, auth_header, request_handler) except Exception as e: raise e app = web.Application() app.router.add_post('/', messages) try: web.run_app(app, host='localhost', port=PORT) except Exception as e: raise e
import asyncio from aiohttp import web from weixin.msg import WeixinMsg app = web.Application(loop=asyncio.get_event_loop()) msg = WeixinMsg("qwertyuiop", None, 0) async def res_msg(request): print('text: ', await request.text()) return await msg.aio_view_func(request) app.add_routes([web.get('/api/wx_msg', res_msg), web.post('/api/wx_msg', res_msg)]) @msg.text() def msg_text(**kwargs): print('text: ', kwargs) return 'succ' @msg.text("help") def msg_help(**kwargs): print('text help: ', kwargs) return 'help' @msg.click def msg_click(**kwargs): print('click: ', kwargs) return dict(content="Welcome", type="text")
from functools import partial import websockets from aiohttp import web import asyncio # Dies ist ein Kommentar. Überrascht? loop = asyncio.get_event_loop() # Eine AbstractEventLoop app = web.Application(loop=loop) # web.Application router = app.router # Setze "router" auf "app.router" async def handle_hello(req): # Definiere eine Coro return web.Response(text="Hello") # Gib "Hello" zurück router.add_route("GET", "/test", handle_hello) async def stop(req): exit(1) router.add_route("GET", "/close", stop) # Erstelle Route "close" async def fabian(req): # test file = open("./webapp/fabian.html", "rb").readlines() return web.Response(text=file, content_type="text/html") router.add_route("GET", "/fabian", fabian) # WOOW PARTIALS! handler = app.make_handler() # Erstelle einen Handler future = loop.create_server(handler, "localhost", 8080) # EIN SERVER! loop.create_task(future) # Starte den Server async def handle_ws(ws, path): async for message in ws: print(message)
def init(loop): app = web.Application(loop=loop) app.router.add_route('GET', '/', index) srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000) logging.info('server started at http://127.0.0.1:9000...') return srv
def app(loop): return web.Application(loop=loop)
# You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import hashlib import json import logging import os import shutil import tarfile import tempfile import zipfile from aiohttp import web #: Application instance APP = web.Application() #: Shortcut for a bad http request BAD_REQUEST = web.Response(status=400, text='{"error": "bad request"}') #: Shortcut for an OK http request OK = web.Response(status=200) #: All supported upload file types and their opener SUPPORTED_FILE_TYPES = { '.whl': zipfile.ZipFile, '.egg': zipfile.ZipFile, '.zip': zipfile.ZipFile, '.tar.gz': tarfile.open, '.tar.bz2': tarfile.open, } #: All hashable file types HASHABLE_FILE_TYPES = (
def create_app(): app = web.Application(middlewares=[auth_middleware]) app.add_routes(auth) app.add_routes(api) return app
json_dict = json.load(f) settings = {k: v for k, v in json_dict.items() if k in key_filter} else: settings = {k: os.environ[k] for k in key_filter} return settings _settings = _load_settings() _credentials = Credentials.from_service_account_info({ 'token_uri': 'https://accounts.google.com/o/oauth2/token', 'client_email': _settings['GCLOUD_CLIENT_EMAIL'], 'private_key': _settings['GCLOUD_PRIVATE_KEY'], }) _gcs_client = gcs.Client(project=_settings['GCLOUD_PROJECT_ID'], credentials=_credentials) _loop = asyncio.get_event_loop() _max_workers = 1 _loop.set_default_executor(ProcessPoolExecutor(max_workers=_max_workers)) _manager = multiprocessing.Manager() _jobs_list = _manager.list() _app = web.Application(logger=_logger) _app.router.add_route('POST', '/jobs', _add_job) web.run_app(_app, port=10000)
def test_repr_for_application(loop): app = web.Application(loop=loop) assert "<Application 0x{:x}>".format(id(app)) == repr(app)
def app(loop): app = web.Application(loop=loop) setup_routes(app) app.on_startup.append(init_pg) app.on_cleanup.append(close_pg) return app
def run(self): # Windows-compatible, with subprocess support: if sys.platform.startswith('win'): loop = asyncio.ProactorEventLoop() asyncio.set_event_loop(loop) else: loop = asyncio.get_event_loop() html_app = web.Application() html_app.router.add_get('/vuespa.js', self._handle_vuespa_js) html_app.router.add_get('/vuespa.ws', self._handle_vuespa_ws) html_app.router.add_get('/{path:.*}', self._handle_vue) html_app.router.add_post('/{path:.*}', self._handle_vue) # Run the application on a randomly selected port (or specified port) if self._port is not None: self._port_vue = self._port + 1 html_server_handler = html_app.make_handler() html_server = loop.run_until_complete( loop.create_server(html_server_handler, self.host, self._port)) self._port = html_server.sockets[0].getsockname()[1] dist_exists = os.path.lexists(os.path.join(self._vue_path, 'dist')) needs_npm_mod = self._development or not dist_exists # Install node packages if no node_modules folder. if (needs_npm_mod and not os.path.lexists( os.path.join(self._vue_path, 'node_modules'))): node_install = loop.run_until_complete( asyncio.create_subprocess_shell('npm install', cwd=self._vue_path)) loop.run_until_complete(node_install.communicate()) promises = [] ui_proc = None if self._development: # Ensure node process is installed first. # BUT FIRST, work around excessive websocket closing. # See https://github.com/vuejs-templates/webpack/issues/1205 vue_config_path = os.path.join(self._vue_path, 'vue.config.js') with open(vue_config_path) as f: js_src = f.read() js_pat = r'(module\.exports *= *)(.*)' m = re.search(js_pat, js_src, flags=re.M | re.DOTALL) if m is not None: try: j = json.loads(m.group(2)) except: raise ValueError( f"Looks like {vue_config_path} has invalid JSON: {m.group(2)}" ) if not j.get('devServer', {}).get('disableHostCheck', False): j.setdefault('devServer', {}) j['devServer']['disableHostCheck'] = True with open(vue_config_path, 'w') as f: f.write( re.sub( js_pat, lambda m: m.group(1) + json.dumps(j, indent=2), js_src, flags=re.M | re.DOTALL)) ui_proc = loop.run_until_complete( asyncio.create_subprocess_shell( f"FORCE_COLOR=1 npx --no-install vue-cli-service serve", stdout=asyncio.subprocess.PIPE, # Leave stderr connected cwd=self._vue_path)) # We need to get the port first, so read lines from stdout until we # find that information. Then, communicate. async def streamer(stream_in, stream_out, re_stop=None): """Returns `None` on process exit, or a regex Match object. """ while True: line = await stream_in.readline() if not line: break line = line.decode() stream_out.write(line) if re_stop is not None: m = re.search(re_stop, line) if m is not None: return m m = loop.run_until_complete( streamer( ui_proc.stdout, sys.stdout, # Note that regex looks weird because we must strip color code re_stop=re.compile( '- Local: .*?http://[^:]+:\x1b\\[[^m]*m(?P<port>\d+)')) ) self._port_vue = int(m.group('port')) promises.append(streamer(ui_proc.stdout, sys.stdout)) promises.append(ui_proc.wait()) elif not dist_exists: # Build UI once, otherwise use cached version proc = loop.run_until_complete( asyncio.create_subprocess_shell('npm run build', cwd=self._vue_path)) loop.run_until_complete(proc.communicate()) webbrowser.open(f'http://{self.host}:{self.port}') try: # Terminate either when a child process terminates OR when a # KeyboardInterrupt is sent. if promises: loop.run_until_complete( asyncio.wait(promises, return_when=asyncio.FIRST_COMPLETED)) else: # Nothing will terminate early. loop.run_forever() except KeyboardInterrupt: pass finally: if ui_proc is not None: ui_proc.kill() html_server.close() loop.run_until_complete(html_server.wait_closed())
def app(): app = web.Application() setup_routes(app) web.run_app(app, host=config.IP_ADDRESS, port=config.PORT)
async def init(loop): app = web.Application(loop=loop) app.router.add_get('/simple', simple) return app
def run(self): app = web.Application() app.router.add_get('/', self.handle_get) app.router.add_post('/', self.handle_post) web.run_app(app, host=self.hostname, port=self.port)
async def setup_app(): app = web.Application() app.add_routes([web.post('/', handle_pdf_request)]) return app
def factory(loop, *args, **kwargs): app = web.Application(router=router, loop=loop, middlewares=[jsonify]) return app
async def make_app(host, port): app = web.Application() app['instance_id'] = os.getenv('INSTANCE_ID', '1') app['tasks'] = [] jaeger_address = os.getenv('JAEGER_ADDRESS') az.aiohttp_helpers._set_span_properties = _set_span_properties if jaeger_address: endpoint = az.create_endpoint( f"social_net_server_{app['instance_id']}", ipv4=host, port=port) tracer = await az.create(jaeger_address, endpoint, sample_rate=1.0) trace_config = az.make_trace_config(tracer) app['client_session'] = aiohttp.ClientSession( trace_configs=[trace_config]) else: app['client_session'] = aiohttp.ClientSession() async def close_session(app): await app["client_session"].close() app.on_cleanup.append(close_session) app.add_routes([ web.static('/static', STATIC_DIR), web.get("/", handle_index, name='index'), web.get("/login/", handle_login_get, name='login'), web.post("/login/", handle_login_post), web.get("/logout/", handle_logout_post), web.get("/register/", handle_register), web.post("/register/", handle_register), web.get("/userpage/", handle_userpage, name='user_page'), web.post("/chat/", handle_chat, name='chat_page'), web.get("/userpage/{uid}/", handle_userpage), web.get("/userlist/", hanlde_userlist), web.post("/add_friend/{uid}/", hanlde_add_friend), web.post("/del_friend/{uid}/", hanlde_del_friend), web.get("/newspage/", hanlde_newspage, name='news_page'), web.post("/add_post/", hanlde_add_post), web.view("/news_ws/", handle_news_ws), web.get('/api/user/', api_user.handle_user), ]) # secret_key must be 32 url-safe base64-encoded bytes fernet_key = os.getenv('FERNET_KEY', fernet.Fernet.generate_key()) secret_key = base64.urlsafe_b64decode(fernet_key) aiohttp_session.setup(app, EncryptedCookieStorage(secret_key)) logging.debug('fernet_key: %r secret_key: %r', fernet_key, secret_key) app.middlewares.append(check_login) aiohttp_jinja2.setup( app, loader=jinja2.FileSystemLoader(TEMPLATE_DIR), context_processors=[username_ctx_processor], ) database_url = os.getenv('CLEARDB_DATABASE_URL', None) or os.getenv( 'DATABASE_URL', None) pool = await aiomysql.create_pool(**extract_database_credentials( database_url), maxsize=50, autocommit=True) app['db_pool'] = pool app.on_shutdown.append(lambda _app: close_db_pool(_app['db_pool'])) databse_ro_url = os.getenv('DATABASE_RO_URL', None) if databse_ro_url: ro_pool = await aiomysql.create_pool(**extract_database_credentials( databse_ro_url), maxsize=50, autocommit=True) app['db_ro_pool'] = ro_pool app.on_shutdown.append(lambda _app: close_db_pool(_app['db_ro_pool'])) else: logging.warning('DATABASE_RO_URL not set') app['db_ro_pool'] = pool redis_url = os.getenv('REDIS_URL', None) if redis_url: app['arq_pool'] = await arq.create_pool( arq.connections.RedisSettings.from_dsn(redis_url)) async def close_arq_pool(_app): _app['arq_pool'].close() await _app['arq_pool'].wait_closed() app.on_shutdown.append(close_arq_pool) tarantool_url = os.getenv('TARANTOOL_URL', None) if tarantool_url: app['tnt'] = asynctnt.Connection( **extract_tarantool_credentials(tarantool_url)) await app['tnt'].connect() app.on_shutdown.append(app['tnt'].disconnect) rabbit_url = os.getenv('CLOUDAMQP_URL', os.getenv('RABBIT_URL', None)) if rabbit_url: connection: aio_pika.Connection = await aio_pika.connect_robust( rabbit_url) app['rabbit'] = connection # await connection.channel() await start_background_task(app, listen_news_updates(app)) app['news_subscribers'] = defaultdict(dict) app.on_shutdown.append(stop_tasks) if jaeger_address: az.setup(app, tracer) await migrate_schema(pool) return app
async def init(): app = web.Application() app.router.add_route("GET", "/{name}", handle) return await loop.create_server(app.make_handler(), "127.0.0.1", 8080)
async def build() -> web.Application: setup_logging() cookie_storage = EncryptedCookieStorage(**settings["session"]["cookie"]) openapi_route = settings["openapi"]["route"] app = web.Application() app["settings"] = settings current_path = Path(__file__).resolve().parent templates_path = str(current_path.joinpath('templates')) aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(templates_path)) app.router.add_get("/", get_swagger_ui_handler, name="swagger") app.router.add_get("/channels", get_channels_handler, name="channels") app.router.add_get("/history", get_history_handler, name="history") app.router.add_get("/history/events", get_history_events_handler, name="history_events") app.router.add_get("/user", get_user_handler, name="user") app.router.add_get("/user/sign_out", get_user_sign_out_handler, name="user_sign_out") app.router.add_get("/user/google", get_user_google_handler, name="user_google") app.router.add_get("/user/google/callback", get_user_google_callback_handler, name="user_google_callback") app.router.add_get("/user/bookmarks", get_user_bookmarks_handler, name="user_bookmarks") app.router.add_post("/user/bookmarks", post_user_bookmarks_handler, name="post_user_bookmarks") app.router.add_delete("/user/bookmarks/{bookmark_id}", delete_user_bookmarks_handler, name="delete_user_bookmarks") app.router.add_get(openapi_route["url"], get_openapi_handler, name=openapi_route["name"]) app.middlewares.append(aiohttp_session.session_middleware(cookie_storage)) app.middlewares.append(cors_middleware) app.middlewares.append(csrf_middleware) app.on_response_prepare.append(set_cors) app.on_response_prepare.append(transform_client_exception_to_json) app.on_startup.append(create_postgres_connection_pool) app.on_startup.append(create_redis_connection_pool) app.on_startup.append(create_sse_redis_subscriber) app.on_startup.append(generate_openapi_spec) app.on_shutdown.append(close_sse_streams) app.on_cleanup.append(cancel_sse_redis_subscriber) app.on_cleanup.append(close_redis_connection_pool) app.on_cleanup.append(close_postgres_connection_pool) return app
def _make_web_app(): for path in settings.MIDDLEWARE: middleware.append(utils.import_member(path)) return web.Application(middlewares=middleware, client_max_size=settings.MAX_REQUEST_BODY_SIZE)
async def init(loop): app = web.Application(loop=loop, middlewares=[response_factory]) add_routes(app, 'coroweb_test') srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000) logging.info('server started at http://127.0.0.1:9000...') return srv