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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ all.
authors = ["Aliaksei Bialíauski <[email protected]>", "Ivanchuk Ivan <[email protected]>"]

[dependencies]
env_logger = "0.11.3"
anyhow = "1.0.86"
log = { version = "0.4.21", features = [] }
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"] }
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();
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;
64 changes: 64 additions & 0 deletions src/xml/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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::fmt::Error;
use std::fs::File;

use log::info;

pub fn touch_storage() {
let path = "fakehub.xml";
info!("Initializing XML storage: {path}");
let creating = File::create(path);
let _ = match creating {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be we should return the file from touch_storage and pass it to next function instead of sharing it by name?

Ok(file) => {
info!("'{path}' initialized");
Ok::<File, Error>(file)
}
Err(err) => {
panic!("fakehub storage failed to initialize in '{path}': {err}");
}
};
}

#[cfg(test)]
mod tests {
use std::fs;
use std::path::Path;

use crate::xml::storage::touch_storage;

fn clean() {
fs::remove_file("fakehub.xml").unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think better solution would be using temp files, like this

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@l3r8yJ we create this file inside touch_storage(), how we can create a temp file there? Or I miss something?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel you can pass a temp directory and create file inside of it. I think that even better would be if touch_storage will accept directory or smth like that

}

#[test]
fn creates_xml_storage() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

best practise is to return anyhow::Result<()> and add Ok(()) at the end of test

touch_storage();
let storage = "fakehub.xml";
let exists = Path::new(storage).exists();
assert!(
exists,
"storage file '{storage}' was not created, but should"
);
clean();
}
}
Loading