Merge branch 'master' into feature/nginx-handler
All checks were successful
Test / get-ci-image (pull_request) Successful in 6s
Test / lint-frontend (pull_request) Successful in 24s
Test / test-frontend (pull_request) Successful in 25s
Verify / get-ci-image (pull_request) Successful in 5s
Test / frontend-build (pull_request) Successful in 40s
Test / lint-crates (pull_request) Successful in 4m58s
Test / test-crates (pull_request) Successful in 5m16s
Verify / verify-generated-db-entities (pull_request) Successful in 5m42s
All checks were successful
Test / get-ci-image (pull_request) Successful in 6s
Test / lint-frontend (pull_request) Successful in 24s
Test / test-frontend (pull_request) Successful in 25s
Verify / get-ci-image (pull_request) Successful in 5s
Test / frontend-build (pull_request) Successful in 40s
Test / lint-crates (pull_request) Successful in 4m58s
Test / test-crates (pull_request) Successful in 5m16s
Verify / verify-generated-db-entities (pull_request) Successful in 5m42s
This commit is contained in:
@@ -84,7 +84,13 @@ time = "0.3"
|
||||
|
||||
# Cert handling
|
||||
zip = { workspace = true }
|
||||
rust-embed = { version = "8.11.0", features = [] }
|
||||
mime_guess = "2.0.5"
|
||||
axum-test = "20.0.0"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio-test.workspace = true
|
||||
mockall.workspace = true
|
||||
|
||||
[features]
|
||||
dev-tools = ["axum/macros"]
|
||||
|
||||
1
apps/nxmesh-master/frontend-dist
Symbolic link
1
apps/nxmesh-master/frontend-dist
Symbolic link
@@ -0,0 +1 @@
|
||||
../nxmesh-frontend/dist/
|
||||
@@ -6,6 +6,7 @@ use nxmesh_proto::{
|
||||
};
|
||||
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};
|
||||
use tonic::transport::Server;
|
||||
use tracing::info;
|
||||
|
||||
use crate::{db::entities::public_key_revocations, service::agent::AgentServerService};
|
||||
|
||||
@@ -69,7 +70,14 @@ impl AgentConnectorTrait for SshAgentConnector {
|
||||
.layer(ssh_interceptor)
|
||||
.add_service(agent_server_service);
|
||||
|
||||
router.serve(addr).await?;
|
||||
info!("SSH Agent gRPC server is listening on {}", addr);
|
||||
router
|
||||
.serve(addr)
|
||||
.await
|
||||
.inspect(|_| info!("SSH Agent gRPC server stopped gracefully."))
|
||||
.inspect_err(|e| {
|
||||
tracing::error!("SSH Agent gRPC server failed: {}", e);
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ mod cli;
|
||||
mod config;
|
||||
mod connector;
|
||||
mod db;
|
||||
mod routes;
|
||||
mod service;
|
||||
|
||||
#[tokio::main]
|
||||
|
||||
160
apps/nxmesh-master/src/routes/frontend/mod.rs
Normal file
160
apps/nxmesh-master/src/routes/frontend/mod.rs
Normal file
@@ -0,0 +1,160 @@
|
||||
use axum::{Router, response::IntoResponse};
|
||||
use tracing::error;
|
||||
|
||||
// In development, build the frontend from the source directory, the soft link will handle the path resolution
|
||||
// In deployment, pre-build the frontend and replace the frontend-dist folder with the built assets, the rust-embed will handle the embedding and path resolution
|
||||
#[derive(rust_embed::Embed)]
|
||||
#[folder = "./frontend-dist/"]
|
||||
struct FrontendAssets;
|
||||
|
||||
const INDEX_HTML: &str = "index.html";
|
||||
|
||||
pub async fn get_router() -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/",
|
||||
axum::routing::get(get_fallback_handler)
|
||||
.head(get_fallback_handler)
|
||||
.options(get_fallback_handler),
|
||||
)
|
||||
.route(
|
||||
"/{*path}",
|
||||
axum::routing::get(get_file_handler)
|
||||
.head(get_file_handler)
|
||||
.options(get_file_handler),
|
||||
)
|
||||
//
|
||||
.fallback(get_fallback_handler().await)
|
||||
}
|
||||
|
||||
pub async fn get_fallback_handler() -> Result<axum::response::Html<Vec<u8>>, axum::http::StatusCode>
|
||||
{
|
||||
let index_html = get_index_html();
|
||||
match index_html {
|
||||
Some(html) => Ok(axum::response::Html(html)),
|
||||
None => Err(axum::http::StatusCode::NOT_FOUND),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_index_html() -> Option<Vec<u8>> {
|
||||
FrontendAssets::get(INDEX_HTML).map(|asset| asset.data.as_ref().to_owned())
|
||||
}
|
||||
|
||||
async fn get_file_handler(
|
||||
axum::extract::Path(path): axum::extract::Path<String>,
|
||||
) -> Result<axum::response::Response, axum::http::StatusCode> {
|
||||
let file_path = if path.is_empty() {
|
||||
INDEX_HTML.to_string()
|
||||
} else {
|
||||
path
|
||||
};
|
||||
|
||||
match FrontendAssets::get(&file_path) {
|
||||
Some(asset) => {
|
||||
let content_type = mime_guess::from_path(&file_path).first_or_octet_stream();
|
||||
let response = axum::response::Response::builder()
|
||||
.header(axum::http::header::CONTENT_TYPE, content_type.as_ref())
|
||||
.body(asset.data.into_owned().into())
|
||||
.map_err(|e| {
|
||||
error!("Failed to build response for {}: {}", file_path, e);
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR
|
||||
})?;
|
||||
Ok(response)
|
||||
}
|
||||
// return index.html for any file not found to support client-side routing in the frontend
|
||||
None => get_fallback_handler()
|
||||
.await
|
||||
.map(|html| html.into_response()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::expect_used)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_asset() {
|
||||
// list all embedded assets for debugging
|
||||
let assets = FrontendAssets::iter().collect::<Vec<_>>();
|
||||
println!("Embedded assets: {:?}", assets);
|
||||
assert!(
|
||||
!assets.is_empty(),
|
||||
"Expected to find embedded assets, but found none"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_index_html() {
|
||||
let index_html = get_index_html();
|
||||
assert!(
|
||||
index_html.is_some(),
|
||||
"Expected to find index.html in embedded assets"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_file_handler_existing_file() {
|
||||
let response = get_file_handler(axum::extract::Path("index.html".to_string())).await;
|
||||
assert!(
|
||||
response.is_ok(),
|
||||
"Expected to successfully retrieve index.html"
|
||||
);
|
||||
let response = response.expect("Expected response to be Ok");
|
||||
assert_eq!(response.status(), axum::http::StatusCode::OK);
|
||||
assert!(
|
||||
response
|
||||
.headers()
|
||||
.get(axum::http::header::CONTENT_TYPE)
|
||||
.map(|ct| ct.to_str().unwrap_or(""))
|
||||
.expect("Content-Type header should be present")
|
||||
.starts_with("text/html")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_file_handler_nonexistent_file() {
|
||||
let response = get_file_handler(axum::extract::Path("nonexistent.txt".to_string())).await;
|
||||
assert!(
|
||||
response.is_ok(),
|
||||
"Expected to fallback to index.html for nonexistent file"
|
||||
);
|
||||
let response = response.expect("Expected response to be Ok");
|
||||
assert_eq!(response.status(), axum::http::StatusCode::OK);
|
||||
assert!(
|
||||
response
|
||||
.headers()
|
||||
.get(axum::http::header::CONTENT_TYPE)
|
||||
.map(|ct| ct.to_str().unwrap_or(""))
|
||||
.expect("Content-Type header should be present")
|
||||
.starts_with("text/html")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod axum_tests {
|
||||
use super::*;
|
||||
use axum_test::TestServer;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_should_return_index_html_for_root_path() {
|
||||
let router = get_router().await;
|
||||
let server = TestServer::new(router);
|
||||
let response = server.get("/").await;
|
||||
assert_eq!(response.status_code(), 200);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_should_return_index_html_for_nonexistent_path() {
|
||||
let router = get_router().await;
|
||||
let server = TestServer::new(router);
|
||||
let fallback_response = server.get("/nonexistent").await;
|
||||
assert_eq!(fallback_response.status_code(), 200);
|
||||
|
||||
let index_response = server.get("/").await;
|
||||
assert_eq!(index_response.status_code(), 200);
|
||||
|
||||
assert_eq!(fallback_response.text(), index_response.text());
|
||||
}
|
||||
}
|
||||
36
apps/nxmesh-master/src/routes/mod.rs
Normal file
36
apps/nxmesh-master/src/routes/mod.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use axum::Router;
|
||||
|
||||
mod frontend;
|
||||
|
||||
pub async fn get_root_router() -> Router {
|
||||
Router::new()
|
||||
.merge(frontend::get_router().await)
|
||||
.fallback(frontend::get_fallback_handler().await)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use axum_test::TestServer;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_should_return_index_html_for_root_path() {
|
||||
let router = get_root_router().await;
|
||||
let server = TestServer::new(router);
|
||||
let response = server.get("/").await;
|
||||
assert_eq!(response.status_code(), 200);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_should_return_index_html_for_nonexistent_path() {
|
||||
let router = get_root_router().await;
|
||||
let server = TestServer::new(router);
|
||||
let fallback_response = server.get("/nonexistent").await;
|
||||
assert_eq!(fallback_response.status_code(), 200);
|
||||
|
||||
let index_response = server.get("/").await;
|
||||
assert_eq!(index_response.status_code(), 200);
|
||||
|
||||
assert_eq!(fallback_response.text(), index_response.text());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
use std::{net::ToSocketAddrs, sync::Arc};
|
||||
|
||||
use tracing::info;
|
||||
|
||||
use crate::{connector::agent::AgentConnectorTrait, service::certificate::CertificateService};
|
||||
|
||||
@@ -26,15 +28,38 @@ pub async fn start_master_server(
|
||||
println!("Certificate generated and stored successfully.");
|
||||
}
|
||||
|
||||
// Initialize agent connector
|
||||
let mut agent_connector = crate::connector::agent::AgentConnector::new(Box::new(
|
||||
crate::connector::agent::ssh::SshAgentConnector::new(settings.clone())?,
|
||||
));
|
||||
let ssh_connector = crate::connector::agent::ssh::SshAgentConnector::new(settings.clone())?;
|
||||
let cert_service_for_agent = cert_service.clone();
|
||||
let settings_for_agent = settings.clone();
|
||||
let connection_for_agent = db_connection.clone();
|
||||
|
||||
// Start the agent server
|
||||
agent_connector
|
||||
.start_server(&settings, cert_service, db_connection)
|
||||
.await?;
|
||||
tokio::spawn(async move {
|
||||
let mut connector = ssh_connector;
|
||||
tracing::info!("Starting agent server...");
|
||||
if let Err(e) = connector
|
||||
.start_server(
|
||||
&settings_for_agent,
|
||||
cert_service_for_agent,
|
||||
connection_for_agent,
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::error!("Agent server failed: {}", e);
|
||||
} else {
|
||||
tracing::info!("Agent server stopped.");
|
||||
}
|
||||
});
|
||||
|
||||
let axum_router = crate::routes::get_root_router().await;
|
||||
|
||||
// Start the HTTP server
|
||||
let addr = format!("{}:{}", settings.server.bind_address, settings.server.port)
|
||||
.to_socket_addrs()?
|
||||
.next()
|
||||
.ok_or("Invalid bind address")?;
|
||||
let listener = tokio::net::TcpListener::bind(addr).await?;
|
||||
info!("Web/API server is listening on {}", addr);
|
||||
axum::serve(listener, axum_router).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user