Skip to content

Commit

Permalink
Update binserve deps
Browse files Browse the repository at this point in the history
  • Loading branch information
logankaser committed Feb 17, 2024
1 parent 0198b80 commit 21416e7
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 80 deletions.
32 changes: 16 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
[package]
name = "binserve"
version = "0.2.0"
version = "0.2.1"
edition = "2021"

[dependencies]
actix-files = "0.6.0"
actix-web = { version = "4.0.1", features = ["rustls"] }
actix-web-lab = "0.16.1"
ahash = "0.7.6"
actix-web = { version = "4.5.1", features = ["rustls-0_22"] }
actix-web-lab = "0.20.2"
ahash = "0.8.8"
anyhow = "1.0.57"
clap = "3.1.18"
clap = "4.5.1"
colored = "2.0.0"
compact_str = "0.4.0"
dashmap = "5.3.4"
env_logger = "0.9.0"
etag = { version = "3.0.0", features = ["std"] }
handlebars = "4.3.1"
jwalk = "0.6.0"
minify-html-onepass = "0.8.0"
compact_str = "0.7.1"
dashmap = "5.5.3"
env_logger = "0.11.2"
etag = { version = "4.0.0", features = ["std"] }
handlebars = "5.1.0"
jwalk = "0.8.1"
minify-html-onepass = "0.15.0"
new_mime_guess = "4.0.1"
notify = "4.0.17"
notify-debouncer-mini = "0.4.1"
num_cpus = "1.13.1"
once_cell = { version = "1.12.0", features = ["parking_lot"] }
parking_lot = "0.12.1"
rustls = "0.20.6"
rustls-pemfile = "1.0.0"
rustls = "0.22.2"
rustls-pemfile = "2.1.0"
serde = { version = "1.0.137", features = ["derive"] }
serde_json = "1.0.81"

[profile.release]
opt-level = 3
codegen-units = 1
panic = 'abort'
panic = "abort"
lto = "thin"
debug = false
incremental = false
Expand Down
9 changes: 3 additions & 6 deletions src/cli/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,18 @@ pub fn args() -> ArgMatches {
.long("host")
.value_name("HOST IP/DOMAIN:PORT")
.help("Host to run binserve on.")
.required(false)
.takes_value(true))
.required(false))
.arg(Arg::new("tls_key")
.short('k')
.long("key")
.value_name("TLS KEY")
.help("TLS key file.")
.required(false)
.takes_value(true))
.required(false))
.arg(Arg::new("tls_cert")
.short('c')
.long("cert")
.value_name("TLS CERT")
.help("TLS cert file.")
.required(false)
.takes_value(true))
.required(false))
.get_matches()
}
6 changes: 3 additions & 3 deletions src/core/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ pub fn init() -> anyhow::Result<()> {

// override with cli configurations if any
let cli_args = interface::args();
if let Some(host) = cli_args.value_of("host") {
if let Some(host) = cli_args.get_one::<String>("host") {
config.server.host = host.into();
}
if let Some(tls_key) = cli_args.value_of("tls_key") {
if let Some(tls_key) = cli_args.get_one::<String>("tls_key") {
config.server.tls.key = tls_key.into();
}
if let Some(tls_cert) = cli_args.value_of("tls_cert") {
if let Some(tls_cert) = cli_args.get_one::<String>("tls_cert") {
config.server.tls.key = tls_cert.into();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pub(super) mod templates;
pub(super) mod tls;
pub(super) mod watcher;

pub static VERSION: &str = "0.2.0";
pub static VERSION: &str = "0.2.1";
2 changes: 1 addition & 1 deletion src/core/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub async fn run_server(config_state: BinserveConfig) -> std::io::Result<()> {
let tls_config = tls::load_rustls_config().unwrap();

// bind the TLS host and the rustls configuration
http_server = http_server.bind_rustls(tls_host, tls_config)?;
http_server = http_server.bind_rustls_0_22(tls_host, tls_config)?;
}

http_server.run().await
Expand Down
23 changes: 6 additions & 17 deletions src/core/tls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustls::{Certificate, PrivateKey, ServerConfig};
use rustls::ServerConfig;
use rustls_pemfile::{certs, pkcs8_private_keys};

use std::fs::File;
Expand All @@ -15,9 +15,7 @@ pub fn load_rustls_config() -> Result<rustls::ServerConfig> {
let config_state = &*CONFIG_STATE.lock();

// init server config builder with safe defaults
let config = ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth();
let config = ServerConfig::builder().with_no_client_auth();

let cert_file_path = &config_state.server.tls.cert;
let cert_key_path = &config_state.server.tls.key;
Expand All @@ -34,23 +32,14 @@ pub fn load_rustls_config() -> Result<rustls::ServerConfig> {
})?);

// convert files to key/cert objects
let cert_chain = certs(cert_file)
.unwrap()
.into_iter()
.map(Certificate)
.collect();

let mut keys: Vec<PrivateKey> = pkcs8_private_keys(key_file)
.unwrap()
.into_iter()
.map(PrivateKey)
.collect();
let cert_chain = certs(cert_file).collect::<Result<Vec<_>, _>>()?;
let key = pkcs8_private_keys(key_file).find_map(Result::ok);

// exit if no keys could be parsed
if keys.is_empty() {
if key.is_none() {
push_message(Type::Error, "Could not locate PKCS 8 private keys.");
std::process::exit(1);
}

Ok(config.with_single_cert(cert_chain, keys.remove(0))?)
Ok(config.with_single_cert(cert_chain, key.unwrap().into())?)
}
77 changes: 41 additions & 36 deletions src/core/watcher.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
use notify_debouncer_mini::{new_debouncer, notify::RecursiveMode};

use compact_str::CompactString;

use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;
use std::time::Duration;

Expand All @@ -26,14 +26,16 @@ pub fn hot_reload_files() -> anyhow::Result<()> {

// Create a watcher object, delivering debounced events.
// The notification back-end is selected based on the platform.
let mut watcher = watcher(tx, Duration::from_secs(1))?;
let mut debouncer = new_debouncer(Duration::from_secs(1), tx)?;

let mut file_mapping: HashMap<PathBuf, CompactString> = HashMap::with_capacity(ROUTEMAP.len());

// add the binserve config file to the hot reloader
let config_file_path = PathBuf::from(CONFIG_FILE);
let abs_config_path = fs::canonicalize(config_file_path)?;
watcher.watch(CONFIG_FILE, RecursiveMode::Recursive)?;
debouncer
.watcher()
.watch(Path::new(CONFIG_FILE), RecursiveMode::Recursive)?;

// Add a path to be watched. All files and directories at that path and
// below will be monitored for changes.
Expand All @@ -51,7 +53,9 @@ pub fn hot_reload_files() -> anyhow::Result<()> {
let abs_file_path = fs::canonicalize(file_path)?;

// add to the system filesystem events watch list
watcher.watch(file_path, RecursiveMode::Recursive)?;
debouncer
.watcher()
.watch(file_path, RecursiveMode::Recursive)?;

// map them to the corresponding keys in the routemap
file_mapping.insert(abs_file_path, key.to_owned());
Expand All @@ -60,41 +64,42 @@ pub fn hot_reload_files() -> anyhow::Result<()> {

loop {
match rx.recv() {
Ok(event) => {
match event {
DebouncedEvent::Write(file_path)
| DebouncedEvent::Create(file_path)
| DebouncedEvent::Remove(file_path) => {
if file_path == abs_config_path {
// read the configuration file
let config = BinserveConfig::read()?;

// prepare template partials
let handlebars_handle = templates::render_templates(&config)?;

// prepare routes table
RouteHandle::add_routes(&config.routes, &handlebars_handle)?;
}

if let Some(route_key) = file_mapping.get(&file_path) {
// read the configuration file
let config = BinserveConfig::read()?;

// prepare template partials
let handlebars_handle = templates::render_templates(&config)?;

// reload the file state and update the global program state
RouteHandle::associate_files_to_routes(
&route_key.to_string(),
&file_path,
&handlebars_handle,
)?;
}
Ok(Ok(events)) => {
for event in events {
if event.path == abs_config_path {
// read the configuration file
let config = BinserveConfig::read()?;

// prepare template partials
let handlebars_handle = templates::render_templates(&config)?;

// prepare routes table
RouteHandle::add_routes(&config.routes, &handlebars_handle)?;
}

if let Some(route_key) = file_mapping.get(&event.path) {
// read the configuration file
let config = BinserveConfig::read()?;

// prepare template partials
let handlebars_handle = templates::render_templates(&config)?;

// reload the file state and update the global program state
RouteHandle::associate_files_to_routes(
&route_key.to_string(),
&event.path,
&handlebars_handle,
)?;
}
_ => (),
}
}
Err(e) => {
println!(
"[!] filesystem watch channel error (binserve hot reload): {:?}",
e
)
}
Ok(Err(e)) => {
println!("[!] filesystem watch error (binserve hot reload): {:?}", e)
}
}
Expand Down

0 comments on commit 21416e7

Please sign in to comment.