Skip to content

Commit

Permalink
Add --dev flag—disabling all caches & enabling high ports.
Browse files Browse the repository at this point in the history
  • Loading branch information
Icelk committed May 15, 2023
1 parent 0c763d9 commit 0756a8d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
36 changes: 20 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ pub(crate) struct KvarnConfig {
import: Option<Vec<String>>,
}

pub struct CliOptions<'a> {
pub high_ports: bool,
pub cache: bool,
pub default_host: Option<&'a str>,
}

/// Parse config at `path`.
#[allow(clippy::or_fun_call)] // it's just as_ref()
async fn read_config(file: impl AsRef<Path>) -> Result<KvarnConfig> {
Expand Down Expand Up @@ -50,8 +56,7 @@ async fn read_config(file: impl AsRef<Path>) -> Result<KvarnConfig> {
pub async fn read_and_resolve(
file: impl AsRef<str>,
custom_extensions: &CustomExtensions,
high_ports: bool,
default_host: Option<&str>,
opts: &CliOptions<'_>,
) -> Result<kvarn::RunConfig> {
#[derive(Debug)]
enum Imported {
Expand Down Expand Up @@ -166,7 +171,7 @@ pub async fn read_and_resolve(
}
}

if let Some(default_host) = default_host {
if let Some(default_host) = opts.default_host {
if !hosts.contains_key(default_host) {
return Err(format!(
"Your choosen default host {default_host} wasn't found. Available: {:?}",
Expand All @@ -178,14 +183,8 @@ pub async fn read_and_resolve(
let mut built_collections = HashMap::new();
for (name, host_names) in collections {
info!("Create host collection \"{name}\" with hosts {host_names:?}");
let collection = construct_collection(
host_names,
&hosts,
&extensions,
custom_extensions,
&default_host,
)
.await?;
let collection =
construct_collection(host_names, &hosts, &extensions, custom_extensions, opts).await?;
built_collections.insert(name, collection);
}
let mut rc = kvarn::RunConfig::new();
Expand All @@ -197,8 +196,7 @@ pub async fn read_and_resolve(
&hosts,
&extensions,
custom_extensions,
high_ports,
&default_host,
opts,
)
.await?
{
Expand Down Expand Up @@ -278,13 +276,13 @@ pub async fn construct_collection(
hosts: &Hosts,
exts: &ExtensionBundles,
custom_exts: &CustomExtensions,
default_host: &Option<&str>,
opts: &CliOptions<'_>,
) -> Result<Arc<kvarn::host::Collection>> {
let mut b = kvarn::host::Collection::builder();
let mut se = vec![];
let mut cert_collection_senders = Vec::new();
for host in host_names {
let host = hosts
let mut host = hosts
.get(&host)
.ok_or_else(|| format!("Didn't find a host with name {host}."))?
.0
Expand All @@ -294,7 +292,13 @@ pub async fn construct_collection(
se.push((host.host.name.clone(), se_handle))
}
cert_collection_senders.extend(host.cert_collection_senders);
if default_host.map_or(false, |default| default == host.host.name) {
if !opts.cache {
host.host.disable_client_cache().disable_server_cache();
}
if opts
.default_host
.map_or(false, |default| default == host.host.name)
{
b = b.default(host.host);
} else {
b = b.insert(host.host);
Expand Down
18 changes: 16 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ pub fn command() -> clap::Command {
.help("Bind to higher ports (8080, 8443) to avoid permission issues")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("dev")
.long("dev")
.help("Enable development environment. Uses high ports & disables all caches.")
.conflicts_with("high_ports")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("host")
.short('h')
Expand Down Expand Up @@ -72,16 +79,23 @@ pub fn command() -> clap::Command {
pub async fn run(
custom_extensions: &config::CustomExtensions,
) -> std::sync::Arc<kvarn::shutdown::Manager> {
use self::config::CliOptions;

let env_log = env_logger::Env::new().filter_or("KVARN_LOG", "rustls=off,info");
env_logger::Builder::from_env(env_log).init();

let matches = command().get_matches();

let opts = CliOptions {
high_ports: matches.get_flag("high_ports") || matches.get_flag("dev"),
cache: !matches.get_flag("dev"),
default_host: matches.get_one::<String>("host").map(String::as_str),
};

let mut rc = match config::read_and_resolve(
matches.get_one::<String>("config").expect("it's required"),
custom_extensions,
matches.get_flag("high_ports"),
matches.get_one::<String>("host").map(String::as_str),
&opts,
)
.await
{
Expand Down
29 changes: 15 additions & 14 deletions src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::collections::HashMap;
use std::fmt::{self, Display};
use std::sync::Arc;

use crate::config::{CustomExtensions, ExtensionBundles, HostCollections, Hosts, Result};
use crate::config::{
CliOptions, CustomExtensions, ExtensionBundles, HostCollections, Hosts, Result,
};
use kvarn::prelude::{CompactString, ToCompactString};
use log::info;
use serde::{Deserialize, Serialize};
Expand All @@ -21,7 +23,7 @@ impl HostSource {
hosts: &Hosts,
exts: &ExtensionBundles,
custom_exts: &CustomExtensions,
default_host: &Option<&str>,
opts: &CliOptions<'_>,
) -> Result<Arc<kvarn::host::Collection>> {
match self {
HostSource::Collection(name) => host_collections
Expand All @@ -34,7 +36,7 @@ impl HostSource {
hosts,
exts,
custom_exts,
default_host,
opts,
)
.await?;
Ok(collection)
Expand All @@ -45,7 +47,7 @@ impl HostSource {
hosts,
exts,
custom_exts,
default_host,
opts,
)
.await?;
Ok(collection)
Expand Down Expand Up @@ -76,8 +78,7 @@ impl PortsKind {
hosts: &Hosts,
exts: &ExtensionBundles,
custom_exts: &CustomExtensions,
high_ports: bool,
default_host: &Option<&str>,
opts: &CliOptions<'_>,
) -> Result<Vec<kvarn::PortDescriptor>> {
enum NPorts<'a> {
One(u16),
Expand Down Expand Up @@ -121,15 +122,15 @@ impl PortsKind {
port,
entry
.source
.resolve(host_collections, hosts, exts, custom_exts, default_host)
.resolve(host_collections, hosts, exts, custom_exts, opts)
.await?,
)
} else {
kvarn::PortDescriptor::unsecure(
port,
entry
.source
.resolve(host_collections, hosts, exts, custom_exts, default_host)
.resolve(host_collections, hosts, exts, custom_exts, opts)
.await?,
)
};
Expand All @@ -138,15 +139,15 @@ impl PortsKind {
Ok(v)
}
PortsKind::Standard(source) => {
let ports = if high_ports {
let ports = if opts.high_ports {
&[8080, 8443]
} else {
&[80, 443]
};
log_bind(&source, NPorts::Several(ports));

let collection = source
.resolve(host_collections, hosts, exts, custom_exts, default_host)
.resolve(host_collections, hosts, exts, custom_exts, opts)
.await?;
let v = vec![
kvarn::PortDescriptor::unsecure(ports[0], collection.clone()),
Expand All @@ -155,21 +156,21 @@ impl PortsKind {
Ok(v)
}
PortsKind::HttpsOnly(source) => {
let port = if high_ports { 8443 } else { 443 };
let port = if opts.high_ports { 8443 } else { 443 };
log_bind(&source, NPorts::One(port));

let collection = source
.resolve(host_collections, hosts, exts, custom_exts, default_host)
.resolve(host_collections, hosts, exts, custom_exts, opts)
.await?;
let v = vec![kvarn::PortDescriptor::new(port, collection)];
Ok(v)
}
PortsKind::HttpOnly(source) => {
let port = if high_ports { 8080 } else { 80 };
let port = if opts.high_ports { 8080 } else { 80 };
log_bind(&source, NPorts::One(port));

let collection = source
.resolve(host_collections, hosts, exts, custom_exts, default_host)
.resolve(host_collections, hosts, exts, custom_exts, opts)
.await?;
let v = vec![kvarn::PortDescriptor::unsecure(port, collection)];
Ok(v)
Expand Down

0 comments on commit 0756a8d

Please sign in to comment.