Skip to content

Commit

Permalink
refactor/nanocld: cleaner system services with file separation (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s authored Oct 24, 2024
1 parent 44926b4 commit dae788b
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 121 deletions.
121 changes: 0 additions & 121 deletions bin/nanocld/src/services/system.rs

This file was deleted.

36 changes: 36 additions & 0 deletions bin/nanocld/src/services/system/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use ntex::web;

use bollard_next::network::InspectNetworkOptions;
use nanocl_error::http::HttpResult;
use nanocl_stubs::system::HostInfo;

use crate::models::SystemState;

/// Get host/node system information
#[cfg_attr(feature = "dev", utoipa::path(
get,
tag = "System",
path = "/info",
responses(
(status = 200, description = "Host/Node information", body = HostInfo),
),
))]
#[web::get("/info")]
pub async fn get_info(
state: web::types::State<SystemState>,
) -> HttpResult<web::HttpResponse> {
let docker = state.inner.docker_api.info().await?;
let host_gateway = state.inner.config.gateway.clone();
let network = state
.inner
.docker_api
.inspect_network("nanoclbr0", None::<InspectNetworkOptions<String>>)
.await?;
let info = HostInfo {
docker,
host_gateway,
network,
config: state.inner.config.clone(),
};
Ok(web::HttpResponse::Ok().json(&info))
}
59 changes: 59 additions & 0 deletions bin/nanocld/src/services/system/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use ntex::web;

pub mod info;
pub mod ping;
pub mod version;

pub use info::*;
pub use ping::*;
pub use version::*;

pub fn ntex_config(config: &mut web::ServiceConfig) {
config.service(get_ping);
config.service(get_version);
config.service(get_info);
}

#[cfg(test)]
mod tests {
use nanocl_stubs::system::HostInfo;
use ntex::http;

use crate::services::ntex_config;
use crate::utils::tests::*;

#[ntex::test]
async fn system_info() {
let system = gen_default_test_system().await;
let client = system.client;
let mut res = client.send_get("/info", None::<String>).await;
test_status_code!(res.status(), http::StatusCode::OK, "system info");
let _ = res.json::<HostInfo>().await.unwrap();
}

#[ntex::test]
async fn wrong_version() {
let client = gen_test_system(ntex_config, "13.44").await.client;
let res = client.send_get("/info", None::<String>).await;
test_status_code!(
res.status(),
http::StatusCode::NOT_FOUND,
"wrong version 13.44"
);
let client = gen_test_system(ntex_config, "5.2").await.client;
let res = client.send_get("/info", None::<String>).await;
test_status_code!(
res.status(),
http::StatusCode::NOT_FOUND,
"wrong version 5.2"
);
}

#[ntex::test]
async fn ping() {
let system = gen_default_test_system().await;
let client = system.client;
let res = client.send_head("/_ping", None::<String>).await;
test_status_code!(res.status(), http::StatusCode::ACCEPTED, "ping");
}
}
17 changes: 17 additions & 0 deletions bin/nanocld/src/services/system/ping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use ntex::web;

use nanocl_error::http::HttpResult;

/// Ping the server to check if it is up
#[cfg_attr(feature = "dev", utoipa::path(
head,
tag = "System",
path = "/_ping",
responses(
(status = 202, description = "Server is up"),
),
))]
#[web::head("/_ping")]
pub async fn get_ping() -> HttpResult<web::HttpResponse> {
Ok(web::HttpResponse::Accepted().into())
}
22 changes: 22 additions & 0 deletions bin/nanocld/src/services/system/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use ntex::web;

use crate::vars;

/// Get version information
#[cfg_attr(feature = "dev", utoipa::path(
get,
tag = "System",
path = "/version",
responses(
(status = 200, description = "Version information", body = BinaryInfo),
),
))]
#[web::get("/version")]
pub async fn get_version() -> web::HttpResponse {
web::HttpResponse::Ok().json(&serde_json::json!({
"Arch": vars::ARCH,
"Channel": vars::CHANNEL,
"Version": vars::VERSION,
"CommitId": vars::COMMIT_ID,
}))
}

0 comments on commit dae788b

Please sign in to comment.