feat: Read path from a toml file

This commit is contained in:
Dendy 2024-09-15 11:50:20 +02:00
parent 1b84ce569e
commit 028412c6a2
4 changed files with 106 additions and 6 deletions

75
Cargo.lock generated
View File

@ -117,8 +117,15 @@ dependencies = [
"axum", "axum",
"serde", "serde",
"tokio", "tokio",
"toml",
] ]
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -173,6 +180,12 @@ version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32085ea23f3234fc7846555e85283ba4de91e21016dc0455a16286d87a292d64" checksum = "32085ea23f3234fc7846555e85283ba4de91e21016dc0455a16286d87a292d64"
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.9" version = "0.3.9"
@ -259,6 +272,16 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "indexmap"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.11" version = "1.0.11"
@ -447,6 +470,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_spanned"
version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "serde_urlencoded" name = "serde_urlencoded"
version = "0.7.1" version = "0.7.1"
@ -524,6 +556,40 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "toml"
version = "0.8.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.22.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]] [[package]]
name = "tower" name = "tower"
version = "0.4.13" version = "0.4.13"
@ -656,3 +722,12 @@ name = "windows_x86_64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winnow"
version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f"
dependencies = [
"memchr",
]

View File

@ -5,5 +5,6 @@ edition = "2021"
[dependencies] [dependencies]
axum = "0.7.5" axum = "0.7.5"
serde = "1.0.210" serde = { version = "1.0.210", features = ['derive'] }
tokio = "1.40.0" tokio = "1.40.0"
toml = "0.8.19"

2
config.toml Normal file
View File

@ -0,0 +1,2 @@
[settings]
base_directory = "/strg/drw"

View File

@ -1,4 +1,4 @@
use std::{fs, path::Path}; use std::{fs::{self, File}, io::Read, path::Path};
use axum::{ use axum::{
response::Html, response::Html,
@ -6,9 +6,24 @@ use axum::{
Router, Router,
}; };
use serde::Deserialize;
#[derive(Deserialize)]
struct Config {
settings: Settings,
}
#[derive(Deserialize)]
struct Settings {
base_directory: String,
}
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() { async fn main() {
let app = Router::new().route("/", get(list_files)); let config = read_config().expect("Failed to read config");
let app = Router::new()
.route("/", get(move || list_files(config.settings.base_directory.clone())));
let listener = tokio::net::TcpListener::bind("127.0.0.1:3004") let listener = tokio::net::TcpListener::bind("127.0.0.1:3004")
.await .await
@ -18,11 +33,18 @@ async fn main() {
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
} }
async fn list_files() -> Html<String> { fn read_config() -> Result<Config, Box<dyn std::error::Error>> {
let directory = "/strg/drw/"; let mut file = File::open("config.toml")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let config: Config = toml::from_str(&contents)?;
Ok(config)
}
async fn list_files(base_path: String) -> Html<String> {
let mut files_list = String::from("<h1>Found drawings:</h1><ul>"); let mut files_list = String::from("<h1>Found drawings:</h1><ul>");
if let Ok(files) = find_ora_files(directory) { if let Ok(files) = find_ora_files(&base_path) {
for file in files { for file in files {
files_list.push_str(&format!("<li>{}</li>", file)); files_list.push_str(&format!("<li>{}</li>", file));
} }