You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.7 KiB
83 lines
1.7 KiB
import falcon, json
|
|
import logging
|
|
from logging import config
|
|
from datetime import datetime
|
|
|
|
|
|
class Message:
|
|
|
|
def __init__(self,user, text):
|
|
|
|
self.user = user
|
|
self.text = text
|
|
self.created = datetime.now()
|
|
|
|
|
|
def __str__(self):
|
|
return self.text
|
|
|
|
class ChatServer(object):
|
|
|
|
def __init__(self):
|
|
self.msglist = {}
|
|
|
|
initmsg = Message('system','Chat Server bereit')
|
|
|
|
self.addMsg(initmsg)
|
|
|
|
def addMsg(self,msg):
|
|
created = datetime.now()
|
|
self.msglist[created.strftime('%d.%m.%y %H:%M.%f')] = msg
|
|
|
|
def on_get(self,req,resp):
|
|
logging.debug('on_get()')
|
|
|
|
logging.debug(f'msglist= {self.msglist}')
|
|
json_msgs = {}
|
|
|
|
for key in self.msglist:
|
|
text = self.msglist[key].text
|
|
json_msgs[key] = {
|
|
'key': key,
|
|
'user': self.msglist[key].user,
|
|
'text': text
|
|
}
|
|
|
|
resp.status = falcon.HTTP_OK
|
|
resp.media = json_msgs
|
|
|
|
def on_post(self,req,resp):
|
|
logging.debug('on_post()')
|
|
|
|
json_string = req.media
|
|
|
|
logging.debug(f'json_string={json_string}')
|
|
|
|
msg = Message(json_string['user'],json_string['text'])
|
|
self.addMsg(msg)
|
|
|
|
|
|
resp.status = falcon.HTTP_OK
|
|
|
|
def on_delete(self,req,resp):
|
|
logging.debug('on_delete()')
|
|
|
|
|
|
def create_chat_resource():
|
|
chat_endpoint = ChatServer()
|
|
|
|
logging.debug(f'endpoint={chat_endpoint}')
|
|
|
|
return chat_endpoint
|
|
|
|
|
|
application = falcon.App()
|
|
|
|
with open('log_config.json') as file_config:
|
|
config.dictConfig(json.load(file_config))
|
|
|
|
resource = create_chat_resource()
|
|
application.add_route('/msg', resource)
|
|
logging.debug('Chat REST Service bereit')
|
|
|
|
|
|
|