Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#9): storage init #20

Merged
merged 18 commits into from
Jun 5, 2024
Merged
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ all.
authors = ["Aliaksei Bialíauski <[email protected]>", "Ivanchuk Ivan <[email protected]>"]

[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"
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 22 additions & 0 deletions src/xml/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
73 changes: 73 additions & 0 deletions src/xml/storage.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
Loading