From 16670a9972b86a78e48ea750d4639dcaed6613bd Mon Sep 17 00:00:00 2001 From: Matteo Rosato <85708462+MRRcode979@users.noreply.github.com> Date: Sat, 26 Nov 2022 16:14:43 -0500 Subject: [PATCH 1/7] Delete readfile.rs --- src/server/readfile.rs | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 src/server/readfile.rs diff --git a/src/server/readfile.rs b/src/server/readfile.rs deleted file mode 100644 index 6be266e..0000000 --- a/src/server/readfile.rs +++ /dev/null @@ -1,17 +0,0 @@ -use std::io; -use std::io::prelude::*; -use std::fs::File; -use std::env; -use std::path::PathBuf; - -pub struct Filename { - path: PathBuf, -} - -impl Filename { - pub fn from_path(path: &str) -> Self { - let mut fullpath = env::current_dir().unwrap(); - fullpath.push(&path[1..]); - Self { path: fullpath } - } -} From 17b1f21523bc3731b9393317d50174ab302f29c5 Mon Sep 17 00:00:00 2001 From: Matteo Rosato <85708462+MRRcode979@users.noreply.github.com> Date: Sat, 26 Nov 2022 16:16:57 -0500 Subject: [PATCH 2/7] Update server.rs --- src/server.rs | 68 +++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/src/server.rs b/src/server.rs index 366fa5e..a4e76e6 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,41 +1,33 @@ -mod readfile; - -use readfile::*; -use colored::Colorize; -use std::{ - fs, - io::{prelude::*, BufReader}, - net::{TcpListener, TcpStream}, -}; - -pub fn serve(filename: &str, port: &str) { - println!("{} {}", "WARNING:".yellow().bold(), "The serve function of htmanager is still in heavy development".bold()); - println!("local (single threaded) dev server hosted at port {} (^C to force shutdown dev server)", port.cyan().italic()); - - let listener = TcpListener::bind("localhost:".to_owned() + port).unwrap(); - - for stream in listener.incoming() { - let stream = stream.unwrap(); - handle_connection(stream, filename); - } -} +// Replacement code for the custom Web Server +use std::str::FromStr; +use warp::{filters::BoxedFilter, http::Uri, path::FullPath, redirect, Filter, Reply}; + + -fn handle_connection(mut stream: TcpStream, path: &str) { - let buf_reader = BufReader::new(&mut stream); - let request_line = buf_reader.lines().next().unwrap().unwrap(); - - if request_line == "GET / HTTP/1.1" { - let status_line = "HTTP/1.1 200 OK"; - - let file = Filename::from_path(path); - let contents = fs::read_to_string(path).unwrap(); - let length = contents.len(); - - let response = - format!( - "{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}" - ); - - stream.write_all(response.as_bytes()).unwrap(); +fn main() { +serve(8080); } + +#[tokio::main] +async fn serve(port: u16) { + let current_dir = std::env::current_dir().expect("failed to read current directory"); + + let routes = root_redirect().or(warp::fs::dir(current_dir)); + + warp::serve(routes).run(([127, 0, 0, 1], port)).await; } + +fn root_redirect() -> BoxedFilter<(impl Reply,)> { + warp::path::full() + .and_then(move |path: FullPath| async move { + let path = path.as_str(); + + if path.ends_with("/") || path.contains(".") { + return Err(warp::reject()); + } + + Ok(redirect::redirect( + Uri::from_str(&[path, "/"].concat()).unwrap(), + )) + }) + .boxed() From 6e538b503af7a7930b4be0aae715c1e4676f41b1 Mon Sep 17 00:00:00 2001 From: Matteo Rosato <85708462+MRRcode979@users.noreply.github.com> Date: Sun, 27 Nov 2022 08:51:43 -0500 Subject: [PATCH 3/7] Update Cargo.toml --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 39e1ef7..b290b8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ license = "GPLv3+" colored = "2.0.0" grass = "0.11.1" clap = { version = "3.2.20", features = ["derive"] } +warp = "0.3.3" From edaebedca6f001030a9884651e8a0e028679d8ee Mon Sep 17 00:00:00 2001 From: Matteo Rosato <85708462+MRRcode979@users.noreply.github.com> Date: Sun, 27 Nov 2022 15:22:39 -0500 Subject: [PATCH 4/7] Add files via upload --- Cargo.toml | 10 +++++++++- Makefile | 9 +++++++++ README.md | 2 +- src/build_scss.rs | 3 ++- src/cli.rs | 11 ++--------- src/main.rs | 7 +------ src/new.rs | 2 ++ src/server.rs | 15 +++++++-------- src/server/readfile.rs | 17 +++++++++++++++++ 9 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 Makefile create mode 100644 src/server/readfile.rs diff --git a/Cargo.toml b/Cargo.toml index b290b8d..2d05bd4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,16 @@ license = "GPLv3+" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[profile.release] +strip = "debuginfo" +opt-level = 3 + +[[bin]] +name = "htmanager" + [dependencies] colored = "2.0.0" -grass = "0.11.1" +grass = "0.11.2" clap = { version = "3.2.20", features = ["derive"] } warp = "0.3.3" +tokio = { version = "1", features = ["full"] } diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..344069f --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +install: + echo "installing..." + cargo build --release + sudo mv ./target/release/htmanager /usr/bin + echo "Done!" + htmanager --help + +uninstall: + sudo rm -fv /usr/bin/htmanager diff --git a/README.md b/README.md index 72acf3c..5957eb9 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ htmanager is a high end website project manager written in Rust and is 100% open source forever. -**Please feel free to contribute!** +**I will be accepting contributions.** ## What can htmanager do? diff --git a/src/build_scss.rs b/src/build_scss.rs index 6a6de00..c9cd87a 100644 --- a/src/build_scss.rs +++ b/src/build_scss.rs @@ -1,5 +1,6 @@ use std::fs; use std::io::Write; +use colored::Colorize; pub fn build_scss(p: &str, o: &str) -> Result<(), Box> { @@ -7,6 +8,6 @@ pub fn build_scss(p: &str, o: &str) -> Result<(), Box> let mut file = fs::File::create(o)?; file.write_all(scss.as_bytes())?; - println!("Done."); + println!("{}", "Done.".green().bold()); Ok(()) } diff --git a/src/cli.rs b/src/cli.rs index f6a8a29..62c38cc 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -16,15 +16,8 @@ redistribute it under certain conditions .author(env!("CARGO_PKG_AUTHORS")) .subcommand( App::new("serve") - .about("Serve project for local development [WARNING: The serve function is in beta!]") - .arg( - Arg::with_name("source") - .help("The html source manager") - .short('s') - .long("source") - .takes_value(true) - .default_value("") - ) + .about("Serve project for local development on 127.0.0.1 and localhost + [WARNING: The serve function is in beta!]") .arg( Arg::with_name("port") .help("the port to run the dev server on") diff --git a/src/main.rs b/src/main.rs index 5973e5c..9ff993e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,11 +32,6 @@ create_project(matches.value_of("directory_name").unwrap()); } if let Some(ref matches) = matches.subcommand_matches("serve") { -if matches.value_of("source") == Some("") { -println!("{} {}", "Error:".red().bold(), "No html source file specified use the -s flag then the filename".cyan()); -std::process::exit(0); -} else { -serve(matches.value_of("source").unwrap(), matches.value_of("port").unwrap()); -} +serve(matches.value_of("port").expect("Unable to parse argument").parse().unwrap()); } } diff --git a/src/new.rs b/src/new.rs index f2139a6..679f218 100644 --- a/src/new.rs +++ b/src/new.rs @@ -6,6 +6,7 @@ const DEFAULT_HTML: &str = " + My Page @@ -14,6 +15,7 @@ const DEFAULT_HTML: &str = "

Hello World

This is a simple HTML demo made by htmanager edit this html to make a website!

+ "; diff --git a/src/server.rs b/src/server.rs index a4e76e6..4a1cf3a 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,20 +1,18 @@ // Replacement code for the custom Web Server use std::str::FromStr; use warp::{filters::BoxedFilter, http::Uri, path::FullPath, redirect, Filter, Reply}; - - - -fn main() { -serve(8080); -} +use colored::Colorize; #[tokio::main] -async fn serve(port: u16) { +pub async fn serve(port: u16) { let current_dir = std::env::current_dir().expect("failed to read current directory"); let routes = root_redirect().or(warp::fs::dir(current_dir)); - warp::serve(routes).run(([127, 0, 0, 1], port)).await; + println!("{} {}", "WARNING:".yellow().bold(), "The serve function of htmanager is still in heavy development".bold()); + println!("local dev server hosted at port {} (^C to force shutdown dev server)", port.to_string().cyan().italic()); + + warp::serve(routes).run(([0, 0, 0, 0], port)).await; } fn root_redirect() -> BoxedFilter<(impl Reply,)> { @@ -31,3 +29,4 @@ fn root_redirect() -> BoxedFilter<(impl Reply,)> { )) }) .boxed() +} diff --git a/src/server/readfile.rs b/src/server/readfile.rs new file mode 100644 index 0000000..6be266e --- /dev/null +++ b/src/server/readfile.rs @@ -0,0 +1,17 @@ +use std::io; +use std::io::prelude::*; +use std::fs::File; +use std::env; +use std::path::PathBuf; + +pub struct Filename { + path: PathBuf, +} + +impl Filename { + pub fn from_path(path: &str) -> Self { + let mut fullpath = env::current_dir().unwrap(); + fullpath.push(&path[1..]); + Self { path: fullpath } + } +} From 35f200113208b01ad2c253f440ebb7125ecade7c Mon Sep 17 00:00:00 2001 From: Matteo Rosato <85708462+MRRcode979@users.noreply.github.com> Date: Sun, 27 Nov 2022 15:26:46 -0500 Subject: [PATCH 5/7] Quick refactor --- README.md | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5957eb9..4b87e4f 100644 --- a/README.md +++ b/README.md @@ -21,15 +21,22 @@ Add multithreading to the serve function. Makefile / installation script? # Install -first clone this repository +first clone this repository or download & extract the zip. -next go into the project directory +git clone https://github.com/MRRcode979/htmanager.git -then run: cargo build --relase +Next go into the htmanager directory and installit with the following command: -finally run the executable and test if it's working by typing: +make install -./target/release/htmanager --help +htmanager should now be installed. + +# Uninstall +Go into the htmanager directory and type in the following command: + +make uninstall + +htmanager should now be uninstalled. That is it for now! Feel free to contribute. From f68e5fb186f12dac2a46a856b61dfbdd10336c6a Mon Sep 17 00:00:00 2001 From: Matteo Rosato <85708462+MRRcode979@users.noreply.github.com> Date: Sun, 27 Nov 2022 15:27:21 -0500 Subject: [PATCH 6/7] Quick refactor --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 4b87e4f..50951de 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,7 @@ if you use external sources from your directory (Like a script tag linking an ex it won't work because htmanager does not serve the whole directory this should be fixed soon though. ## TODO: -Add directory listing to the serve function. -Add multithreading to the serve function. -Makefile / installation script? +blank for now # Install first clone this repository or download & extract the zip. From efcc3a94b9cd6b9b350e2f00332d38d528d8db2f Mon Sep 17 00:00:00 2001 From: Matteo Rosato <85708462+MRRcode979@users.noreply.github.com> Date: Sun, 27 Nov 2022 15:27:56 -0500 Subject: [PATCH 7/7] Delete src/server directory --- src/server/readfile.rs | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 src/server/readfile.rs diff --git a/src/server/readfile.rs b/src/server/readfile.rs deleted file mode 100644 index 6be266e..0000000 --- a/src/server/readfile.rs +++ /dev/null @@ -1,17 +0,0 @@ -use std::io; -use std::io::prelude::*; -use std::fs::File; -use std::env; -use std::path::PathBuf; - -pub struct Filename { - path: PathBuf, -} - -impl Filename { - pub fn from_path(path: &str) -> Self { - let mut fullpath = env::current_dir().unwrap(); - fullpath.push(&path[1..]); - Self { path: fullpath } - } -}