-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor/nanocld: cleaner system services with file separation (#1109)
- Loading branch information
Showing
5 changed files
with
134 additions
and
121 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})) | ||
} |