Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serve and add Makefile #1

Merged
merged 7 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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"] }
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -16,20 +16,25 @@ 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
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.
3 changes: 2 additions & 1 deletion src/build_scss.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::fs;
use std::io::Write;
use colored::Colorize;

pub fn build_scss(p: &str, o: &str) -> Result<(), Box<grass::Error>>
{
let scss = grass::from_path(p, &grass::Options::default())?;
let mut file = fs::File::create(o)?;

file.write_all(scss.as_bytes())?;
println!("Done.");
println!("{}", "Done.".green().bold());
Ok(())
}
11 changes: 2 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 1 addition & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 2 additions & 0 deletions src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const DEFAULT_HTML: &str = "
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='styles.css'>
<title>My Page</title>
</head>
<body>
Expand All @@ -14,6 +15,7 @@ const DEFAULT_HTML: &str = "
<h2 id='article-head' class='header'>Hello World</h2>
<p>This is a simple HTML demo made by htmanager edit this html to make a website!</p>
</article>
<script src='./main.js' type='text/javascript'></script>
</body>
</html>";

Expand Down
57 changes: 24 additions & 33 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,32 @@
mod readfile;

use readfile::*;
// Replacement code for the custom Web Server
use std::str::FromStr;
use warp::{filters::BoxedFilter, http::Uri, path::FullPath, redirect, Filter, Reply};
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());
#[tokio::main]
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));

let listener = TcpListener::bind("localhost:".to_owned() + port).unwrap();
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());

for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream, filename);
}
warp::serve(routes).run(([0, 0, 0, 0], port)).await;
}

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}"
);
fn root_redirect() -> BoxedFilter<(impl Reply,)> {
warp::path::full()
.and_then(move |path: FullPath| async move {
let path = path.as_str();

stream.write_all(response.as_bytes()).unwrap();
}
if path.ends_with("/") || path.contains(".") {
return Err(warp::reject());
}

Ok(redirect::redirect(
Uri::from_str(&[path, "/"].concat()).unwrap(),
))
})
.boxed()
}
17 changes: 0 additions & 17 deletions src/server/readfile.rs

This file was deleted.