peak refactoring.
- main method was too complex - now its better, **to read**
This commit is contained in:
4
.idea/material_theme_project_new.xml
generated
4
.idea/material_theme_project_new.xml
generated
@@ -3,7 +3,9 @@
|
|||||||
<component name="MaterialThemeProjectNewConfig">
|
<component name="MaterialThemeProjectNewConfig">
|
||||||
<option name="metadata">
|
<option name="metadata">
|
||||||
<MTProjectMetadataState>
|
<MTProjectMetadataState>
|
||||||
<option name="userId" value="2d7a3dd6:1982d440666:-7ff2" />
|
<option name="migrated" value="true" />
|
||||||
|
<option name="pristineConfig" value="false" />
|
||||||
|
<option name="userId" value="25433ed1:19828f6ea6b:-7ffe" />
|
||||||
</MTProjectMetadataState>
|
</MTProjectMetadataState>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
<body>
|
<body>
|
||||||
<h1 class="specialboi">Hello from RSTTP</h1>
|
<h1 class="specialboi">Hello from RSTTP</h1>
|
||||||
|
<h2 style="font-style: italic;font-weight: bold;">Special Heading2</h2>
|
||||||
<p id="javascriptdate">
|
<p id="javascriptdate">
|
||||||
<p>
|
<p>
|
||||||
<script src="script.js"></script>
|
<script src="script.js"></script>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
|
|
||||||
function updateDateTime() {
|
function updateDateTime() {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
dateElement.textContent = `Current date and time: ${now.toLocaleString()}`;
|
dateElement.textContent = `Current date and time: ${now}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update once on load
|
// Update once on load
|
||||||
|
|||||||
76
src/main.rs
76
src/main.rs
@@ -258,59 +258,68 @@ async fn run_server(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct ServerConfig {
|
||||||
|
address: String,
|
||||||
|
base_path: Arc<PathBuf>,
|
||||||
|
cache: Arc<RwLock<FileCache>>,
|
||||||
|
stats: Arc<ServerStats>,
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> io::Result<()> {
|
async fn main() -> io::Result<()> {
|
||||||
let args: Vec<String> = std::env::args().collect();
|
let config = parse_arguments()?;
|
||||||
|
let server_handle = spawn_server(config.clone());
|
||||||
|
run_interface(config, server_handle).await
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_arguments() -> io::Result<ServerConfig> {
|
||||||
|
let args: Vec<String> = std::env::args().collect();
|
||||||
if args.len() < 4 {
|
if args.len() < 4 {
|
||||||
eprintln!("Usage: {} <address> <port> <path_to_serve>", args[0]);
|
eprintln!("Usage: {} <address> <port> <path_to_serve>", args[0]);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let address = &args[1];
|
|
||||||
let port = &args[2];
|
|
||||||
let path = Path::new(&args[3]);
|
let path = Path::new(&args[3]);
|
||||||
|
|
||||||
if !path.exists() || !path.is_dir() {
|
if !path.exists() || !path.is_dir() {
|
||||||
eprintln!("Error: The specified path does not exist or is not a directory");
|
eprintln!("Error: The specified path does not exist or is not a directory");
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let base_path = Arc::new(path.canonicalize()?);
|
let bind_addr = format!("{}:{}", &args[1], &args[2]);
|
||||||
let bind_addr = format!("{}:{}", address, port);
|
|
||||||
let cache = Arc::new(RwLock::new(FileCache::new(3600)));
|
|
||||||
let stats = Arc::new(ServerStats::new());
|
|
||||||
|
|
||||||
// Spawn the server in a separate task
|
Ok(ServerConfig {
|
||||||
let server_stats = Arc::clone(&stats);
|
address: bind_addr,
|
||||||
let server_cache = Arc::clone(&cache);
|
base_path: Arc::new(path.canonicalize()?),
|
||||||
let server_base_path = Arc::clone(&base_path);
|
cache: Arc::new(RwLock::new(FileCache::new(3600))),
|
||||||
let server_addr = bind_addr.clone();
|
stats: Arc::new(ServerStats::new()),
|
||||||
|
})
|
||||||
let server_handle = tokio::spawn(async move {
|
}
|
||||||
|
|
||||||
|
fn spawn_server(config: ServerConfig) -> tokio::task::JoinHandle<io::Result<()>> {
|
||||||
|
tokio::spawn(async move {
|
||||||
run_server(
|
run_server(
|
||||||
server_addr,
|
config.address,
|
||||||
server_base_path,
|
config.base_path,
|
||||||
server_cache,
|
config.cache,
|
||||||
server_stats,
|
config.stats,
|
||||||
).await
|
).await
|
||||||
});
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize and run TUI
|
async fn run_interface(
|
||||||
|
config: ServerConfig,
|
||||||
|
server_handle: tokio::task::JoinHandle<io::Result<()>>,
|
||||||
|
) -> io::Result<()> {
|
||||||
let mut tui = Tui::new()?;
|
let mut tui = Tui::new()?;
|
||||||
let mut background = false;
|
let mut background = false;
|
||||||
|
|
||||||
while !background {
|
while !background {
|
||||||
match tui.run(Arc::clone(&stats), bind_addr.clone()) {
|
match tui.run(Arc::clone(&config.stats), config.address.clone()) {
|
||||||
Ok(true) => background = true, // Ctrl+B pressed
|
Ok(true) => background = true, // Ctrl+B pressed
|
||||||
Ok(false) => {
|
Ok(false) => {
|
||||||
// Check for Ctrl+C or continue
|
if should_exit()? {
|
||||||
if event::poll(std::time::Duration::from_millis(100))? {
|
break;
|
||||||
if let Event::Key(key) = event::read()? {
|
|
||||||
if key.code == KeyCode::Char('c') && key.modifiers == KeyModifiers::CONTROL {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -326,4 +335,13 @@ async fn main() -> io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn should_exit() -> io::Result<bool> {
|
||||||
|
if event::poll(std::time::Duration::from_millis(100))? {
|
||||||
|
if let Event::Key(key) = event::read()? {
|
||||||
|
return Ok(key.code == KeyCode::Char('c') && key.modifiers == KeyModifiers::CONTROL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(false)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user