Skip to content

Commit

Permalink
Merge pull request #72 from uqbar-dao/hf/even-more-edits
Browse files Browse the repository at this point in the history
more edits
  • Loading branch information
edgaruncentered authored Jan 22, 2024
2 parents e97ecca + f2aea44 commit 46e4d2f
Show file tree
Hide file tree
Showing 18 changed files with 291 additions and 170 deletions.
2 changes: 1 addition & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@
- [SQLite API](./apis/sqlite.md)
- [Terminal API](./apis/terminal.md)
- [VFS API](./apis/vfs.md)
- [Websocket API](./apis/websocket_authentication.md)
- [WebSocket API](./apis/websocket_authentication.md)
8 changes: 4 additions & 4 deletions src/apis/vfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Every request takes a path and a corresponding action.

## Drives

VFS paths are normal relative paths within the directory `/your_node_home/vfs/`, but to be valid they need to be within a drive.
A drive is just a directory within your vfs, consisting of 2 parts: `/package_id/drive_name/`.
VFS paths are normal relative paths within the directory `your_node_home/vfs/`, but to be valid they need to be within a drive.
A drive is just a directory within your VFS, consisting of 2 parts: `package_id/drive_name/`.

For example: `/your_package:publisher.os/pkg/`.
This directory is usually filled with files put into the `/pkg` directory when installing with app_store.
For example: `your_package:publisher.os/pkg/`.
This directory is usually filled with files put into the `pkg/` directory when installing with `app_store`.
[Capabilities](../process-capabilities.md) are checked on the drive part of the path.
When calling `create_drive()` you'll be given "read" and "write" caps that you can share with other processes.

Expand Down
2 changes: 1 addition & 1 deletion src/apis/websocket_authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const api = new KinodeEncryptorApi({
nodeId: window.our.node, // this is set if the /our.js script is present in index.html
processId: "my_package:my_package:template.os",
onOpen: (_event, api) => {
console.log('Connected to kinode node')
console.log('Connected to Kinode')
// Send a message to the node via WebSocket
api.send({ data: 'Hello World' })
},
Expand Down
6 changes: 3 additions & 3 deletions src/chess_app/chess_engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Once you have the template app installed and can see it running on your testing

# Chess Engine

Chess is a good example for an Kinode application walk-through because:
Chess is a good example for a Kinode application walk-through because:
1. The basic game logic is already readily available.
There are thousands of high-quality chess libraries across many languages that can be imported into a Wasm app that runs on Kinode.
We'll be using [pleco](https://github.com/pleco-rs/Pleco)
Expand Down Expand Up @@ -157,11 +157,11 @@ lto = true
anyhow = "1.0"
base64 = "0.13"
bincode = "1.3.3"
kinode_process_lib = { git = "ssh://[email protected]/uqbar-dao/process_lib.git", tag = "v0.5.4-alpha" }
pleco = "0.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
url = "*"
kinode_process_lib = { git = "ssh://[email protected]/uqbar-dao/process_lib.git", rev = "a2d3e9e" }
wit-bindgen = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "efcc759" }

[lib]
Expand All @@ -183,7 +183,7 @@ use kinode_process_lib::{

extern crate base64;

// Boilerplate: generate the Wasm bindings for an Kinode app
// Boilerplate: generate the Wasm bindings for a Kinode app
wit_bindgen::generate!({
path: "wit",
world: "process",
Expand Down
25 changes: 14 additions & 11 deletions src/chess_app/frontend.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# Adding a Frontend

Here, we'll add a web frontend to the code from the [previous section](./chess_engine.md).
Here, you'll add a web frontend to the code from the [previous section](./chess_engine.md).

Creating a web frontend has two parts:
1. Altering the process code to serve and handle HTTP requests
2. Writing a webpage to interact with the process.
Here, you'll use React to make a single-page app that displays your current games and allows us to: create new games, resign from games, and make moves on the chess board.

JavaScript and React development aren't in the scope of this tutorial, so we'll provide that code [here](https://github.com/uqbar-dao/chess-ui).
JavaScript and React development aren't in the scope of this tutorial, so you can find that code [here](https://github.com/uqbar-dao/chess-ui).

The important part of the frontend for the purpose of this tutorial is the build, specifically the `pkg/ui` directory that will be loaded into the VFS during installation.
Serve these as static files, [which you can get here](https://github.com/uqbar-dao/chess-ui/tree/tutorial/tutorial_build) if you don't want to build them yourself.
The important part of the frontend for the purpose of this tutorial is how to set up those pre-existing files to be built and installed by `kit`.
When files found in the `ui/` directory, if a `package.json` file is found with a `build:copy` field in `scripts`, `kit` will run that to build the UI (see [here](https://github.com/uqbar-dao/chess-ui/blob/82419ea0e53e6d86d6dc6c8ed7f656c3ab51fdc8/package.json#L10)).
The `build:copy` in that file builds the UI and then places the resulting files into the `pkg/ui/` directory where they will be installed by `kit start-package`.
This allows your process to fetch them from the virtual filesystem, as all files in `pkg/` are mounted.
See the [VFS API overview](../apis/vfs.md) to see how to use files mounted in `pkg/`.

Run `npm run build` in the `chess-ui` repo and copy the output `dist` folder into the `pkg` folder in your app, so it'll be ingested on-install.
This allows your process to fetch them from the virtual filesystem, as all files in `pkg` are mounted.
Rename it to `ui` so that you have the files in `pkg/ui`.
See the [VFS API overview](../apis/vfs.md) to see how to use files mounted in `pkg`.
Get the chess UI files and place them in the proper place (next to `pkg/`):
```bash
git clone https://github.com/uqbar-dao/chess-ui ui
```

Chess will use the `http_server` runtime module to serve a static frontend and receive HTTP requests from it.
You'll also use a WebSocket connection to send updates to the frontend when the game state changes.
Expand Down Expand Up @@ -162,14 +165,14 @@ fn handle_http_request(
state: &mut ChessState,
http_request: &http::IncomingHttpRequest,
) -> anyhow::Result<()> {
if http_request.path()? != "games" {
if http_request.path()? != "/games" {
return http::send_response(
http::StatusCode::NOT_FOUND,
None,
"Not Found".to_string().as_bytes().to_vec(),
);
}
match http_request.method.as_str() {
match http_request.method()?.as_str() {
// on GET: give the frontend all of our active games
"GET" => http::send_response(
http::StatusCode::OK,
Expand Down Expand Up @@ -355,7 +358,7 @@ fn send_ws_update(
) -> anyhow::Result<()> {
for channel in open_channels {
Request::new()
.target((&our.node, "http_server", "sys", "kinode"))
.target((&our.node, "http_server", "distro", "sys"))
.body(serde_json::to_vec(
&http::HttpServerAction::WebSocketPush {
channel_id: *channel,
Expand Down
Loading

0 comments on commit 46e4d2f

Please sign in to comment.