mirror of
https://github.com/Tim-Paik/srv.git
synced 2024-10-13 00:29:43 +00:00
Compare commits
2 Commits
v1.0.0-rc.
...
v1.0.0-rc.
Author | SHA1 | Date | |
---|---|---|---|
2c1cdc5720 | |||
02b6c26874 |
485
Cargo.lock
generated
485
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -11,16 +11,16 @@ actix-http = "2.2"
|
|||||||
actix-web = {version = "3.3", features = ["rustls"]}
|
actix-web = {version = "3.3", features = ["rustls"]}
|
||||||
actix-web-httpauth = "0.5"
|
actix-web-httpauth = "0.5"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
clap = {version = "3.0.0-beta.4", features = ["wrap_help", "color"]}
|
clap = {version = "3.0.0", features = ["wrap_help", "color", "cargo"]}
|
||||||
env_logger = "0.9"
|
env_logger = "0.9"
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime_guess = "2"
|
mime_guess = "2.0"
|
||||||
regex = "1.5"
|
regex = "1.5"
|
||||||
rustls = "0.18"
|
rustls = "0.18"
|
||||||
serde = "1"
|
serde = "1.0"
|
||||||
sha2 = "0.9"
|
sha2 = "0.9"
|
||||||
tera = "1"
|
tera = "1.13"
|
||||||
toml = "0.5"
|
toml = "0.5"
|
||||||
urlencoding = "2.1"
|
urlencoding = "2.1"
|
||||||
|
|
||||||
|
162
src/main.rs
162
src/main.rs
@ -12,6 +12,7 @@ use actix_web::{
|
|||||||
dev::{self, Service, ServiceResponse},
|
dev::{self, Service, ServiceResponse},
|
||||||
http, middleware, App, HttpResponse, HttpServer,
|
http, middleware, App, HttpResponse, HttpServer,
|
||||||
};
|
};
|
||||||
|
use clap::Arg;
|
||||||
use env_logger::fmt::Color;
|
use env_logger::fmt::Color;
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use sha2::Digest;
|
use sha2::Digest;
|
||||||
@ -342,102 +343,71 @@ async fn validator(
|
|||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
let matches = clap_app!((crate_name!()) =>
|
let check_does_dir_exits = |path: &str| match std::fs::metadata(path) {
|
||||||
(version: crate_version!())
|
Ok(meta) => {
|
||||||
(author: crate_authors!())
|
if meta.is_dir() {
|
||||||
(about: crate_description!())
|
Ok(())
|
||||||
(@arg noindex: --noindex "Disable automatic index page generation")
|
} else {
|
||||||
(@arg compress: -c --compress "Enable streaming compression (Content-length/segment download will be disabled)")
|
Err("Parameter is not a directory".to_owned())
|
||||||
// (@arg upload: -u --upload "Enable file upload")
|
|
||||||
(@arg nocache: --nocache "Disable HTTP cache")
|
|
||||||
(@arg nocolor: --nocolor "Disable cli colors")
|
|
||||||
(@arg cors: --cors [VALUE] min_values(0) max_values(1) "Enable CORS")
|
|
||||||
(@arg spa: --spa "Enable Single-Page Application mode (always serve /index.html when the file is not found)")
|
|
||||||
(@arg dotfiles: -d --dotfiles "Show dotfiles")
|
|
||||||
(@arg open: -o --open "Open the page in the default browser")
|
|
||||||
(@arg quiet: -q --quiet "Disable access log output")
|
|
||||||
(@arg quietall: --quietall "Disable all output")
|
|
||||||
(@arg ROOT: default_value["."] {
|
|
||||||
|path| match std::fs::metadata(path) {
|
|
||||||
Ok(meta) => {
|
|
||||||
if meta.is_dir() {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err("Parameter is not a directory".to_owned())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => Err(e.to_string()),
|
|
||||||
}
|
}
|
||||||
} "Root directory")
|
}
|
||||||
(@arg address: -a --address +takes_value default_value["0.0.0.0"] {
|
Err(e) => Err(e.to_string()),
|
||||||
|s| match IpAddr::from_str(s) {
|
};
|
||||||
Ok(_) => Ok(()),
|
let check_does_file_exits = |path: &str| match std::fs::metadata(path) {
|
||||||
Err(e) => Err(e.to_string()),
|
Ok(metadata) => {
|
||||||
|
if metadata.is_file() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err("Parameter is not a file".to_owned())
|
||||||
}
|
}
|
||||||
} "IP address to serve on")
|
}
|
||||||
(@arg port: -p --port +takes_value default_value["8000"] {
|
Err(e) => Err(e.to_string()),
|
||||||
|s| match s.parse::<u16>() {
|
};
|
||||||
Ok(_) => Ok(()),
|
let check_is_ip_addr = |s: &str| match IpAddr::from_str(s) {
|
||||||
Err(e) => Err(e.to_string()),
|
Ok(_) => Ok(()),
|
||||||
}
|
Err(e) => Err(e.to_string()),
|
||||||
} "Port to serve on")
|
};
|
||||||
(@arg auth: --auth +takes_value {
|
let check_is_port_num = |s: &str| match s.parse::<u16>() {
|
||||||
|s| {
|
Ok(_) => Ok(()),
|
||||||
let parts = s.splitn(2, ':').collect::<Vec<&str>>();
|
Err(e) => Err(e.to_string()),
|
||||||
if parts.len() < 2 || parts.len() >= 2 && parts[1].is_empty() {
|
};
|
||||||
Err("Password not found".to_owned())
|
let check_is_auth = |s: &str| {
|
||||||
} else if parts[0].is_empty() {
|
let parts = s.splitn(2, ':').collect::<Vec<&str>>();
|
||||||
Err("Username not found".to_owned())
|
if parts.len() < 2 || parts.len() >= 2 && parts[1].is_empty() {
|
||||||
} else {
|
Err("Password not found".to_owned())
|
||||||
Ok(())
|
} else if parts[0].is_empty() {
|
||||||
}
|
Err("Username not found".to_owned())
|
||||||
}
|
} else {
|
||||||
} "HTTP Auth (username:password)")
|
Ok(())
|
||||||
(@arg cert: --cert +takes_value {
|
}
|
||||||
|s| match std::fs::metadata(s) {
|
};
|
||||||
Ok(metadata) => {
|
let matches = app_from_crate!()
|
||||||
if metadata.is_file() {
|
.arg(Arg::new("noindex").long("noindex").help("Disable automatic index page generation"))
|
||||||
Ok(())
|
.arg(Arg::new("compress").short('c').long("compress").help("Enable streaming compression (Content-length/segment download will be disabled)"))
|
||||||
} else {
|
.arg(Arg::new("nocache").long("nocache").help("Disable HTTP cache"))
|
||||||
Err("Parameter is not a file".to_owned())
|
.arg(Arg::new("nocolor").long("nocolor").help("Disable cli colors"))
|
||||||
}
|
.arg(Arg::new("cors").long("cors").takes_value(true).min_values(0).max_values(1).help("Enable CORS [with custom value]"))
|
||||||
}
|
.arg(Arg::new("spa").long("spa").help("Enable Single-Page Application mode (always serve /index.html when the file is not found)"))
|
||||||
Err(e) => Err(e.to_string()),
|
.arg(Arg::new("dotfiles").short('d').long("dotfiles").help("Show dotfiles"))
|
||||||
}
|
.arg(Arg::new("open").short('o').long("open").help("Open the page in the default browser"))
|
||||||
} "Path of TLS/SSL public key (certificate)")
|
.arg(Arg::new("quiet").short('q').long("quiet").help("Disable access log output"))
|
||||||
(@arg key: --key +takes_value {
|
.arg(Arg::new("quietall").long("quietall").help("Disable all output"))
|
||||||
|s| match std::fs::metadata(s) {
|
.arg(Arg::new("ROOT").default_value(".").validator(check_does_dir_exits).help("Root directory"))
|
||||||
Ok(metadata) => {
|
.arg(Arg::new("address").short('a').long("address").default_value("0.0.0.0").takes_value(true).validator(check_is_ip_addr).help("IP address to serve on"))
|
||||||
if metadata.is_file() {
|
.arg(Arg::new("port").short('p').long("port").default_value("8000").takes_value(true).validator(check_is_port_num).help("Port to serve on"))
|
||||||
Ok(())
|
.arg(Arg::new("auth").long("auth").takes_value(true).validator(check_is_auth).help("HTTP Auth (username:password)"))
|
||||||
} else {
|
.arg(Arg::new("cert").long("cert").takes_value(true).validator(check_does_file_exits).help("Path of TLS/SSL public key (certificate)"))
|
||||||
Err("Parameter is not a file".to_owned())
|
.arg(Arg::new("key").long("key").takes_value(true).validator(check_does_file_exits).help("Path of TLS/SSL private key"))
|
||||||
}
|
.subcommand(clap::App::new("doc")
|
||||||
}
|
.about("Open cargo doc via local server (Need cargo installation)")
|
||||||
Err(e) => Err(e.to_string()),
|
.arg(Arg::new("nocolor").long("nocolor").help("Disable cli colors"))
|
||||||
}
|
.arg(Arg::new("noopen").long("noopen").help("Do not open the page in the default browser"))
|
||||||
} "Path of TLS/SSL private key")
|
.arg(Arg::new("log").long("log").help("Enable access log output [default: disabled]"))
|
||||||
(@subcommand doc =>
|
.arg(Arg::new("quietall").long("quietall").help("Disable all output"))
|
||||||
(about: "Open cargo doc via local server (Need cargo installation)")
|
.arg(Arg::new("address").short('a').long("address").default_value("0.0.0.0").takes_value(true).validator(check_is_ip_addr).help("IP address to serve on"))
|
||||||
(@arg nocolor: --nocolor "Disable cli colors")
|
.arg(Arg::new("port").short('p').long("port").default_value("8000").takes_value(true).validator(check_is_port_num).help("Port to serve on"))
|
||||||
(@arg noopen: -no --noopen "Do not open the page in the default browser")
|
|
||||||
(@arg log: --log "Enable access log output [default: false]")
|
|
||||||
(@arg quietall: --quietall "Disable all output")
|
|
||||||
(@arg address: -a --address +takes_value default_value["0.0.0.0"] {
|
|
||||||
|s| match IpAddr::from_str(s) {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(e) => Err(e.to_string()),
|
|
||||||
}
|
|
||||||
} "IP address to serve on")
|
|
||||||
(@arg port: -p --port +takes_value default_value["8000"] {
|
|
||||||
|s| match s.parse::<u16>() {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(e) => Err(e.to_string()),
|
|
||||||
}
|
|
||||||
} "Port to serve on")
|
|
||||||
)
|
)
|
||||||
)
|
.get_matches();
|
||||||
.get_matches();
|
|
||||||
|
|
||||||
set_var(
|
set_var(
|
||||||
"ROOT",
|
"ROOT",
|
||||||
@ -608,11 +578,11 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.format(|buf, record| {
|
.format(|buf, record| {
|
||||||
let data = record.args().to_string();
|
let data = record.args().to_string();
|
||||||
let mut style = buf.style();
|
let mut style = buf.style();
|
||||||
let blue = style.set_color(Color::Rgb(52, 152, 219));
|
let blue = style.set_color(Color::Cyan);
|
||||||
let mut style = buf.style();
|
let mut style = buf.style();
|
||||||
let red = style.set_color(Color::Rgb(231, 76, 60));
|
let red = style.set_color(Color::Red);
|
||||||
let mut style = buf.style();
|
let mut style = buf.style();
|
||||||
let green = style.set_color(Color::Rgb(76, 175, 80));
|
let green = style.set_color(Color::Green);
|
||||||
if record.target() == "actix_web::middleware::logger" {
|
if record.target() == "actix_web::middleware::logger" {
|
||||||
let data: Vec<&str> = data.splitn(5, '^').collect();
|
let data: Vec<&str> = data.splitn(5, '^').collect();
|
||||||
let time = blue.value(
|
let time = blue.value(
|
||||||
|
Reference in New Issue
Block a user