yfrake.server.runner.server_runner()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 21
nop 1
dl 0
loc 27
rs 9.376
c 0
b 0
f 0
1
# ==================================================================================== #
2
#    runner.py - This file is part of the YFrake package.                              #
3
# ------------------------------------------------------------------------------------ #
4
#                                                                                      #
5
#    MIT License                                                                       #
6
#                                                                                      #
7
#    Copyright (c) 2022 Mattias Aabmets                                                #
8
#                                                                                      #
9
#    Permission is hereby granted, free of charge, to any person obtaining a copy      #
10
#    of this software and associated documentation files (the "Software"), to deal     #
11
#    in the Software without restriction, including without limitation the rights      #
12
#    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell         #
13
#    copies of the Software, and to permit persons to whom the Software is             #
14
#    furnished to do so, subject to the following conditions:                          #
15
#                                                                                      #
16
#    The above copyright notice and this permission notice shall be included in all    #
17
#    copies or substantial portions of the Software.                                   #
18
#                                                                                      #
19
#    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR        #
20
#    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,          #
21
#    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE       #
22
#    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER            #
23
#    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,     #
24
#    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     #
25
#    SOFTWARE.                                                                         #
26
#                                                                                      #
27
# ==================================================================================== #
28
from yfrake.openapi.utils import get_spec_file_path
29
from yfrake.config.utils import get_runtime_args
30
from yfrake.server.handler import handler
31
from yfrake.server import helpers
32
from yfrake import client, config
33
from aiohttp import web
34
import asyncio
35
36
37
# ==================================================================================== #
38
@client.session
39
async def server_runner(run_forever=False):
40
    settings = config.settings['server']
41
    spec = get_spec_file_path()
42
    app = web.Application()
43
44
    swagger = helpers.create_swagger(app, spec)
45
    routes = [web.get('/{any}', handler)]
46
    swagger.add_routes(routes)
47
48
    cors = helpers.create_cors(app)
49
    routes = list(app.router.routes())
50
    for route in routes:
51
        cors.add(route)
52
53
    runner = web.AppRunner(app=app)
54
    await runner.setup()
55
56
    site = helpers.create_site(runner, settings)
57
    await site.start()
58
59
    helpers.notify_user(settings)
60
    while run_forever:  # pragma: no cover
61
        await asyncio.sleep(3600)
62
63
    await site.stop()
64
    await runner.cleanup()
65
66
67
# ------------------------------------------------------------------------------------ #
68
if __name__ == '__main__':
69
    args = get_runtime_args()
70
    config.file = args.get('config_file')
71
    coro = server_runner(run_forever=True)
72
    asyncio.run(coro)
73