Skip to content

Commit

Permalink
Updated examples to correctly use serve_ui (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
willbach authored Feb 3, 2024
1 parent e929077 commit d6f2892
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/apis/frontend_development.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ my_package
The simplest way to serve a UI is using the `serve_ui` function from `process_lib`:

```
serve_ui(&our, "ui").unwrap();
serve_ui(&our, "ui", true, false, vec!["/"]).unwrap();
```

This will serve the `index.html` in the specified folder (here, `"ui"`) at the home path of your process.
If your process is called `my_process:my_package:template.os` and your Kinode is running locally on port 8080,
then the UI will be served at `http://localhost:8080/my_process:my_package:template.os`.

`serve_ui` takes two arguments: `&our` (&Address) and the directory where the UI assets are stored.
`serve_ui` takes five arguments: our `&Address`, the name of the folder that contains your frontend, whether the UI requires authentication, whether the UI is local-only, and the path(s) on which to serve the UI (usually `["/"]`).

## Development without kit

Expand Down
4 changes: 2 additions & 2 deletions src/chess_app/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ In `my_chess/src/lib.rs`, inside `init()`:
use kinode_process_lib::http;
...
// Serve the index.html and other UI files found in pkg/ui at the root path.
http::serve_ui(&our, "ui").unwrap();
http::serve_ui(&our, "ui", true, false, vec!["/"]).unwrap();

// Allow HTTP requests to be made to /games; they will be handled dynamically.
http::bind_http_path("/games", true, false).unwrap();
Expand All @@ -44,7 +44,7 @@ The above code should be inserted into the `init()` function such that the front

The `http` library in [process_lib](../process_stdlib/overview.md) provides a simple interface for serving static files and handling HTTP requests.
Use `serve_ui` to serve the static files includeded in the process binary, and `bind_http_path` to handle requests to `/games`.
`serve_ui` takes two arguments: the process' `&Address` and the name of the folder inside `pkg` that contains the `index.html` and other associated UI files.
`serve_ui` takes five arguments: the process' `&Address`, the name of the folder inside `pkg` that contains the `index.html` and other associated UI files, whether the UI requires authentication, whether the UI is local-only, and the path(s) on which to serve the UI (usually `["/"]`).
See [process_lib docs](../process_stdlib/overview.md) for more functions and documentation on their parameters.
These requests all serve HTTP that can only be accessed by a logged-in node user (the `true` parameter for `authenticated`) and can be accessed remotely (the `false` parameter for `local_only`).
This API is under active development!
Expand Down
6 changes: 3 additions & 3 deletions src/cookbook/publish_to_web.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ my_package
The simplest way to serve a UI is using the `serve_ui` function from `process_lib`:

```
serve_ui(&our, "ui").unwrap();
serve_ui(&our, "ui", true, false, vec!["/"]).unwrap();
```

This will serve the `index.html` in the specified folder at the home path of your process.
If your process is called `main:my_package:myusername.os` and your Kinode is running locally on port 8080,
then the UI will be served at `http://localhost:8080/main:my_package:myusername.os`.

`serve_ui` takes two arguments: `&our` (&Address) and the directory where the UI assets are stored.
`serve_ui` takes five arguments: the process' `&Address`, the name of the folder inside `pkg` that contains the `index.html` and other associated UI files, whether the UI requires authentication, whether the UI is local-only, and the path(s) on which to serve the UI (usually `["/"]`).
By convention, this is the `ui` directory inside of the `pkg` directory that will be uploaded when you install the process.
There must be an `index.html` in the `"ui"` directory (or whatever your top-level directory is called).

Expand All @@ -52,7 +52,7 @@ Note that `serve_ui` caches all files in `http_server`, so if your website or we
In this case, you would bind the `index.html` file to your main route, and then bind a given HTTP route to serve all of your assets like so:

```
serve_index_html(&our, "ui").unwrap();
serve_index_html(&our, "ui", true, false, vec!["/"]).unwrap();
bind_http_path("/assets/*", true, false).unwrap();
```

Expand Down
8 changes: 4 additions & 4 deletions src/frontends.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ New libraries will be added as they become necessary or helpful.
## process_lib

In `process_lib::http` there is a function `serve_ui` which is helpful for serving your frontend from a node.
The two arguments to `serve_ui` are the current process' `&Address` and the name of the folder that contains your frontend.
The five arguments to `serve_ui` are the current process' `&Address`, the name of the folder that contains your frontend, whether the UI requires authentication, whether the UI is local-only, and the path(s) on which to serve the UI.
The frontend folder must be placed in the `pkg` folder so that it is loaded into the virtual file system when the process is installed.
So if you put the frontend files in `pkg/ui` (with `index.html` at the top level!), the second argument to `serve_ui` would be `ui`.
Under the hood, `serve_ui` looks for `index.html` and binds it statically to your process' main route.
Under the hood, `serve_ui` looks for `index.html` and binds it statically to the paths that were passed in.
Then all of the other files in the frontend folder are bound to their respective paths.
For example, if your process is called `process:process:my-node`, then `index.html` would be bound to `/process:process:my-node`.
The file `pkg/ui/assets/index.js` would be bound to `/process:process:my-node/assets/index.js`
For example, if your process is called `process:process:my-node` and you passed the paths `["/"]` then `index.html` would be bound to `/process:process:my-node`.
The file `pkg/ui/assets/index.js` would be bound to `/process:process:my-node/assets/index.js`.

## Local Development and "gotchas"

Expand Down

0 comments on commit d6f2892

Please sign in to comment.