Add some info to log and errors handling

This commit is contained in:
g1n 2021-07-17 18:02:12 +00:00
parent 3a4a6d404b
commit 61a2c4912f
1 changed files with 11 additions and 9 deletions

View File

@ -1,6 +1,7 @@
use std::fs;
use std::path::Path;
use std::io::prelude::*;
use std::io::ErrorKind;
use std::net::TcpListener;
use std::net::TcpStream;
@ -34,6 +35,7 @@ fn main() {
if verbose {
println!("Work in progress");
}
println!("Starting gytheio on {}:{}", host, port);
let listener = TcpListener::bind(format!("{}:{}", host, port)).unwrap();
for stream in listener.incoming() {
@ -73,13 +75,7 @@ fn handle_connection(mut stream: TcpStream, root_dir: String) {
"4 Not found"
};
//let filename = "index.gmi";
/*
let _contents = "";
if _filename != "" {
let _contents = fs::read_to_string(_filename).unwrap();
} else { */
let mut _contents = "".to_string();
let mut _contents = String::new();
let full_path = format!("{}{}", root_dir, _path);
let mut exists = Path::new(&format!("{}{}", root_dir, _path).to_string()).exists();
if exists {
@ -88,7 +84,13 @@ fn handle_connection(mut stream: TcpStream, root_dir: String) {
} else {
exists = Path::new(&format!("{}{}/index.gmi", root_dir, _path).to_string()).exists();
if exists {
_contents = fs::read_to_string(format!("{}{}index.gmi", root_dir, _path)).unwrap();
_contents = fs::read_to_string(format!("{}{}index.gmi", root_dir, _path)).unwrap_or_else(|error| {
if error.kind() == ErrorKind::NotFound {
panic!("NotFound error!");
} else {
panic!("Other error!");
}
});
} else {
_contents = format!("=> ..\n");
let path_to_folder = full_path.clone();
@ -109,7 +111,7 @@ fn handle_connection(mut stream: TcpStream, root_dir: String) {
}
let response = format!(
"{}\r\n {}",
"{}\r\n{}",
status_line,
_contents
);