From 4bdae47ab86a1b035f8419f02c084ea9fc143d11 Mon Sep 17 00:00:00 2001 From: superstar54 Date: Sat, 9 Dec 2023 10:54:37 +0000 Subject: [PATCH] use FastAPI to create a backend --- aiida_worktree/web/backend/app/__init__.py | 0 aiida_worktree/web/backend/app/api.py | 43 ++++++++++++++++++++++ aiida_worktree/web/backend/app/utils.py | 20 ++++++++++ aiida_worktree/web/backend/main.py | 5 +++ 4 files changed, 68 insertions(+) create mode 100644 aiida_worktree/web/backend/app/__init__.py create mode 100644 aiida_worktree/web/backend/app/api.py create mode 100644 aiida_worktree/web/backend/app/utils.py create mode 100644 aiida_worktree/web/backend/main.py diff --git a/aiida_worktree/web/backend/app/__init__.py b/aiida_worktree/web/backend/app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/aiida_worktree/web/backend/app/api.py b/aiida_worktree/web/backend/app/api.py new file mode 100644 index 00000000..b1b5c6d6 --- /dev/null +++ b/aiida_worktree/web/backend/app/api.py @@ -0,0 +1,43 @@ +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware +from aiida import load_profile, orm + +load_profile() + +app = FastAPI() + +origins = ["http://localhost:3000", "localhost:3000"] + + +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + + +@app.get("/", tags=["root"]) +async def read_root() -> dict: + return {"message": "Welcome to your todo list."} + + +@app.get("/worktree/{id}") +async def read_item(id: str): + from fastapi import HTTPException + from .utils import worktree_to_json + + try: + node = orm.load_node(id) + from aiida.orm.utils.serialize import deserialize_unsafe + + wtdata = node.base.extras.get("worktree", None) + if wtdata is None: + print("No worktree data found in the node.") + return + wtdata = deserialize_unsafe(wtdata) + content = worktree_to_json(wtdata) + return {"id": id, "content": content} + except KeyError: + raise HTTPException(status_code=404, detail=f"Worktree {id} not found") diff --git a/aiida_worktree/web/backend/app/utils.py b/aiida_worktree/web/backend/app/utils.py new file mode 100644 index 00000000..1268a40d --- /dev/null +++ b/aiida_worktree/web/backend/app/utils.py @@ -0,0 +1,20 @@ +def worktree_to_json(wtdata): + """Export a worktree to a JSON serializable dictionary.""" + wtdata_short = { + "name": wtdata["name"], + "uuid": wtdata["uuid"], + "state": wtdata["state"], + "nodes": {}, + "links": wtdata["links"], + } + for name, node in wtdata["nodes"].items(): + wtdata_short["nodes"][name] = { + "name": node["name"], + "identifier": node["metadata"]["identifier"], + "node_type": node["metadata"]["node_type"], + "uuid": node["uuid"], + "state": node["state"], + "action": node["action"], + "inputs": node["inputs"], + } + return wtdata_short diff --git a/aiida_worktree/web/backend/main.py b/aiida_worktree/web/backend/main.py new file mode 100644 index 00000000..19942641 --- /dev/null +++ b/aiida_worktree/web/backend/main.py @@ -0,0 +1,5 @@ +import uvicorn + + +if __name__ == "__main__": + uvicorn.run("app.api:app", host="0.0.0.0", port=8000, reload=True)