diff --git a/Cargo.toml b/Cargo.toml index 7f81b0ac..c23b6b20 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,8 +33,11 @@ all. authors = ["Aliaksei BialĂ­auski ", "Ivanchuk Ivan "] [dependencies] +anyhow = "1.0.86" serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.117" -anyhow = "1.0.86" -axum = "0.7.5" tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros", "fs"] } +axum = "0.7.5" +log = { version = "0.4.21", features = [] } +env_logger = "0.11.3" +tempdir = "0.3.7" diff --git a/src/main.rs b/src/main.rs index e7dee931..50bb2df1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,10 +22,13 @@ use axum::routing::get; use axum::Router; mod routes; +mod xml; use crate::routes::home; +use crate::xml::storage::touch_storage; #[tokio::main] async fn main() { + touch_storage(Some("fakehub.xml")); let app = Router::new().route("/", get(home::home)); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap(); diff --git a/src/xml/mod.rs b/src/xml/mod.rs new file mode 100644 index 00000000..e263c1e6 --- /dev/null +++ b/src/xml/mod.rs @@ -0,0 +1,22 @@ +// The MIT License (MIT) +// +// Copyright (c) 2024 Aliaksei Bialiauski +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +pub mod storage; diff --git a/src/xml/storage.rs b/src/xml/storage.rs new file mode 100644 index 00000000..1d7cff2f --- /dev/null +++ b/src/xml/storage.rs @@ -0,0 +1,73 @@ +// The MIT License (MIT) +// +// Copyright (c) 2024 Aliaksei Bialiauski +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +use std::fs::File; + +use log::info; + +pub fn touch_storage(path: Option<&str>) -> File { + let location = path.unwrap_or("fakehub.xml"); + info!("Initializing XML storage: {location}"); + match File::create(location) { + Ok(file) => { + info!("'{location}' initialized"); + file + } + Err(err) => { + panic!("fakehub storage failed to initialize in '{location}': {err}") + } + } +} + +#[cfg(test)] +mod tests { + use tempdir::TempDir; + + use crate::xml::storage::touch_storage; + + #[test] + fn creates_xml_storage() -> anyhow::Result<()> { + let temp = TempDir::new("temp")?; + let path = temp.path().join("fakehub.xml"); + let storage = path.to_str(); + touch_storage(storage); + assert!( + path.exists(), + "storage file {:?} was not created, but should be", + storage + ); + Ok(()) + } + + #[test] + fn creates_xml_storage_with_different_name() -> anyhow::Result<()> { + let temp = TempDir::new("temp")?; + let path = temp.path().join("test.xml"); + let storage = path.to_str(); + touch_storage(storage); + assert!( + path.exists(), + "storage file {:?} was not created, but should be", + storage + ); + Ok(()) + } +}