From b7d62d66cf83720845bf7134fa357b7286256d96 Mon Sep 17 00:00:00 2001
From: hubertshelley
Date: Thu, 15 Dec 2022 22:01:06 +0800
Subject: [PATCH 1/4] publish client 0.1.0
---
clients/luoshu_rust_client/src/lib.rs | 62 ++++++++++++++++++++-------
1 file changed, 47 insertions(+), 15 deletions(-)
diff --git a/clients/luoshu_rust_client/src/lib.rs b/clients/luoshu_rust_client/src/lib.rs
index 9097810..8912709 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,
@@ -59,9 +91,9 @@ impl LuoshuClient {
self.name.clone(),
Service::new("127.0.0.1".to_string(), self.port),
)
- .into(),
+ .into(),
)
- .into();
+ .into();
self.connection.write_frame(&frame).await?;
let time_sleep = || async {
tokio::time::sleep(Duration::from_secs(5)).await;
@@ -110,9 +142,9 @@ impl LuoshuClient {
callback: F,
namespace: Option,
) -> LuoshuClientResult<()>
- where
- F: Fn(C) + Send + 'static,
- C: Serialize + for<'a> Deserialize<'a>,
+ where
+ F: Fn(C) + Send + 'static,
+ C: Serialize + for<'a> Deserialize<'a>,
{
let subscribe = Subscribe::new(namespace.unwrap_or_else(|| String::from("default")), name);
let subscribe_str = subscribe.to_string();
@@ -151,9 +183,9 @@ impl LuoshuClient {
callback: F,
namespace: Option,
) -> LuoshuClientResult<()>
- where
- F: Fn(C) + Send + 'static,
- C: Serialize + for<'a> Deserialize<'a>,
+ where
+ F: Fn(C) + Send + 'static,
+ C: Serialize + for<'a> Deserialize<'a>,
{
let namespace = namespace.unwrap_or_else(|| String::from("default"));
self.connection
@@ -164,9 +196,9 @@ impl LuoshuClient {
name.clone(),
serde_json::from_slice(&serde_json::to_vec(&config)?)?,
)
- .into(),
+ .into(),
)
- .into(),
+ .into(),
)
.await?;
self.subscribe_config(name, callback, Some(namespace)).await
@@ -179,8 +211,8 @@ impl LuoshuClient {
config: C,
namespace: Option,
) -> LuoshuClientResult<()>
- where
- C: Serialize + for<'a> Deserialize<'a>,
+ where
+ C: Serialize + for<'a> Deserialize<'a>,
{
let namespace = namespace.unwrap_or_else(|| String::from("default"));
self.connection
@@ -191,9 +223,9 @@ impl LuoshuClient {
name,
serde_json::from_slice(&serde_json::to_vec(&config)?)?,
)
- .into(),
+ .into(),
)
- .into(),
+ .into(),
)
.await
.map_err(|e| e.into())
From 7696379cc58ecafc44dd2a811846761a109078c3 Mon Sep 17 00:00:00 2001
From: hubertshelley
Date: Thu, 15 Dec 2022 22:12:08 +0800
Subject: [PATCH 2/4] publish client 0.1.0
---
clients/luoshu_rust_client/Cargo.toml | 5 +++
clients/luoshu_rust_client/readme.md | 60 +++++++++++++++++++++++++++
clients/luoshu_rust_client/src/lib.rs | 2 +-
3 files changed, 66 insertions(+), 1 deletion(-)
create mode 100644 clients/luoshu_rust_client/readme.md
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 8912709..bc4417d 100644
--- a/clients/luoshu_rust_client/src/lib.rs
+++ b/clients/luoshu_rust_client/src/lib.rs
@@ -17,7 +17,7 @@ use tokio::net::TcpStream;
use tokio::sync::mpsc;
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
-/// 洛书客户端结
+/// 洛书客户端
///
/// 订阅配置信息,并注册服务到洛书
/// ```
From 8400c815362afe3705316a47d61f43312c694fe8 Mon Sep 17 00:00:00 2001
From: hubertshelley
Date: Mon, 26 Jun 2023 17:58:24 +0800
Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=89=8D=E7=AB=AF?=
=?UTF-8?q?=E9=A1=B9=E7=9B=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitmodules | 3 ++
Cargo.lock | 97 ++++++++++++++++++++++++++++++++++++++++
luoshu-frontend | 1 +
luoshu/Cargo.toml | 2 +-
luoshu/src/data/frame.rs | 1 -
luoshu/src/web/mod.rs | 30 +++++++------
readme.md | 14 ++++++
7 files changed, 132 insertions(+), 16 deletions(-)
create mode 100644 .gitmodules
create mode 160000 luoshu-frontend
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/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..bf8a97c 100644
--- a/luoshu/src/web/mod.rs
+++ b/luoshu/src/web/mod.rs
@@ -5,9 +5,10 @@ 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::{Depot, FlowCtrl, Handler, Request, Response, Router, Server};
use std::sync::Arc;
+use salvo::serve_static::StaticDir;
use tokio::sync::RwLock;
use crate::data::LuoshuSledData;
@@ -19,11 +20,19 @@ use service::get_routers as get_service_routers;
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());
+ let router = Router::with_hoop(set_store).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 +54,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 @@
+
+### 运行说明
+#### 编译前端
+```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
From c8bbab522bb3f802459a2be8fceee05681aec536 Mon Sep 17 00:00:00 2001
From: hubertshelley
Date: Mon, 26 Jun 2023 18:01:48 +0800
Subject: [PATCH 4/4] cargo fmt
---
clients/luoshu_rust_client/src/lib.rs | 28 ++++++++++++------------
luoshu/src/web/mod.rs | 31 ++++++++++++++-------------
2 files changed, 30 insertions(+), 29 deletions(-)
diff --git a/clients/luoshu_rust_client/src/lib.rs b/clients/luoshu_rust_client/src/lib.rs
index bc4417d..32293ee 100644
--- a/clients/luoshu_rust_client/src/lib.rs
+++ b/clients/luoshu_rust_client/src/lib.rs
@@ -91,9 +91,9 @@ impl LuoshuClient {
self.name.clone(),
Service::new("127.0.0.1".to_string(), self.port),
)
- .into(),
+ .into(),
)
- .into();
+ .into();
self.connection.write_frame(&frame).await?;
let time_sleep = || async {
tokio::time::sleep(Duration::from_secs(5)).await;
@@ -142,9 +142,9 @@ impl LuoshuClient {
callback: F,
namespace: Option,
) -> LuoshuClientResult<()>
- where
- F: Fn(C) + Send + 'static,
- C: Serialize + for<'a> Deserialize<'a>,
+ where
+ F: Fn(C) + Send + 'static,
+ C: Serialize + for<'a> Deserialize<'a>,
{
let subscribe = Subscribe::new(namespace.unwrap_or_else(|| String::from("default")), name);
let subscribe_str = subscribe.to_string();
@@ -183,9 +183,9 @@ impl LuoshuClient {
callback: F,
namespace: Option,
) -> LuoshuClientResult<()>
- where
- F: Fn(C) + Send + 'static,
- C: Serialize + for<'a> Deserialize<'a>,
+ where
+ F: Fn(C) + Send + 'static,
+ C: Serialize + for<'a> Deserialize<'a>,
{
let namespace = namespace.unwrap_or_else(|| String::from("default"));
self.connection
@@ -196,9 +196,9 @@ impl LuoshuClient {
name.clone(),
serde_json::from_slice(&serde_json::to_vec(&config)?)?,
)
- .into(),
- )
.into(),
+ )
+ .into(),
)
.await?;
self.subscribe_config(name, callback, Some(namespace)).await
@@ -211,8 +211,8 @@ impl LuoshuClient {
config: C,
namespace: Option,
) -> LuoshuClientResult<()>
- where
- C: Serialize + for<'a> Deserialize<'a>,
+ where
+ C: Serialize + for<'a> Deserialize<'a>,
{
let namespace = namespace.unwrap_or_else(|| String::from("default"));
self.connection
@@ -223,9 +223,9 @@ impl LuoshuClient {
name,
serde_json::from_slice(&serde_json::to_vec(&config)?)?,
)
- .into(),
- )
.into(),
+ )
+ .into(),
)
.await
.map_err(|e| e.into())
diff --git a/luoshu/src/web/mod.rs b/luoshu/src/web/mod.rs
index bf8a97c..5321cc1 100644
--- a/luoshu/src/web/mod.rs
+++ b/luoshu/src/web/mod.rs
@@ -5,10 +5,10 @@ mod resp;
mod service;
use async_trait::async_trait;
-use salvo::prelude::{TcpListener};
+use salvo::prelude::TcpListener;
+use salvo::serve_static::StaticDir;
use salvo::{Depot, FlowCtrl, Handler, Request, Response, Router, Server};
use std::sync::Arc;
-use salvo::serve_static::StaticDir;
use tokio::sync::RwLock;
use crate::data::LuoshuSledData;
@@ -20,19 +20,20 @@ use service::get_routers as get_service_routers;
pub async fn run_server(addr: &str, data: Arc>) {
let set_store = SetStore(data);
- let router = Router::with_hoop(set_store).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),
- ));
+ let router = Router::with_hoop(set_store)
+ .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);