wala-rust

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

commit 73be106c9b105f4a5c99fc157952c99d75e2fbcf
parent cc25d1cebe19be1082ca37b07cf2bde4d5536e77
Author: lash <dev@holbrook.no>
Date:   Wed,  6 Jul 2022 05:14:08 +0000

Add settable host and port

Diffstat:
MCargo.toml | 1+
Asrc/arg.rs | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main.rs | 9+++++++--
Msrc/request.rs | 2++
4 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -14,6 +14,7 @@ hex = "^0.4" tempfile = "^3.3.0" mime = "^0.3.13" ascii = "^1.0.0" +clap = "^2.34.0" [dependencies.tree_magic] version = "^0.2.3" diff --git a/src/arg.rs b/src/arg.rs @@ -0,0 +1,66 @@ +use clap::{ + App, + Arg, + ArgMatches, + SubCommand, +}; + +pub struct Settings { + pub host: String, + pub port: u16, +} + +const BIND_HOST: &str = "0.0.0.0"; +const BIND_PORT: u16 = 8000; + +impl Settings { + + pub fn new() -> Settings { + Settings { + host: BIND_HOST.to_string(), + port: BIND_PORT, + } + } + + fn bind_from_args(&mut self, arg: &ArgMatches) { + match arg.value_of("host") { + Some(v) => { + self.host = v.to_string(); + }, + _ => {}, + }; + + match arg.value_of("port") { + Some(v) => { + let port = u16::from_str_radix(&v, 10); + self.port = port.unwrap(); + }, + _ => {}, + }; + } + + pub fn from_args() -> Settings { + let mut o = App::new("wala"); + o = o.version("0.0.1"); + o = o.author("Louis Holbrook <dev@holbrook.no>"); + o = o.arg( + Arg::with_name("host") + .long("host") + .short("h") + .value_name("Host or ip to bind server to.") + .takes_value(true) + ); + o = o.arg( + Arg::with_name("port") + .long("port") + .short("p") + .value_name("Port to bind server to") + .takes_value(true) + ); + + let arg_matches = o.get_matches(); + let mut settings = Settings::new(); + settings.bind_from_args(&arg_matches); + settings + } +} diff --git a/src/main.rs b/src/main.rs @@ -40,6 +40,9 @@ use record::{ mod request; use request::process_method; +mod arg; +use arg::Settings; + use log::{debug, info, error}; use tempfile::tempfile; @@ -241,6 +244,7 @@ fn process_meta(req: &Request, path: &Path, digest: Vec<u8>) -> Option<Mime> { } } + #[cfg(feature = "meta")] match m { Some(v) => { match meta::register_type(path, digest, v) { @@ -260,10 +264,11 @@ fn process_meta(req: &Request, path: &Path, digest: Vec<u8>) -> Option<Mime> { fn main() { env_logger::init(); + let settings = Settings::from_args(); let base_path = Path::new("."); - let ip_addr = Ipv4Addr::from_str("0.0.0.0").unwrap(); - let tcp_port: u16 = 8001; + let ip_addr = Ipv4Addr::from_str(&settings.host).unwrap(); + let tcp_port: u16 = settings.port; let sock_addr = SocketAddrV4::new(ip_addr, tcp_port); let srv_cfg = ServerConfig{ addr: sock_addr, diff --git a/src/request.rs b/src/request.rs @@ -19,6 +19,7 @@ use crate::auth::{ }; use std::io::Read; +#[cfg(feature = "meta")] use crate::meta::get_type as get_meta_type; use log::{ @@ -117,6 +118,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s f: Some(v), m: None, }; + #[cfg(feature = "meta")] match get_meta_type(path, digest) { Some(v) => { res.m = Some(v);