Skip to content

Commit

Permalink
Move gui and cli versions to different files
Browse files Browse the repository at this point in the history
- Move common code from main to dedicated files for reusability
- Optimize on the cross-build script
  • Loading branch information
joaofl committed Aug 21, 2024
1 parent bf9559a commit b151e09
Show file tree
Hide file tree
Showing 10 changed files with 403 additions and 318 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ default = ["ui"]

[[bin]]
name = "quick-serve-gui"
path = "src/main.rs"
path = "src/main_gui.rs"
required-features = ["ui"]

[[bin]]
name = "quick-serve"
path = "src/main.rs"
path = "src/main_cli.rs"

[dependencies]
# For cross-compiling for windows
Expand Down
33 changes: 15 additions & 18 deletions cross-build-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,21 @@ mkdir -p target/assets 2>/dev/null || true

# Cross-compile for each target platform
for target in "${TARGETS[@]}"; do
ext=""
if [ $target = "x86_64-pc-windows-gnu" ]; then
ext=".exe"
fi

# rustup target add $target

echo; echo "########################################################################################"; echo
echo "Cross-compiling gui version for $target..."
# cargo zigbuild --release --target "$target" --jobs $(nproc) --bin quick-serve
cross build --release --target "$target" --jobs $(nproc) --bin quick-serve
cp -vf target/${target}/release/quick-serve${ext} target/assets/quick-serve-gui-${target}${ext} || true

echo; echo "########################################################################################"; echo
echo "Cross-compiling the headless version for $target..."
# cargo zigbuild --release --target "$target" --no-default-features --jobs $(nproc)
cross build --release --target "$target" --no-default-features --jobs $(nproc) --bin quick-serve
cp -vf target/${target}/release/quick-serve${ext} target/assets/quick-serve-${target}${ext} || true
echo; echo "########################################################################################"; echo
echo "Cross-compiling gui version for $target..."

cross build --release --target "$target"
done

# Copy the compiled files
for target in "${TARGETS[@]}"; do
ext=""
if [ $target = "x86_64-pc-windows-gnu" ]; then
ext=".exe"
fi

cp -vf target/${target}/release/quick-serve-gui${ext} target/assets/quick-serve-gui-${target}${ext} || true
cp -vf target/${target}/release/quick-serve${ext} target/assets/quick-serve-${target}${ext} || true
done

exit 0
70 changes: 70 additions & 0 deletions src/common/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@


use clap::Parser;
use clap::ArgAction;

use crate::Protocol;

#[derive(Parser, Debug)]
#[command(author, version, about = "Quick-Serve", long_about = "Instant file serving made easy")]
pub struct Cli {

// TODO: Still have to figure a way to **not** show the
// headless option when running headless
#[arg(
help = "Headless",
long, required = false,
action = ArgAction::SetTrue,
)] pub headless: bool,

#[arg(
help = "Bind IP",
short, long, required = false,
default_value = "127.0.0.1",
value_name = "IP",
require_equals = true,
)] pub bind_ip: String,

#[arg(
help = "Directory to serve",
short = 'd', long, required = false,
default_value = "/tmp/",
value_name = "PATH",
require_equals = true,
)] pub serve_dir: String,

#[arg(
help = "Verbose logging",
short, long, required = false,
action = clap::ArgAction::Count,
)] pub verbose: u8,

#[arg(
default_missing_value = Protocol::Http.get_default_port().to_string(),
help = format!("Start the HTTP server [default port: {}]", Protocol::Http.get_default_port().to_string()),
long, required = false,
num_args = 0..=1,
require_equals = true,
value_name = "PORT",
)] pub http: Option<u32>,

#[arg(
default_missing_value = Protocol::Ftp.get_default_port().to_string(),
help = format!("Start the FTP server [default port: {}]", Protocol::Ftp.get_default_port().to_string()),
long, required = false,
num_args = 0..=1,
require_equals = true,
value_name = "PORT",
)] pub ftp: Option<u32>,

#[arg(
default_missing_value = Protocol::Tftp.get_default_port().to_string(),
help = format!("Start the TFTP server [default port: {}]", Protocol::Tftp.get_default_port().to_string()),
long, required = false,
num_args = 0..=1,
require_equals = true,
value_name = "PORT",
)] pub tftp: Option<u32>,
}


4 changes: 4 additions & 0 deletions src/common/messages.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Disable warning since `receiver` is not used in this file,
// while its a placeholder for the future with too much impact to remove
#![allow(dead_code)]

use crate::servers::server::Protocol;
use tokio::sync::broadcast::{channel, Receiver, Sender};

Expand Down
4 changes: 4 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
pub use args::*;
pub use messages::*;
pub use utils::*;

// Import and re-export the submodule files.
pub mod args;
pub mod messages;
pub mod utils;
21 changes: 21 additions & 0 deletions src/common/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::process::exit;

use log::info;
use log::warn;

pub fn setup_ctrlc_handler() {
////////////////////////////////////////////////////////////////////////
// Ctrl+c handler from here on
////////////////////////////////////////////////////////////////////////
// TODO: only add handle if any server has been invoked
// Add handle for Ctrl+C
tokio::spawn(async move {
ctrlc::set_handler(move || {
warn!("Ctrl+C received. Closing connections and exiting.");
//TODO: send a message to stop all servers and wait 10

exit(1);
}).expect("Error setting Ctrl+C handler");
info!("Press Ctrl+C to exit.");
});
}
Loading

0 comments on commit b151e09

Please sign in to comment.