Skip to content

Commit

Permalink
Merge pull request #9 from jubako/better_serve
Browse files Browse the repository at this point in the history
Move serve subcommand in its own submodule.
  • Loading branch information
mgautierfr authored Feb 18, 2024
2 parents d4a2a55 + 7d317c6 commit c874f74
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
9 changes: 4 additions & 5 deletions libwaj/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::{AllProperties, Builder, Entry, Reader};
use crate::Waj;
use ascii::IntoAsciiString;
use jbk::reader::builder::PropertyBuilderTrait;
use log::{error, info, trace};
use log::{debug, error, trace, warn};
use percent_encoding::{percent_decode, percent_encode, CONTROLS};
use std::borrow::Cow;
use std::net::ToSocketAddrs;
Expand Down Expand Up @@ -142,7 +142,7 @@ impl Server {
}
}
}
info!("{url} not found");
warn!("{url} not found");
if let Ok(Entry::Content(e)) = waj.get_entry::<FullBuilder>("404.html") {
let reader = waj.get_reader(e.content_address)?;
let mut response = Response::new(
Expand Down Expand Up @@ -171,7 +171,6 @@ impl Server {

pub fn serve(&self, address: &str) -> jbk::Result<()> {
let addr = address.to_socket_addrs().unwrap().next().unwrap();
info!("Serving on address {addr}");
let server = Arc::new(tiny_http::Server::http(addr).unwrap());
let mut guards = Vec::with_capacity(4);
let next_request_id = Arc::new(AtomicUsize::new(0));
Expand All @@ -193,7 +192,7 @@ impl Server {
}
let request = match server.recv_timeout(std::time::Duration::from_millis(500)) {
Err(e) => {
info!("error {e}");
error!("error {e}");
break;
}
Ok(rq) => match rq {
Expand All @@ -211,7 +210,7 @@ impl Server {

let now = std::time::Instant::now();

println!("[{request_id}] : {} {url}", request.method());
debug!("[{request_id}] : {} {url}", request.method());

let etag_match =
if let Some(request_etag) = get_etag_from_headers(request.headers()) {
Expand Down
17 changes: 4 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod create;
mod list;
mod serve;

use anyhow::{Context, Result};
use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand};
use log::error;
use std::path::PathBuf;
Expand Down Expand Up @@ -41,7 +42,7 @@ enum Commands {
Create(create::Options),

#[command(arg_required_else_help = true)]
Serve(Serve),
Serve(serve::Options),

#[command(arg_required_else_help = true)]
List(list::Options),
Expand Down Expand Up @@ -110,17 +111,7 @@ fn run() -> Result<()> {
None => Ok(Cli::command().print_help()?),
Some(c) => match c {
Commands::Create(options) => create::create(options),
Commands::Serve(options) => {
if options.verbose > 0 {
println!(
"Serve archive {:?} at {:?}",
options.infile, options.address,
);
}
let server = waj::Server::new(&options.infile)
.with_context(|| format!("Opening {:?}", options.infile))?;
Ok(server.serve(&options.address)?)
}
Commands::Serve(options) => serve::serve(options),
Commands::List(options) => list::list(options),
},
}
Expand Down
29 changes: 29 additions & 0 deletions src/serve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use anyhow::{Context, Result};
use clap::Parser;
use log::info;
use std::path::PathBuf;

/// Serve the waj archive on the web.
#[derive(Parser)]
pub struct Options {
/// Archive to serve
#[arg(value_parser)]
infile: PathBuf,

/// On which address serve the archive.
#[arg(value_parser, default_value = "localhost:1234")]
address: String,

#[arg(from_global)]
verbose: u8,
}

pub fn serve(options: Options) -> Result<()> {
info!(
"Serve archive {:?} at {:?}",
options.infile, options.address,
);
let server = waj::Server::new(&options.infile)
.with_context(|| format!("Opening {:?}", options.infile))?;
Ok(server.serve(&options.address)?)
}

0 comments on commit c874f74

Please sign in to comment.