diff --git a/node-app/Spacefile b/node-app/Spacefile index 6ee512e..dd0cca9 100644 --- a/node-app/Spacefile +++ b/node-app/Spacefile @@ -6,3 +6,5 @@ micros: engine: nodejs16 primary: true run: node index.js + dev: node index.js + provide_actions: true diff --git a/node-app/index.js b/node-app/index.js index 9d051e6..ad4c4fb 100644 --- a/node-app/index.js +++ b/node-app/index.js @@ -30,6 +30,36 @@ app.post("/api/todos", async (request, response) => { response.send(resp); }); +app.post("/actions/list", async (request, response) => { + const todos = await todos_base.fetch(); + response.json({ + items: todos.items, + }); +}); + +app.get("/__space/actions", async (request, response) => { + response.json({ + actions: [ + { + name: "list", + title: "List todos", + path: "/actions/list", + }, + { + name: "add", + title: "Add todo", + path: "/api/todos", + input: [ + { + name: "text", + type: "string", + }, + ], + }, + ], + }); +}); + app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); }); diff --git a/python-app/README.md b/python-app/README.md index 99fa51b..dab0772 100644 --- a/python-app/README.md +++ b/python-app/README.md @@ -1,3 +1,9 @@ # Python app on Space An example Space app built with [Python](https://python.org) and [FastAPI](https://fastapi.tiangolo.com). + +## Triggering actions + +``` +space dev trigger -x list +``` diff --git a/python-app/Spacefile b/python-app/Spacefile index d2f147d..29a84d9 100644 --- a/python-app/Spacefile +++ b/python-app/Spacefile @@ -5,3 +5,6 @@ micros: src: . engine: python3.9 primary: true + provide_actions: true + run: uvicorn main:app + dev: .venv/bin/uvicorn main:app --reload diff --git a/python-app/main.py b/python-app/main.py index 9ca94bb..166e6f8 100644 --- a/python-app/main.py +++ b/python-app/main.py @@ -32,3 +32,33 @@ async def add_todo(item: TodoItem): resp = todos_base.put(item.dict()) # Return the response as JSON. return resp + +@app.post("/actions/list") +async def list_action(): + todos = todos_base.fetch() + # Return the items as JSON. + return todos.items + + +@app.get("/__space/actions") +async def actions(): + return { + "actions": [ + { + "name": "list", + "title": "List all todos", + "path": "/actions/list" + }, + { + "name": "add", + "path": "/api/todos", + "title": "Add a todo", + "input": [ + { + "name": "text", + "type": "string" + } + ] + } + ] + } diff --git a/python-app/requirements.txt b/python-app/requirements.txt index 962cf93..3f38d01 100644 --- a/python-app/requirements.txt +++ b/python-app/requirements.txt @@ -1,2 +1,3 @@ deta~=1.1.0 -fastapi~=0.92.0 +fastapi~=0.96.0 +uvicorn~=0.22.0