forked from ArgLab/writing_observer
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebapp_helpers.py
More file actions
127 lines (105 loc) · 3.48 KB
/
Copy pathwebapp_helpers.py
File metadata and controls
127 lines (105 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'''
This file contains assorted middlewares and helpers
'''
import errno
import pmss
import socket
import aiohttp_cors
import aiohttp_session
import aiohttp_session.cookie_storage
import learning_observer.auth
from learning_observer.log_event import debug_log
import learning_observer.settings as settings
pmss.register_field(
name='session_secret',
type=pmss.TYPES.passwordtoken,
description='Unique secret key for YOUR deployment to encrypt/decrypt '\
'data stored in the session object.',
required=True
)
pmss.register_field(
name='session_max_age',
type=pmss.TYPES.integer,
description='Max age of a session in seconds.',
required=True
)
async def request_logger_middleware(request, handler):
'''
Print all hits. Helpful for debugging. Should eventually go into a
log file.
'''
debug_log(request)
async def add_nocache_middleware(request, response):
'''
This prevents the browser from caching pages.
Browsers do wonky things when logging in / out, keeping old pages
around. Caching generally seems like a train wreck for this system.
There's a lot of cleanup we can do to make this more robust, but
for now, this is a good enough solution.
'''
if '/static/' not in str(request.url):
response.headers['cache-control'] = 'no-cache'
def setup_middlewares(app):
'''
This is a helper function to setup middlewares.
'''
app.on_response_prepare.append(request_logger_middleware)
# Avoid caching. We should be more specific about what we want to
# cache.
app.on_response_prepare.append(add_nocache_middleware)
app.middlewares.append(learning_observer.auth.auth_middleware)
def setup_session_storage(app):
'''
This is a helper function to setup session storage.
'''
protocol = settings.pmss_settings.protocol()
cookie_params = {}
if protocol == 'https':
debug_log('Setting cookie parameters for HTTPS')
cookie_params = {
'domain': settings.pmss_settings.hostname(),
'secure': True,
'samesite': 'None'
}
aiohttp_session.setup(app, aiohttp_session.cookie_storage.EncryptedCookieStorage(
learning_observer.auth.fernet_key(settings.pmss_settings.session_secret(types=['aio'])),
max_age=settings.pmss_settings.session_max_age(types=['aio']),
**cookie_params))
def find_open_port():
"""
Find an open port to run on.
By default, run on port 8888. If in use, move up ports, until we find
one that is not in use.
Returns:
int: The open port.
"""
port = 8888
bound = False
while not bound:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(("127.0.0.1", port))
bound = True
except socket.error as e:
if e.errno == errno.EADDRINUSE:
bound = False
port = port + 1
else:
raise
s.close()
return port
def setup_cors(app):
'''
This is a helper function to setup CORS.
This setup is overly broad. We need this for incoming events
and similar, but we don't want to expose the entire API
through this as we do here.
TODO: Handle auth / auth more specifically on individual routes.
'''
cors = aiohttp_cors.setup(app, defaults={
"*": aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
)
})