Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

add actions to nodejs and python starters #7

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions node-app/Spacefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ micros:
engine: nodejs16
primary: true
run: node index.js
dev: node index.js
provide_actions: true
30 changes: 30 additions & 0 deletions node-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
6 changes: 6 additions & 0 deletions python-app/README.md
Original file line number Diff line number Diff line change
@@ -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
```
3 changes: 3 additions & 0 deletions python-app/Spacefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions python-app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
]
}
3 changes: 2 additions & 1 deletion python-app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
deta~=1.1.0
fastapi~=0.92.0
fastapi~=0.96.0
uvicorn~=0.22.0