From 2c84eb48a736a55cb21abf5e40ce5e39cc2f378b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Thu, 7 Nov 2024 19:01:25 +0800 Subject: [PATCH] Doc: update getting-started --- .../docs/getting_started/getting-started.md | 77 ++++++++----------- 1 file changed, 32 insertions(+), 45 deletions(-) diff --git a/openraft/src/docs/getting_started/getting-started.md b/openraft/src/docs/getting_started/getting-started.md index f778d1352..081f22b76 100644 --- a/openraft/src/docs/getting_started/getting-started.md +++ b/openraft/src/docs/getting_started/getting-started.md @@ -289,53 +289,41 @@ Finally, we put these parts together and boot up a raft node : ```ignore -// Define the types used in the application. -pub struct TypeConfig {} -impl openraft::RaftTypeConfig for TypeConfig { - type D = Request; - type R = Response; - type NodeId = NodeId; - type Node = BasicNode; - type Entry = openraft::Entry; - type SnapshotData = Cursor>; -} - -#[tokio::main] -async fn main() { - #[actix_web::main] - async fn main() -> std::io::Result<()> { - // Setup the logger - env_logger::init_from_env(Env::default().default_filter_or("info")); +openraft::declare_raft_types!( + pub TypeConfig: + D = Request, + R = Response, +); - // Parse the parameters passed by arguments. - let options = Opt::parse(); - let node_id = options.id; +#[actix_web::main] +async fn main() -> std::io::Result<()> { - // Create a configuration for the raft instance. + let node_id = 1; let config = Arc::new(Config::default().validate().unwrap()); - // Create a instance of where the Raft data will be stored. - let store = Arc::new(ExampleStore::default()); - - let (log_store, state_machine) = Adaptor::new(store.clone()); - - // Create the network layer that will connect and communicate the raft instances and - // will be used in conjunction with the store created above. - let network = Arc::new(ExampleNetwork {}); - - // Create a local raft instance. - let raft = openraft::Raft::new(node_id, config.clone(), network, log_store, state_machine).await.unwrap(); - - // Create an application that will store all the instances created above, this will - // be later used on the actix-web services. - let app = Data::new(ExampleApp { - id: options.id, - raft, - store, - config, + let log_store = LogStore::default(); + let state_machine_store = Arc::new(StateMachineStore::default()); + let network = Network {}; + + let raft = openraft::Raft::new( + node_id, + config.clone(), + network, + log_store.clone(), + state_machine_store.clone(), + ) + .await + .unwrap(); + + let app_data = Data::new(App { + id: node_id, + addr: "127.0.0.1:9999".to_string(), + raft, + log_store, + state_machine_store, + config, }); - // Start the actix-web server. HttpServer::new(move || { App::new() .wrap(Logger::default()) @@ -356,10 +344,9 @@ async fn main() { // application API .service(api::write).service(api::read) }) - .bind(options.http_addr)? - .run() - .await - } + .bind(options.http_addr)? + .run() + .await } ```