diff options
-rw-r--r-- | back/app.py | 17 | ||||
-rw-r--r-- | back/config.py | 4 |
2 files changed, 14 insertions, 7 deletions
diff --git a/back/app.py b/back/app.py index 0d7bbee..2b8cf6f 100644 --- a/back/app.py +++ b/back/app.py @@ -19,6 +19,7 @@ if is_debug: def now_ms() -> int: from datetime import datetime + return floor(datetime.utcnow().timestamp() * 1000) @@ -33,7 +34,7 @@ class Todo: def marshal(self) -> str: self_dict = dict() for field in fields(self): - self_dict.update({ field.name: getattr(self, field.name) }) + self_dict.update({field.name: getattr(self, field.name)}) return json.dumps(self_dict) @classmethod @@ -49,7 +50,7 @@ class Todo: # types representation if type(json_obj[field.name]) == field.type: raise TypeError(f"Todo.{field.name} must be instance of {field.type}") - self_dict.update({ field.name: json_obj[field.name] }) + self_dict.update({field.name: json_obj[field.name]}) cls.verify_json(self_dict) return cls(**self_dict) @@ -71,12 +72,15 @@ class Todo: async def init_redis(app, loop): app.db = await create_redis_pool("redis://localhost") + @app.listener("after_server_stop") async def close_redis(app, loop): from asyncio import sleep + app.db.close() await app.db.wait_closed() + @app.post("/new-todo") async def new_todo(req): todo = Todo.unmarshal_safe(req.json) @@ -85,21 +89,24 @@ async def new_todo(req): return response_json(dict(status="ok", id=id)) + @app.post("/delete-todo") async def delete_todo(req): id = req.json["id"] await req.app.db.delete(f"upnet:todo:{id}") return response_json(dict(status="ok", was=id)) + @app.get("/todos") async def get_todos(req): all_todos_keys = await req.app.db.keys("upnet:todo:*") # TODO: await all together - todos = [asdict(await Todo.from_redis(req.app.db, todo_id)) - for todo_id in all_todos_keys + todos = [ + asdict(await Todo.from_redis(req.app.db, todo_id)) for todo_id in all_todos_keys ] - return response_json({ "todos": todos }) + return response_json({"todos": todos}) + if __name__ == "__main__": app.go_fast(host="0.0.0.0", port=8000, debug=is_debug) diff --git a/back/config.py b/back/config.py index 9c5b6fd..367e24f 100644 --- a/back/config.py +++ b/back/config.py @@ -4,11 +4,11 @@ import sys modpath = __file__ # Turn pyc files into py files if we can -if modpath.endswith('.pyc') and os.path.exists(modpath[:-1]): +if modpath.endswith(".pyc") and os.path.exists(modpath[:-1]): modpath = modpath[:-1] # Sort out symlinks APP_DIR = os.path.realpath(os.path.dirname(modpath)) -REDIS_HOST = 'localhost' +REDIS_HOST = "localhost" debug = True |