|
1
|
|
|
# ==================================================================================== # |
|
2
|
|
|
# helpers.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 aiohttp_swagger3 import SwaggerFile |
|
29
|
|
|
from aiohttp_swagger3 import SwaggerUiSettings |
|
30
|
|
|
from aiohttp import web |
|
31
|
|
|
import aiohttp_cors |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
# ==================================================================================== # |
|
35
|
|
|
def create_swagger(app, spec): |
|
36
|
|
|
app['storage'] = dict() |
|
37
|
|
|
swagger = SwaggerFile( |
|
38
|
|
|
app=app, |
|
39
|
|
|
spec_file=str(spec), |
|
40
|
|
|
swagger_ui_settings=SwaggerUiSettings(path="/"), |
|
41
|
|
|
validate=False |
|
42
|
|
|
) |
|
43
|
|
|
return swagger |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
# ------------------------------------------------------------------------------------ # |
|
47
|
|
|
def create_cors(app): |
|
48
|
|
|
options = aiohttp_cors.ResourceOptions( |
|
49
|
|
|
allow_credentials=True, |
|
50
|
|
|
expose_headers="*", |
|
51
|
|
|
allow_headers="*" |
|
52
|
|
|
) |
|
53
|
|
|
cors = aiohttp_cors.setup( |
|
54
|
|
|
app=app, |
|
55
|
|
|
defaults={'*': options} |
|
56
|
|
|
) |
|
57
|
|
|
return cors |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
# ------------------------------------------------------------------------------------ # |
|
61
|
|
|
def create_site(runner, settings): |
|
62
|
|
|
site = web.TCPSite( |
|
63
|
|
|
runner=runner, |
|
64
|
|
|
host=settings['host'], |
|
65
|
|
|
port=settings['port'], |
|
66
|
|
|
backlog=settings['backlog'] |
|
67
|
|
|
) |
|
68
|
|
|
return site |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
# ------------------------------------------------------------------------------------ # |
|
72
|
|
|
def notify_user(settings): |
|
73
|
|
|
host = settings['host'] |
|
74
|
|
|
port = settings['port'] |
|
75
|
|
|
msg = f'Running YFrake server at: http://{host}:{port}' |
|
76
|
|
|
sep = '-' * len(msg) |
|
77
|
|
|
print(sep + '\n' + msg + '\n' + sep) |
|
78
|
|
|
|