Skip to content

Commit

Permalink
Accept host and port as CLI arguments when --serve is used (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdenadai authored Oct 18, 2024
1 parent c87fc52 commit 6600b67
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct Cli {
#[arg(long)]
pub serve: bool,

#[arg(long, default_value = "localhost:8000")]
pub bind: String,

/// Path to custom configuration file (defaults to marmite.yaml)
#[arg(long, default_value = "marmite.yaml")]
pub config: String,
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn main() -> io::Result<()> {
let serve = args.serve;
let debug = args.debug;
let config_path = input_folder.join(args.config);
let bind_address: &str = args.bind.as_str();

// Initialize site data
let marmite = fs::read_to_string(&config_path).unwrap_or_else(|e| {
Expand Down Expand Up @@ -129,7 +130,7 @@ fn main() -> io::Result<()> {
// Serve the site if the flag was provided
if serve {
println!("Starting built-in HTTP server...");
server::start_server(output_folder.clone().into());
server::start_server(&bind_address, output_folder.clone().into());
}

println!("Site generated at: {}/", output_folder.display());
Expand Down
9 changes: 6 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use std::path::PathBuf;
use std::sync::Arc;
use tiny_http::{Response, Server};

pub fn start_server(output_folder: Arc<PathBuf>) {
let server = Server::http("localhost:8000").unwrap();
pub fn start_server(bind_address: &str, output_folder: Arc<PathBuf>) {
let server = Server::http(bind_address).unwrap();

println!("Server started at http://localhost:8000/ - Type ^C to stop.");
println!(
"Server started at http://{}/ - Type ^C to stop.",
bind_address
);

for request in server.incoming_requests() {
let response = match handle_request(&request, &output_folder) {
Expand Down

0 comments on commit 6600b67

Please sign in to comment.