Skip to content

Commit

Permalink
Merge pull request #8 from hubertshelley/develop
Browse files Browse the repository at this point in the history
添加前端项目
  • Loading branch information
hubertshelley committed Jun 27, 2023
2 parents 8d9071e + c8bbab5 commit faf219b
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -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
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
@@ -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
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
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
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
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
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

0 comments on commit faf219b

Please sign in to comment.