feat/web: Patchset 3 (#455)

https://apifox.com/apidoc/shared-ceda7a60-e817-4ea8-827b-de4e874dc45e

implement all backend API
This commit is contained in:
Sijie.Sun
2024-11-02 15:13:19 +08:00
committed by GitHub
parent 18da94bf33
commit 8aca5851f2
41 changed files with 4621 additions and 217 deletions
+7 -1
View File
@@ -101,8 +101,14 @@ impl WebClientService for Controller {
req: RunNetworkInstanceRequest,
) -> Result<RunNetworkInstanceResponse, rpc_types::error::Error> {
let cfg = TomlConfigLoader::new_from_str(&req.config)?;
let id = cfg.get_id();
if let Some(inst_id) = req.inst_id {
cfg.set_id(inst_id.into());
}
self.run_network_instance(cfg)?;
Ok(RunNetworkInstanceResponse {})
Ok(RunNetworkInstanceResponse {
inst_id: Some(id.into()),
})
}
async fn retain_network_instance(
+28 -10
View File
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::sync::{Arc, Weak};
use tokio::{
sync::{broadcast, Mutex},
@@ -7,7 +7,7 @@ use tokio::{
};
use crate::{
common::get_machine_id,
common::{constants::EASYTIER_VERSION, get_machine_id},
proto::{
rpc_impl::bidirect::BidirectRpcManager,
rpc_types::controller::BaseController,
@@ -47,7 +47,8 @@ impl Session {
.register(WebClientServiceServer::new(controller.clone()), "");
let mut tasks: JoinSet<()> = JoinSet::new();
let heartbeat_ctx = Self::heartbeat_routine(&rpc_mgr, controller.token(), &mut tasks);
let heartbeat_ctx =
Self::heartbeat_routine(&rpc_mgr, Arc::downgrade(&controller), &mut tasks);
Session {
rpc_mgr,
@@ -59,7 +60,7 @@ impl Session {
fn heartbeat_routine(
rpc_mgr: &BidirectRpcManager,
token: String,
controller: Weak<Controller>,
tasks: &mut JoinSet<()>,
) -> HeartbeatCtx {
let (tx, _rx1) = broadcast::channel(2);
@@ -71,7 +72,8 @@ impl Session {
let mid = get_machine_id();
let inst_id = uuid::Uuid::new_v4();
let token = token;
let token = controller.upgrade().unwrap().token();
let hostname = gethostname::gethostname().to_string_lossy().to_string();
let ctx_clone = ctx.clone();
let mut tick = interval(std::time::Duration::from_secs(1));
@@ -79,13 +81,29 @@ impl Session {
.rpc_client()
.scoped_client::<WebServerServiceClientFactory<BaseController>>(1, 1, "".to_string());
tasks.spawn(async move {
let req = HeartbeatRequest {
machine_id: Some(mid.into()),
inst_id: Some(inst_id.into()),
user_token: token.to_string(),
};
loop {
tick.tick().await;
let Some(controller) = controller.upgrade() else {
break;
};
let req = HeartbeatRequest {
machine_id: Some(mid.into()),
inst_id: Some(inst_id.into()),
user_token: token.to_string(),
easytier_version: EASYTIER_VERSION.to_string(),
hostname: hostname.clone(),
report_time: chrono::Local::now().to_string(),
running_network_instances: controller
.list_network_instance_ids()
.into_iter()
.map(Into::into)
.collect(),
};
match client
.heartbeat(BaseController::default(), req.clone())
.await