diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..5a3af04 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "luoshu-frontend"] + path = luoshu-frontend + url = https://github.com/hubertshelley/luoshu-frontend diff --git a/Cargo.lock b/Cargo.lock index 8d75fce..7eef63a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -663,6 +663,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "http" version = "0.2.8" @@ -1166,6 +1172,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "path-slash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e91099d4268b0e11973f036e885d652fb0b21fedcf69738c627f94db6a44f42" + [[package]] name = "percent-encoding" version = "2.2.0" @@ -1392,6 +1404,40 @@ dependencies = [ "winapi", ] +[[package]] +name = "rust-embed" +version = "6.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b68543d5527e158213414a92832d2aab11a84d2571a5eb021ebe22c43aab066" +dependencies = [ + "rust-embed-impl", + "rust-embed-utils", + "walkdir", +] + +[[package]] +name = "rust-embed-impl" +version = "6.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d4e0f0ced47ded9a68374ac145edd65a6c1fa13a96447b873660b2a568a0fd7" +dependencies = [ + "proc-macro2", + "quote", + "rust-embed-utils", + "syn", + "walkdir", +] + +[[package]] +name = "rust-embed-utils" +version = "7.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512b0ab6853f7e14e3c8754acb43d6f748bb9ced66aa5915a6553ac8213f7731" +dependencies = [ + "sha2", + "walkdir", +] + [[package]] name = "ryu" version = "1.0.11" @@ -1404,7 +1450,28 @@ version = "0.37.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "344f4d0588dbe2dd4be10d4413a4592877e0d044cb4495da29cae830828c4937" dependencies = [ + "salvo-static", + "salvo_core", +] + +[[package]] +name = "salvo-static" +version = "0.37.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fad405b17b09cba8cab74626e55bc64c2f1c85f971f4b9548768691a0cd78f0" +dependencies = [ + "hex", + "mime", + "mime_guess", + "path-slash", + "percent-encoding", + "rust-embed", "salvo_core", + "serde", + "serde_json", + "time 0.3.17", + "tokio", + "tracing", ] [[package]] @@ -1463,6 +1530,15 @@ dependencies = [ "syn", ] +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "scopeguard" version = "1.1.0" @@ -1529,6 +1605,17 @@ dependencies = [ "digest", ] +[[package]] +name = "sha2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sharded-slab" version = "0.1.4" @@ -1961,6 +2048,16 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "walkdir" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "want" version = "0.3.0" diff --git a/clients/luoshu_rust_client/Cargo.toml b/clients/luoshu_rust_client/Cargo.toml index b49a69f..3f0ad52 100644 --- a/clients/luoshu_rust_client/Cargo.toml +++ b/clients/luoshu_rust_client/Cargo.toml @@ -2,6 +2,11 @@ name = "luoshu_rust_client" version = "0.1.0" edition = "2021" +license = "Apache-2.0" +description = """ +Client for Luoshu +""" +readme = "./readme.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/clients/luoshu_rust_client/readme.md b/clients/luoshu_rust_client/readme.md new file mode 100644 index 0000000..323c8b1 --- /dev/null +++ b/clients/luoshu_rust_client/readme.md @@ -0,0 +1,60 @@ +
+

洛书客户端

+
+ +### 安装洛书 + +```shell +cargo install luoshu +``` + +### 运行洛书 +不开放管理web接口 +```shell +luoshu +``` +开放管理web接口 +```shell +luoshu --web +``` + + +### 洛书客户端使用 + +引入洛书客户端 +```toml +[workspace.dependencies] +# ... +luoshu_rust_client = "0.1.0" +# ... +``` +订阅配置信息,并注册服务到洛书并编写业务代码 +```rust +use std::thread::sleep; +use luoshu_rust_client::LuoshuClient; +#[derive(Debug, Serialize, Deserialize, Clone)] +struct Config { + test1: String, + test2: Vec, +} +#[tokio::test] +async fn it_works() -> LuoshuClientResult<()> { + let mut client = LuoshuClient::new(15666, "test_rust_server".to_string(), None).await; + client + .subscribe_config( + "test_config2".to_string(), + |config: Config| println!("config changed:{:#?}", config), + None, + ) + .await?; + tokio::spawn(async move { + client.registry().await.expect("TODO: panic message"); + }); + + // 此处使用无限循环模拟服务的持续运行 + loop { + println!("sleep"); + sleep(Duration::from_secs(10)) + } +} +``` \ No newline at end of file diff --git a/clients/luoshu_rust_client/src/lib.rs b/clients/luoshu_rust_client/src/lib.rs index 9097810..32293ee 100644 --- a/clients/luoshu_rust_client/src/lib.rs +++ b/clients/luoshu_rust_client/src/lib.rs @@ -17,7 +17,39 @@ use tokio::net::TcpStream; use tokio::sync::mpsc; use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; -/// 洛书客户端结构体 +/// 洛书客户端 +/// +/// 订阅配置信息,并注册服务到洛书 +/// ``` +/// use std::thread::sleep; +/// use luoshu_rust_client::LuoshuClient; +/// +/// #[derive(Debug, Serialize, Deserialize, Clone)] +/// struct Config { +/// test1: String, +/// test2: Vec, +/// } +/// +/// #[tokio::test] +/// async fn it_works() -> LuoshuClientResult<()> { +/// let mut client = LuoshuClient::new(15666, "test_rust_server".to_string(), None).await; +/// client +/// .subscribe_config( +/// "test_config2".to_string(), +/// |config: Config| println!("config changed:{:#?}", config), +/// None, +/// ) +/// .await?; +/// tokio::spawn(async move { +/// client.registry().await.expect("TODO: panic message"); +/// }); +/// // loop { +/// // println!("sleep"); +/// // sleep(Duration::from_secs(10)) +/// // } +/// Ok(()) +/// } +/// ``` pub struct LuoshuClient { namespace: String, name: String, diff --git a/luoshu-frontend b/luoshu-frontend new file mode 160000 index 0000000..09e2a63 --- /dev/null +++ b/luoshu-frontend @@ -0,0 +1 @@ +Subproject commit 09e2a63398901a933dee343df21d587327d3ea2c diff --git a/luoshu/Cargo.toml b/luoshu/Cargo.toml index 3d4a77a..96a61e5 100644 --- a/luoshu/Cargo.toml +++ b/luoshu/Cargo.toml @@ -19,7 +19,7 @@ path = "src/bin/client.rs" [dependencies] clap = { workspace = true, features = ["derive"] } once_cell.workspace = true -salvo = { workspace = true } +salvo = { workspace = true, features = ["serve-static"] } tokio = { workspace = true, features = ["full"] } sled.workspace = true tracing = { workspace = true } diff --git a/luoshu/src/data/frame.rs b/luoshu/src/data/frame.rs index 232b821..db3bcb8 100644 --- a/luoshu/src/data/frame.rs +++ b/luoshu/src/data/frame.rs @@ -64,7 +64,6 @@ impl From for Frame { } } -#[allow(dead_code)] impl Frame { /// 消息分析 pub fn parse(src: &[u8]) -> LuoshuResult { diff --git a/luoshu/src/web/mod.rs b/luoshu/src/web/mod.rs index 57d6abd..5321cc1 100644 --- a/luoshu/src/web/mod.rs +++ b/luoshu/src/web/mod.rs @@ -5,8 +5,9 @@ mod resp; mod service; use async_trait::async_trait; -use salvo::prelude::{TcpListener, Text}; -use salvo::{handler, Depot, FlowCtrl, Handler, Request, Response, Router, Server}; +use salvo::prelude::TcpListener; +use salvo::serve_static::StaticDir; +use salvo::{Depot, FlowCtrl, Handler, Request, Response, Router, Server}; use std::sync::Arc; use tokio::sync::RwLock; @@ -20,10 +21,19 @@ pub async fn run_server(addr: &str, data: Arc>) { let set_store = SetStore(data); let router = Router::with_hoop(set_store) - .get(index) - .push(get_service_routers()) - .push(get_namespace_routers()) - .push(get_configuration_routers()); + .push( + Router::with_path("api") + .push(get_service_routers()) + .push(get_namespace_routers()) + .push(get_configuration_routers()), + ) + .push( + Router::with_path("<**path>").get( + StaticDir::new(["luoshu-frontend/dist"]) + .with_defaults("index.html") + .with_listing(true), + ), + ); tracing::info!("admin listening on: http://{}", addr); @@ -45,10 +55,3 @@ impl Handler for SetStore { _ctrl.call_next(_req, _depot, _res).await; } } - -#[handler] -async fn index(res: &mut Response) { - res.render(Text::Html(INDEX_HTML)); -} - -static INDEX_HTML: &str = include_str!("./templates/index.html"); diff --git a/readme.md b/readme.md index 41be727..52a3856 100644 --- a/readme.md +++ b/readme.md @@ -23,3 +23,17 @@ License

+ +### 运行说明 +#### 编译前端 +```shell +cd luoshu-frontend +npm install +npm run build + +``` +#### 运行后端 +```shell +cargo run --release --bin luoshu -- --web +``` +打开浏览器访问 http://localhost:19999 \ No newline at end of file