diff --git a/src/service/actix/api.rs b/src/service/actix/api.rs new file mode 100644 index 0000000..34bd807 --- /dev/null +++ b/src/service/actix/api.rs @@ -0,0 +1,13 @@ +use actix_web::{get, HttpResponse}; + +use crate::service::constants::*; +use crate::service::dto; + +#[get("/version")] +pub async fn get_version() -> Result { + let current_version = dto::Version { + major: CURRENT_MAJOR_VERSION, + minor: CURRENT_MINOR_VERSION, + }; + Ok(HttpResponse::Ok().json(current_version)) +} diff --git a/src/service/actix/mod.rs b/src/service/actix/mod.rs index 39540f4..f8e7365 100644 --- a/src/service/actix/mod.rs +++ b/src/service/actix/mod.rs @@ -4,6 +4,7 @@ use std::path::Path; pub mod server; +mod api; #[cfg(test)] mod tests; @@ -15,6 +16,7 @@ fn configure_app( swagger_dir_path: &Path, ) { // TODO logging - cfg.service(fs::Files::new(swagger_url, swagger_dir_path).index_file("index.html")) + cfg.service(web::scope("/api").service(api::get_version)) + .service(fs::Files::new(swagger_url, swagger_dir_path).index_file("index.html")) .service(fs::Files::new(web_url, web_dir_path).index_file("index.html")); } diff --git a/src/service/actix/tests/api.rs b/src/service/actix/tests/api.rs index e69de29..6564f1f 100644 --- a/src/service/actix/tests/api.rs +++ b/src/service/actix/tests/api.rs @@ -0,0 +1,23 @@ +use actix_web::body::Body::Bytes; +use actix_web::dev::*; +use actix_web::test::TestRequest; +use actix_web::{test, App}; + +use crate::service::dto; + +#[actix_rt::test] +async fn test_version() { + let app = App::new().configure(super::configure_test_app); + let mut service = test::init_service(app).await; + let req = TestRequest::get().uri("/api/version").to_request(); + let resp = service.call(req).await.unwrap(); + assert!(resp.status().is_success()); + + let body = match resp.response().body().as_ref() { + Some(Bytes(bytes)) => bytes, + _ => panic!("Response error"), + }; + + let response_json: dto::Version = serde_json::from_slice(body).unwrap(); + assert_eq!(response_json, dto::Version { major: 4, minor: 0 }); +} diff --git a/src/service/actix/tests/mod.rs b/src/service/actix/tests/mod.rs index d3b07a4..ea62291 100644 --- a/src/service/actix/tests/mod.rs +++ b/src/service/actix/tests/mod.rs @@ -1,5 +1,6 @@ use std::path::PathBuf; +mod api; mod swagger; mod web; diff --git a/src/service/constants.rs b/src/service/constants.rs new file mode 100644 index 0000000..e222d58 --- /dev/null +++ b/src/service/constants.rs @@ -0,0 +1,5 @@ +pub const CURRENT_MAJOR_VERSION: i32 = 4; +pub const CURRENT_MINOR_VERSION: i32 = 0; +pub const COOKIE_SESSION: &str = "session"; +pub const COOKIE_USERNAME: &str = "username"; +pub const COOKIE_ADMIN: &str = "admin"; diff --git a/src/service/dto.rs b/src/service/dto.rs new file mode 100644 index 0000000..51973f1 --- /dev/null +++ b/src/service/dto.rs @@ -0,0 +1,7 @@ +use serde::{Deserialize, Serialize}; + +#[derive(PartialEq, Debug, Serialize, Deserialize)] +pub struct Version { + pub major: i32, + pub minor: i32, +} diff --git a/src/service/mod.rs b/src/service/mod.rs index dde4ec6..fe22d69 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -1,3 +1,6 @@ +mod constants; +mod dto; + #[cfg(feature = "service-actix")] mod actix; #[cfg(feature = "service-actix")] diff --git a/src/service/rocket/api.rs b/src/service/rocket/api.rs index 64c7b3d..538f02f 100644 --- a/src/service/rocket/api.rs +++ b/src/service/rocket/api.rs @@ -20,17 +20,13 @@ use crate::db::DB; use crate::index; use crate::lastfm; use crate::playlist; +use crate::service::constants::*; +use crate::service::dto; use crate::thumbnails; use crate::user; use crate::utils; use crate::vfs::VFSSource; -const CURRENT_MAJOR_VERSION: i32 = 4; -const CURRENT_MINOR_VERSION: i32 = 0; -const COOKIE_SESSION: &str = "session"; -const COOKIE_USERNAME: &str = "username"; -const COOKIE_ADMIN: &str = "admin"; - pub fn get_routes() -> Vec { routes![ version, @@ -207,15 +203,9 @@ impl From for PathBuf { } } -#[derive(PartialEq, Debug, Serialize, Deserialize)] -pub struct Version { - pub major: i32, - pub minor: i32, -} - #[get("/version")] -fn version() -> Json { - let current_version = Version { +fn version() -> Json { + let current_version = dto::Version { major: CURRENT_MAJOR_VERSION, minor: CURRENT_MINOR_VERSION, }; diff --git a/src/service/rocket/api_tests.rs b/src/service/rocket/api_tests.rs index f792563..bd3dcac 100644 --- a/src/service/rocket/api_tests.rs +++ b/src/service/rocket/api_tests.rs @@ -8,6 +8,7 @@ use super::api; use crate::config; use crate::ddns; use crate::index; +use crate::service::dto; use crate::vfs; use super::test::get_test_environment; @@ -57,8 +58,8 @@ fn version() { assert_eq!(response.status(), Status::Ok); let response_body = response.body_string().unwrap(); - let response_json: api::Version = serde_json::from_str(&response_body).unwrap(); - assert_eq!(response_json, api::Version { major: 4, minor: 0 }); + let response_json: dto::Version = serde_json::from_str(&response_body).unwrap(); + assert_eq!(response_json, dto::Version { major: 4, minor: 0 }); } #[test]