Feat/web (Patchset 4) (#460)
support basic functions in frontend 1. create/del network 2. inspect network running status
This commit is contained in:
@@ -11,7 +11,7 @@ use axum_login::tower_sessions::{ExpiredDeletion, SessionManagerLayer};
|
||||
use axum_login::{login_required, AuthManagerLayerBuilder, AuthzBackend};
|
||||
use axum_messages::MessagesManagerLayer;
|
||||
use easytier::common::scoped_task::ScopedTask;
|
||||
use easytier::proto::{self, rpc_types};
|
||||
use easytier::proto::{rpc_types};
|
||||
use network::NetworkApi;
|
||||
use sea_orm::DbErr;
|
||||
use tokio::net::TcpListener;
|
||||
@@ -43,16 +43,16 @@ type AppState = State<AppStateInner>;
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
struct ListSessionJsonResp(Vec<StorageToken>);
|
||||
|
||||
pub type Error = proto::error::Error;
|
||||
pub type ErrorKind = proto::error::error::ErrorKind;
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub struct Error {
|
||||
message: String,
|
||||
}
|
||||
type RpcError = rpc_types::error::Error;
|
||||
type HttpHandleError = (StatusCode, Json<Error>);
|
||||
|
||||
pub fn other_error<T: ToString>(error_message: T) -> Error {
|
||||
Error {
|
||||
error_kind: Some(ErrorKind::OtherError(proto::error::OtherError {
|
||||
error_message: error_message.to_string(),
|
||||
})),
|
||||
message: error_message.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,16 +6,17 @@ use axum::routing::{delete, post};
|
||||
use axum::{extract::State, routing::get, Json, Router};
|
||||
use axum_login::AuthUser;
|
||||
use dashmap::DashSet;
|
||||
use easytier::launcher::NetworkConfig;
|
||||
use easytier::proto::common::Void;
|
||||
use easytier::proto::rpc_types::controller::BaseController;
|
||||
use easytier::proto::{self, web::*};
|
||||
use easytier::proto::{web::*};
|
||||
|
||||
use crate::client_manager::session::Session;
|
||||
use crate::client_manager::ClientManager;
|
||||
|
||||
use super::users::AuthSession;
|
||||
use super::{
|
||||
convert_db_error, AppState, AppStateInner, Error, ErrorKind, HttpHandleError, RpcError,
|
||||
convert_db_error, other_error, AppState, AppStateInner, Error, HttpHandleError, RpcError,
|
||||
};
|
||||
|
||||
fn convert_rpc_error(e: RpcError) -> (StatusCode, Json<Error>) {
|
||||
@@ -24,13 +25,15 @@ fn convert_rpc_error(e: RpcError) -> (StatusCode, Json<Error>) {
|
||||
RpcError::Timeout(_) => StatusCode::GATEWAY_TIMEOUT,
|
||||
_ => StatusCode::BAD_GATEWAY,
|
||||
};
|
||||
let error = Error::from(&e);
|
||||
let error = Error {
|
||||
message: format!("{:?}", e),
|
||||
};
|
||||
(status_code, Json(error))
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
struct ValidateConfigJsonReq {
|
||||
config: String,
|
||||
config: NetworkConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
@@ -77,24 +80,14 @@ impl NetworkApi {
|
||||
let Some(result) = client_mgr.get_session_by_machine_id(machine_id) else {
|
||||
return Err((
|
||||
StatusCode::NOT_FOUND,
|
||||
Error {
|
||||
error_kind: Some(ErrorKind::OtherError(proto::error::OtherError {
|
||||
error_message: format!("No such session: {}", machine_id),
|
||||
})),
|
||||
}
|
||||
.into(),
|
||||
other_error(format!("No such session: {}", machine_id)).into(),
|
||||
));
|
||||
};
|
||||
|
||||
let Some(token) = result.get_token().await else {
|
||||
return Err((
|
||||
StatusCode::UNAUTHORIZED,
|
||||
Error {
|
||||
error_kind: Some(ErrorKind::OtherError(proto::error::OtherError {
|
||||
error_message: "No token reported".to_string(),
|
||||
})),
|
||||
}
|
||||
.into(),
|
||||
other_error(format!("No token reported")).into(),
|
||||
));
|
||||
};
|
||||
|
||||
@@ -106,12 +99,7 @@ impl NetworkApi {
|
||||
{
|
||||
return Err((
|
||||
StatusCode::FORBIDDEN,
|
||||
Error {
|
||||
error_kind: Some(ErrorKind::OtherError(proto::error::OtherError {
|
||||
error_message: "Token mismatch".to_string(),
|
||||
})),
|
||||
}
|
||||
.into(),
|
||||
other_error(format!("Token mismatch")).into(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -123,16 +111,22 @@ impl NetworkApi {
|
||||
State(client_mgr): AppState,
|
||||
Path(machine_id): Path<uuid::Uuid>,
|
||||
Json(payload): Json<ValidateConfigJsonReq>,
|
||||
) -> Result<Json<Void>, HttpHandleError> {
|
||||
) -> Result<Json<ValidateConfigResponse>, HttpHandleError> {
|
||||
let config = payload.config;
|
||||
let result =
|
||||
Self::get_session_by_machine_id(&auth_session, &client_mgr, &machine_id).await?;
|
||||
|
||||
let c = result.scoped_rpc_client();
|
||||
c.validate_config(BaseController::default(), ValidateConfigRequest { config })
|
||||
let ret = c
|
||||
.validate_config(
|
||||
BaseController::default(),
|
||||
ValidateConfigRequest {
|
||||
config: Some(config),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(convert_rpc_error)?;
|
||||
Ok(Void::default().into())
|
||||
Ok(ret.into())
|
||||
}
|
||||
|
||||
async fn handle_run_network_instance(
|
||||
|
||||
Reference in New Issue
Block a user