sdk_storage/src/main.rs
4NK Dev a2c19e7e9e Externalize configuration and improve Docker compatibility
- Add STORAGE_DIR and PORT environment variables
- Update tests to use 0.0.0.0 instead of 127.0.0.1 for Docker compatibility
- Update documentation and changelog
2025-09-20 21:24:17 +00:00

56 lines
1.7 KiB
Rust

use std::env;
use async_std::task;
use async_std::fs::create_dir_all;
use sdk_storage::{StorageService, create_app};
use tide::log;
// Configuration via variables d'environnement avec valeurs par défaut
const DEFAULT_STORAGE_DIR: &str = "./storage";
const DEFAULT_PORT: u16 = 8080;
const DEFAULT_TTL: u64 = 86400;
#[async_std::main]
async fn main() -> tide::Result<()> {
// Initialize logging
env_logger::init();
log::info!("Starting server");
// Configuration via variables d'environnement
let storage_dir = std::env::var("STORAGE_DIR").unwrap_or_else(|_| DEFAULT_STORAGE_DIR.to_string());
let port = std::env::var("PORT")
.ok()
.and_then(|p| p.parse::<u16>().ok())
.unwrap_or(DEFAULT_PORT);
// Parse command line arguments
let args: Vec<String> = env::args().collect();
let no_ttl_permanent = args.iter().any(|arg| arg == "--permanent");
if no_ttl_permanent {
println!("No-TTL requests will be treated as permanent");
} else {
println!("No-TTL requests will use default TTL of {} seconds", DEFAULT_TTL);
}
let svc = StorageService::new(&storage_dir);
create_dir_all(&storage_dir).await.expect("Failed to create storage directory.");
// background cleanup loop
let svc_clone = svc.clone();
task::spawn(async move {
loop {
if let Err(e) = svc_clone.cleanup_expired_files_once().await {
eprintln!("cleanup error: {}", e);
}
task::sleep(std::time::Duration::from_secs(60)).await;
}
});
let mut app = create_app(no_ttl_permanent, &storage_dir);
app.listen(format!("0.0.0.0:{}", port)).await?;
println!("Server running at http://0.0.0.0:{}", port);
Ok(())
}