Skip to content

Commit

Permalink
Merge pull request #45 from logankaser/master
Browse files Browse the repository at this point in the history
Update Binserve Dependencies
  • Loading branch information
mufeedvh committed Feb 18, 2024
2 parents ed5fe07 + aec0467 commit 1e32f94
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 360 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
21 changes: 7 additions & 14 deletions src/cli/interface.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use clap::{Command, Arg, ArgMatches};

use crate::core::VERSION;
use clap::{Arg, ArgMatches, Command};

/// Prints an ASCII art banner to look cool!
pub fn banner() {
eprintln!(
"{}", format!("{} {}\n", include_str!("banner"), VERSION)
)
eprintln!("{} {}\n", include_str!("banner"), env!("CARGO_PKG_VERSION"))
}

/// Command-line arguments
pub fn args() -> ArgMatches {
Command::new("binserve")
.version(VERSION)
.version(env!("CARGO_PKG_VERSION"))
.author("Mufeed VH <mufeed@lyminal.space>")
.about("A fast static web server with Automatic HTTPs, routing, templating, and security in a single binary you can setup with zero code.")
.arg(Arg::new("command")
Expand All @@ -25,21 +21,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()
}
}
7 changes: 3 additions & 4 deletions src/cli/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ pub fn push_message(log_type: Type, message: &str) {
Type::_Skipped => format!("{}{}{}", "[".bold(), "SKIPPED".bold().yellow(), "]".bold()),
Type::Error => format!("{}{}{}", "[".bold(), "ERROR".bold().red(), "]".bold()),
Type::Info => format!("{}{}{}", "[".bold(), "INFO".bold().cyan(), "]".bold()),
Type::Success => format!("{}{}{}", "[".bold(), "SUCCESS".bold().green(), "]".bold())
Type::Success => format!("{}{}{}", "[".bold(), "SUCCESS".bold().green(), "]".bold()),
};

eprintln!("{}", format!("{} {}", prefix, message))
}
eprintln!("{} {}", prefix, message)
}
2 changes: 1 addition & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub(crate) mod interface;
pub(crate) mod messages;
pub(crate) mod messages;
38 changes: 20 additions & 18 deletions src/core/config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::fs::File;
use std::io::BufReader;
use std::io::{self, prelude::*};
use std::path::{Path, PathBuf};

use serde::{Serialize, Deserialize};
use serde_json;
use serde::{Deserialize, Serialize};

pub const CONFIG_FILE: &'static str = "binserve.json";
pub const CONFIG_FILE: &str = "binserve.json";

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Tls {
pub host: String,

pub enable: bool,

#[serde(default)]
pub key: PathBuf,

Expand All @@ -27,7 +26,7 @@ pub struct Server {
pub host: String,

#[serde(default)]
pub tls: Tls
pub tls: Tls,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
Expand All @@ -39,7 +38,7 @@ pub struct Static {
pub served_from: String,

#[serde(default)]
pub error_pages: HashMap<i16, PathBuf>
pub error_pages: HashMap<i16, PathBuf>,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
Expand All @@ -48,12 +47,16 @@ pub struct Template {
pub partials: HashMap<String, PathBuf>,

#[serde(default)]
pub variables: HashMap<String, String>
pub variables: HashMap<String, String>,
}

// configuration toggles
const fn enabled() -> bool { true }
const fn disabled() -> bool { false }
const fn enabled() -> bool {
true
}
const fn disabled() -> bool {
false
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
Expand All @@ -76,7 +79,7 @@ pub struct Config {
pub follow_symlinks: bool,

#[serde(default = "disabled")]
pub enable_logging: bool
pub enable_logging: bool,
}

/// secure/fallback defaults
Expand All @@ -89,7 +92,7 @@ impl Default for Config {
enable_directory_listing: false,
minify_html: false,
follow_symlinks: false,
enable_logging: false
enable_logging: false,
}
}
}
Expand All @@ -109,24 +112,23 @@ pub struct BinserveConfig {
pub config: Config,

#[serde(default)]
pub insert_headers: HashMap<String, String>
pub insert_headers: HashMap<String, String>,
}

use once_cell::sync::Lazy;
use parking_lot::Mutex;

/// A universal config state
pub static CONFIG_STATE: Lazy<Mutex<BinserveConfig>> = Lazy::new(|| {
Mutex::new(BinserveConfig::default())
});
pub static CONFIG_STATE: Lazy<Mutex<BinserveConfig>> =
Lazy::new(|| Mutex::new(BinserveConfig::default()));

impl BinserveConfig {
/// Read and serialize the config file.
pub fn read() -> io::Result<Self> {
let config_file = File::open(CONFIG_FILE)?;
let buf_reader = BufReader::new(config_file);
let config: BinserveConfig = serde_json::from_reader(buf_reader)?;

// update global config state
*CONFIG_STATE.lock() = config.to_owned();

Expand All @@ -145,4 +147,4 @@ impl BinserveConfig {

Ok(())
}
}
}
40 changes: 17 additions & 23 deletions src/core/engine.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use crate::cli::interface;

use super::{
files, server, templates, watcher,
routes::RouteHandle,
config::BinserveConfig,
};
use super::{config::BinserveConfig, files, routes::RouteHandle, server, templates, watcher};

use crate::cli::messages::{Type, push_message};
use crate::cli::messages::{push_message, Type};

pub fn init() -> anyhow::Result<()> {
let start_time = std::time::Instant::now();
Expand All @@ -22,9 +18,15 @@ pub fn init() -> anyhow::Result<()> {

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

// prepare template partials
let handlebars_handle = templates::render_templates(&config)?;
Expand All @@ -37,36 +39,28 @@ pub fn init() -> anyhow::Result<()> {
if end_time.as_millis() == 0 {
push_message(
Type::Info,
&format!("Build finished in {} μs ⚡", end_time.as_micros())
&format!("Build finished in {} μs ⚡", end_time.as_micros()),
)
} else {
push_message(
Type::Info,
&format!("Build finished in {} ms ⚡", end_time.as_millis())
&format!("Build finished in {} ms ⚡", end_time.as_millis()),
)
}

if config.server.tls.enable {
push_message(
Type::Info,
"Enabled TLS (HTTPS) 🔒"
)
push_message(Type::Info, "Enabled TLS (HTTPS) 🔒")
}

if config.config.enable_logging {
push_message(
Type::Info,
"Enabled logging 📜"
)
push_message(Type::Info, "Enabled logging 📜")
}

// start the hot reloader (file wacther)
std::thread::spawn(|| {
watcher::hot_reload_files()
});
std::thread::spawn(watcher::hot_reload_files);

// and finally server take off!
server::run_server(config)?;

Ok(())
}
}
Loading

0 comments on commit 1e32f94

Please sign in to comment.