Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加前端项目 #8

Merged
merged 4 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "luoshu-frontend"]
path = luoshu-frontend
url = https://github.com/hubertshelley/luoshu-frontend
97 changes: 97 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions clients/luoshu_rust_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
60 changes: 60 additions & 0 deletions clients/luoshu_rust_client/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<div align="center">
<h1>洛书客户端</h1>
</div>

### 安装洛书

```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<usize>,
}
#[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))
}
}
```
34 changes: 33 additions & 1 deletion clients/luoshu_rust_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>,
/// }
///
/// #[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,
Expand Down
1 change: 1 addition & 0 deletions luoshu-frontend
Submodule luoshu-frontend added at 09e2a6
2 changes: 1 addition & 1 deletion luoshu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 0 additions & 1 deletion luoshu/src/data/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ impl From<ActionEnum> for Frame {
}
}

#[allow(dead_code)]
impl Frame {
/// 消息分析
pub fn parse(src: &[u8]) -> LuoshuResult<Frame> {
Expand Down
29 changes: 16 additions & 13 deletions luoshu/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,10 +21,19 @@ pub async fn run_server(addr: &str, data: Arc<RwLock<LuoshuSledData>>) {
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);

Expand All @@ -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");
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,17 @@
<img alt="License" src="https://img.shields.io/crates/l/luoshu.svg" />
</p>
</div>

### 运行说明
#### 编译前端
```shell
cd luoshu-frontend
npm install
npm run build

```
#### 运行后端
```shell
cargo run --release --bin luoshu -- --web
```
打开浏览器访问 http://localhost:19999