mirror of
https://github.com/avatao-content/test-tutorial-framework
synced 2025-04-04 12:02:41 +00:00
Replace TFWConnector with PipeIO in the webservice
This commit is contained in:
parent
1b53222937
commit
9d9021d01d
@ -1,11 +1,5 @@
|
|||||||
FROM eu.gcr.io/avatao-challengestore/tutorial-framework
|
FROM eu.gcr.io/avatao-challengestore/tutorial-framework
|
||||||
|
|
||||||
# Install webservice dependencies
|
|
||||||
RUN pip3 install Flask==1.0 \
|
|
||||||
SQLAlchemy==1.2.7 \
|
|
||||||
passlib==1.7.1 \
|
|
||||||
git+https://github.com/avatao-content/tfwconnector.git#subdirectory=python3
|
|
||||||
|
|
||||||
# Define variables to use later
|
# Define variables to use later
|
||||||
ENV TFW_EHMAIN_DIR="${TFW_DIR}/builtin_event_handlers" \
|
ENV TFW_EHMAIN_DIR="${TFW_DIR}/builtin_event_handlers" \
|
||||||
TFW_WEBSERVICE_DIR="/srv/webservice" \
|
TFW_WEBSERVICE_DIR="/srv/webservice" \
|
||||||
|
69
solvable/src/webservice/pipe_io.py
Normal file
69
solvable/src/webservice/pipe_io.py
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
|
||||||
|
class PipeReader:
|
||||||
|
def __init__(self, pipe_path):
|
||||||
|
self._pipe = open(pipe_path, 'rb')
|
||||||
|
self._message_handler = lambda msg: None
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, type_, value, traceback):
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self._pipe.close()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def message_handler(self):
|
||||||
|
return self._message_handler
|
||||||
|
|
||||||
|
@message_handler.setter
|
||||||
|
def message_handler(self, value):
|
||||||
|
if not isinstance(value, Callable):
|
||||||
|
raise ValueError("message_handler must be callable!")
|
||||||
|
self._message_handler = value
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
msg = self.recv_message()
|
||||||
|
while msg:
|
||||||
|
self._message_handler(msg)
|
||||||
|
msg = self.recv_message()
|
||||||
|
|
||||||
|
def recv_message(self):
|
||||||
|
return self._pipe.readline()[:-1]
|
||||||
|
|
||||||
|
|
||||||
|
class PipeWriter:
|
||||||
|
def __init__(self, pipe_path):
|
||||||
|
self._pipe = open(pipe_path, 'wb')
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, type_, value, traceback):
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self._pipe.close()
|
||||||
|
|
||||||
|
def send_message(self, message):
|
||||||
|
self._pipe.write(message + b'\n')
|
||||||
|
self._pipe.flush()
|
||||||
|
|
||||||
|
|
||||||
|
class PipeIO:
|
||||||
|
def __init__(self, in_pipe_path, out_pipe_path):
|
||||||
|
self.reader = PipeReader(in_pipe_path)
|
||||||
|
self.writer = PipeWriter(out_pipe_path)
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, type_, value, traceback):
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.reader.close()
|
||||||
|
self.writer.close()
|
@ -1,7 +1,6 @@
|
|||||||
from functools import partial
|
from json import dumps
|
||||||
|
|
||||||
from tfwconnector import MessageSender
|
|
||||||
|
|
||||||
|
from pipe_io import PipeWriter
|
||||||
from crypto import PasswordHasher
|
from crypto import PasswordHasher
|
||||||
from model import User
|
from model import User
|
||||||
from errors import InvalidCredentialsError, UserExistsError
|
from errors import InvalidCredentialsError, UserExistsError
|
||||||
@ -12,11 +11,14 @@ class UserOps:
|
|||||||
self.username = username
|
self.username = username
|
||||||
self.password = password
|
self.password = password
|
||||||
self.db_session = db_session
|
self.db_session = db_session
|
||||||
self.message_sender = MessageSender()
|
self.pipe = PipeWriter('/tmp/tfw_json_send')
|
||||||
self.log = partial(
|
self.log = lambda message: self.pipe.send_message(dumps({
|
||||||
self.message_sender.send,
|
'key': 'message',
|
||||||
'Authenticator'
|
'data': {
|
||||||
)
|
'originator': 'Authenticator',
|
||||||
|
'message': message
|
||||||
|
}
|
||||||
|
}).encode())
|
||||||
|
|
||||||
def authenticate(self):
|
def authenticate(self):
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user