Spaces:
Running
Running
Merge branch 'main' into darabos-crdt-updated
Browse files- server/main.py +26 -11
server/main.py
CHANGED
@@ -10,30 +10,34 @@ from . import workspace
|
|
10 |
here = pathlib.Path(__file__).parent
|
11 |
lynxkite_modules = {}
|
12 |
for _, name, _ in pkgutil.iter_modules([str(here)]):
|
13 |
-
if name.endswith(
|
14 |
-
print(f
|
15 |
-
name = f
|
16 |
lynxkite_modules[name] = importlib.import_module(name)
|
17 |
|
18 |
app = fastapi.FastAPI(lifespan=crdt.lifespan)
|
19 |
app.include_router(crdt.router)
|
20 |
|
|
|
21 |
@app.get("/api/catalog")
|
22 |
def get_catalog():
|
23 |
return {
|
24 |
k: {op.name: op.model_dump() for op in v.values()}
|
25 |
-
for k, v in ops.CATALOGS.items()
|
|
|
26 |
|
27 |
|
28 |
class SaveRequest(workspace.BaseConfig):
|
29 |
path: str
|
30 |
ws: workspace.Workspace
|
31 |
|
|
|
32 |
def save(req: SaveRequest):
|
33 |
path = DATA_PATH / req.path
|
34 |
assert path.is_relative_to(DATA_PATH)
|
35 |
workspace.save(req.ws, path)
|
36 |
|
|
|
37 |
@app.post("/api/save")
|
38 |
async def save_and_execute(req: SaveRequest):
|
39 |
save(req)
|
@@ -41,6 +45,7 @@ async def save_and_execute(req: SaveRequest):
|
|
41 |
save(req)
|
42 |
return req.ws
|
43 |
|
|
|
44 |
@app.get("/api/load")
|
45 |
def load(path: str):
|
46 |
path = DATA_PATH / path
|
@@ -49,31 +54,41 @@ def load(path: str):
|
|
49 |
return workspace.Workspace()
|
50 |
return workspace.load(path)
|
51 |
|
52 |
-
|
|
|
|
|
53 |
|
54 |
@dataclasses.dataclass(order=True)
|
55 |
class DirectoryEntry:
|
56 |
name: str
|
57 |
type: str
|
58 |
|
|
|
59 |
@app.get("/api/dir/list")
|
60 |
def list_dir(path: str):
|
61 |
path = DATA_PATH / path
|
62 |
assert path.is_relative_to(DATA_PATH)
|
63 |
-
return sorted(
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
@app.post("/api/dir/mkdir")
|
68 |
def make_dir(req: dict):
|
69 |
-
path = DATA_PATH / req[
|
70 |
assert path.is_relative_to(DATA_PATH)
|
71 |
assert not path.exists()
|
72 |
path.mkdir()
|
73 |
return list_dir(path.parent)
|
74 |
|
|
|
75 |
@app.post("/api/service")
|
76 |
async def service(req: dict):
|
77 |
-
|
78 |
-
module = lynxkite_modules[req[
|
79 |
return await module.api_service(req)
|
|
|
10 |
here = pathlib.Path(__file__).parent
|
11 |
lynxkite_modules = {}
|
12 |
for _, name, _ in pkgutil.iter_modules([str(here)]):
|
13 |
+
if name.endswith("_ops") and not name.startswith("test_"):
|
14 |
+
print(f"Importing {name}")
|
15 |
+
name = f"server.{name}"
|
16 |
lynxkite_modules[name] = importlib.import_module(name)
|
17 |
|
18 |
app = fastapi.FastAPI(lifespan=crdt.lifespan)
|
19 |
app.include_router(crdt.router)
|
20 |
|
21 |
+
|
22 |
@app.get("/api/catalog")
|
23 |
def get_catalog():
|
24 |
return {
|
25 |
k: {op.name: op.model_dump() for op in v.values()}
|
26 |
+
for k, v in ops.CATALOGS.items()
|
27 |
+
}
|
28 |
|
29 |
|
30 |
class SaveRequest(workspace.BaseConfig):
|
31 |
path: str
|
32 |
ws: workspace.Workspace
|
33 |
|
34 |
+
|
35 |
def save(req: SaveRequest):
|
36 |
path = DATA_PATH / req.path
|
37 |
assert path.is_relative_to(DATA_PATH)
|
38 |
workspace.save(req.ws, path)
|
39 |
|
40 |
+
|
41 |
@app.post("/api/save")
|
42 |
async def save_and_execute(req: SaveRequest):
|
43 |
save(req)
|
|
|
45 |
save(req)
|
46 |
return req.ws
|
47 |
|
48 |
+
|
49 |
@app.get("/api/load")
|
50 |
def load(path: str):
|
51 |
path = DATA_PATH / path
|
|
|
54 |
return workspace.Workspace()
|
55 |
return workspace.load(path)
|
56 |
|
57 |
+
|
58 |
+
DATA_PATH = pathlib.Path.cwd() / "data"
|
59 |
+
|
60 |
|
61 |
@dataclasses.dataclass(order=True)
|
62 |
class DirectoryEntry:
|
63 |
name: str
|
64 |
type: str
|
65 |
|
66 |
+
|
67 |
@app.get("/api/dir/list")
|
68 |
def list_dir(path: str):
|
69 |
path = DATA_PATH / path
|
70 |
assert path.is_relative_to(DATA_PATH)
|
71 |
+
return sorted(
|
72 |
+
[
|
73 |
+
DirectoryEntry(
|
74 |
+
p.relative_to(DATA_PATH), "directory" if p.is_dir() else "workspace"
|
75 |
+
)
|
76 |
+
for p in path.iterdir()
|
77 |
+
]
|
78 |
+
)
|
79 |
+
|
80 |
|
81 |
@app.post("/api/dir/mkdir")
|
82 |
def make_dir(req: dict):
|
83 |
+
path = DATA_PATH / req["path"]
|
84 |
assert path.is_relative_to(DATA_PATH)
|
85 |
assert not path.exists()
|
86 |
path.mkdir()
|
87 |
return list_dir(path.parent)
|
88 |
|
89 |
+
|
90 |
@app.post("/api/service")
|
91 |
async def service(req: dict):
|
92 |
+
"""Executors can provide extra HTTP APIs through the /api/service endpoint."""
|
93 |
+
module = lynxkite_modules[req["module"]]
|
94 |
return await module.api_service(req)
|