From 942aa86d8de74100583d8157e65435aac0f30491 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Grie=C3=9Fhaber?= <griesshaber@hdm-stuttgart.de>
Date: Mon, 13 May 2024 10:11:10 +0200
Subject: [PATCH] add frontend as submodule

---
 .gitmodules             |  3 +++
 api/__init__.py         |  0
 api/main.py             | 32 +++++++++++++++++++++++++-------
 api/routers/__init__.py |  0
 api/routers/runs.py     | 24 ++++++++++++++++++++++++
 frontend                |  1 +
 6 files changed, 53 insertions(+), 7 deletions(-)
 create mode 100644 .gitmodules
 create mode 100644 api/__init__.py
 create mode 100644 api/routers/__init__.py
 create mode 100644 api/routers/runs.py
 create mode 160000 frontend

diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..c3a6fa1
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "frontend"]
+	path = frontend
+	url = git@gitlab.mi.hdm-stuttgart.de:griesshaber/evotree.git
diff --git a/api/__init__.py b/api/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/api/main.py b/api/main.py
index 50a4437..3226ba9 100644
--- a/api/main.py
+++ b/api/main.py
@@ -1,12 +1,35 @@
-from fastapi import FastAPI, Request
+from fastapi import FastAPI, Request, Response
+from fastapi.staticfiles import StaticFiles
+from requests import request as make_request
+
+from api.routers import runs
 
 # from api.optimization import Backend
 
 DEBUG = True
 
 app = FastAPI(debug=DEBUG, title="Prompt Optimization Backend")
+app.include_router(runs.router, prefix="/api/runs")
+if DEBUG:
+
+    @app.get("/{_:path}")
+    async def frontent_dev(request: Request):
+        response = make_request(
+            method=request.method,
+            url=f"{request.url.replace(port=3000)}",
+            headers=request.headers,
+            stream=True,
+        )
+        return Response(
+            content=response.raw.read(),
+            status_code=response.status_code,
+            headers=response.headers,
+            media_type=response.headers["content-type"],
+        )
+
+else:
+    app.mount("/", StaticFiles(directory="frontend/build/", html=True), name="frontend")
 
-# api = Backend(debug=DEBUG)
 
 def test():
     pass
@@ -37,8 +60,3 @@ async def evolve(prompt1: str, prompt2: str) -> str:
 @app.get("/family_tree/get")
 async def get_family() -> dict:
     return dict()
-
-
-@app.get("/")
-def main(request: Request) -> str:
-    return "server running"
diff --git a/api/routers/__init__.py b/api/routers/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/api/routers/runs.py b/api/routers/runs.py
new file mode 100644
index 0000000..20b1daf
--- /dev/null
+++ b/api/routers/runs.py
@@ -0,0 +1,24 @@
+import json
+from pathlib import Path
+
+from fastapi import APIRouter
+
+router = APIRouter()
+
+runs_directory = Path("runs")
+
+
+@router.get("/")
+async def list():
+    return [
+        run.name
+        for run in runs_directory.iterdir()
+        if run.is_dir() and (run / "snapshot.json").exists()
+    ]
+
+
+@router.get("/{run_name}/")
+async def get(run_name: str):
+    run_path = runs_directory / run_name
+    snapshot = run_path / "snapshot.json"
+    return json.load(snapshot.open("r"))
diff --git a/frontend b/frontend
new file mode 160000
index 0000000..d430de1
--- /dev/null
+++ b/frontend
@@ -0,0 +1 @@
+Subproject commit d430de1597342eedf0cede1873507a3ffaa28dbb
-- 
GitLab