Add example code snippet to the README

Create a copy-able snippet to get folks going.

Include a link to the Developer Book.
This commit is contained in:
Salvatore Testa 2024-01-11 10:57:07 -08:00
parent ead8e9ab86
commit cbd6ce718b
No known key found for this signature in database
1 changed files with 56 additions and 0 deletions

View File

@ -18,6 +18,62 @@ The easiest way to help grow the Veilid network is to run your own node. Every u
To run such a node, establish a Debian or Fedora based VPS and install the veilid-server service. To make this process simple we are hosting package manager repositories for .deb and .rpm packages. See the [installing](./INSTALL.md) guide for more information.
## Building on Veilid
If you want to start using Veilid for your own app, take a look at the [Developer Book](https://veilid.gitlab.io/developer-book/).
A basic example using `veilid-core` and `tokio` might look like this.
```rust
use std::sync::Arc;
use veilid_core::VeilidUpdate::{AppMessage, Network};
use veilid_core::{VeilidConfigBlockStore, VeilidConfigInner, VeilidConfigProtectedStore, VeilidConfigTableStore, VeilidUpdate};
#[tokio::main]
async fn main() {
let update_callback = Arc::new(move |update: VeilidUpdate| {
match update {
AppMessage(msg) => {
println!("Message: {}", String::from_utf8_lossy(msg.message().into()));
}
Network(msg) => {
println!("Network: Peers {:}, bytes/sec [{} up] [{} down]", msg.peers.iter().count(), msg.bps_up, msg.bps_down)
}
_ => {
println!("{:?}", update)
}
};
});
let config = VeilidConfigInner {
program_name: "Example Veilid".into(),
namespace: "veilid-example".into(),
protected_store: VeilidConfigProtectedStore {
// avoid prompting for password, don't do this in production
always_use_insecure_storage: true,
directory: "./.veilid/block_store".into(),
..Default::default()
},
block_store: VeilidConfigBlockStore {
directory: "./.veilid/block_store".into(),
..Default::default()
},
table_store: VeilidConfigTableStore {
directory: "./.veilid/table_store".into(),
..Default::default()
},
..Default::default()
};
let veilid = veilid_core::api_startup_config(update_callback, config).await.unwrap();
println!("Node ID: {}", veilid.config().unwrap().get_veilid_state().config.network.routing_table.node_id);
veilid.attach().await.unwrap();
// Until CTRL+C is pressed, keep running
tokio::signal::ctrl_c().await.unwrap();
veilid.shutdown().await;
}
```
## Development
If you're inclined to get involved in code and non-code development, please check out the [contributing](./CONTRIBUTING.md) guide. We're striving for this project to be developed in the open and by people for people. Specific areas in which we are looking for help include: