mirror of https://github.com/Tim-Paik/srv.git
0.1.0-alpha.1
This commit is contained in:
parent
4536c5fb8b
commit
a613a35ff0
|
@ -66,6 +66,12 @@ version = "0.2.8"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
|
||||
|
||||
[[package]]
|
||||
name = "binascii"
|
||||
version = "0.1.4"
|
||||
|
@ -102,6 +108,19 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"time 0.1.43",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "3.0.0-beta.2"
|
||||
|
@ -116,6 +135,7 @@ dependencies = [
|
|||
"os_str_bytes",
|
||||
"strsim",
|
||||
"termcolor",
|
||||
"terminal_size",
|
||||
"textwrap",
|
||||
"unicode-width",
|
||||
"vec_map",
|
||||
|
@ -134,6 +154,17 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "colored"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"lazy_static",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "const_fn"
|
||||
version = "0.4.8"
|
||||
|
@ -153,7 +184,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "d5f1c7727e460397e56abc4bddc1d49e07a1ad78fc98eb2e1c8f032a58a2f80d"
|
||||
dependencies = [
|
||||
"percent-encoding",
|
||||
"time",
|
||||
"time 0.2.27",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
|
@ -501,6 +532,15 @@ version = "0.4.7"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.51"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
|
@ -592,7 +632,7 @@ dependencies = [
|
|||
"httparse",
|
||||
"log",
|
||||
"mime",
|
||||
"spin",
|
||||
"spin 0.9.2",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"twoway",
|
||||
|
@ -608,6 +648,25 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_cpus"
|
||||
version = "1.13.0"
|
||||
|
@ -856,6 +915,21 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ring"
|
||||
version = "0.16.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
"once_cell",
|
||||
"spin 0.5.2",
|
||||
"untrusted",
|
||||
"web-sys",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rocket"
|
||||
version = "0.5.0-rc.1"
|
||||
|
@ -885,7 +959,7 @@ dependencies = [
|
|||
"serde",
|
||||
"state",
|
||||
"tempfile",
|
||||
"time",
|
||||
"time 0.2.27",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
"tokio-util",
|
||||
|
@ -933,8 +1007,9 @@ dependencies = [
|
|||
"smallvec",
|
||||
"stable-pattern",
|
||||
"state",
|
||||
"time",
|
||||
"time 0.2.27",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"uncased",
|
||||
]
|
||||
|
||||
|
@ -956,6 +1031,19 @@ dependencies = [
|
|||
"semver 0.11.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls"
|
||||
version = "0.19.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "35edb675feee39aec9c99fa5ff985081995a06d594114ae14cbe797ad7b7a6d7"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"log",
|
||||
"ring",
|
||||
"sct",
|
||||
"webpki",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.5"
|
||||
|
@ -980,6 +1068,16 @@ version = "1.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[package]]
|
||||
name = "sct"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b362b83898e0e69f38515b82ee15aa80636befe47c3b6d3d89a911e78fc228ce"
|
||||
dependencies = [
|
||||
"ring",
|
||||
"untrusted",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "0.9.0"
|
||||
|
@ -1081,6 +1179,12 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.9.2"
|
||||
|
@ -1203,15 +1307,36 @@ dependencies = [
|
|||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "terminal_size"
|
||||
version = "0.1.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789"
|
||||
dependencies = [
|
||||
"terminal_size",
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.2.27"
|
||||
|
@ -1280,6 +1405,17 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-rustls"
|
||||
version = "0.22.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bc6844de72e57df1980054b38be3a9f4702aba4858be64dd700181a8a6d0e1b6"
|
||||
dependencies = [
|
||||
"rustls",
|
||||
"tokio",
|
||||
"webpki",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-stream"
|
||||
version = "0.1.7"
|
||||
|
@ -1405,6 +1541,12 @@ version = "0.2.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
|
||||
|
||||
[[package]]
|
||||
name = "vec_map"
|
||||
version = "0.8.2"
|
||||
|
@ -1491,10 +1633,33 @@ checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f"
|
|||
name = "web"
|
||||
version = "0.1.0-alpha"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
"colored",
|
||||
"lazy_static",
|
||||
"rocket",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.51"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e828417b379f3df7111d3a2a9e5753706cae29c41f7c4029ee9fd77f3e09e582"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "webpki"
|
||||
version = "0.21.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8e38c0608262c46d4a56202ebabdeb094cef7e560ca7a226c6bf055188aa4ea"
|
||||
dependencies = [
|
||||
"ring",
|
||||
"untrusted",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
|
|
|
@ -6,5 +6,8 @@ name = "web"
|
|||
version = "0.1.0-alpha"
|
||||
|
||||
[dependencies]
|
||||
clap = "3.0.0-beta.2"
|
||||
rocket = "0.5.0-rc.1"
|
||||
chrono = "0.4"
|
||||
clap = {version = "3.0.0-beta.2", features = ["wrap_help", "color"]}
|
||||
colored = "2"
|
||||
lazy_static = "1.4.0"
|
||||
rocket = {version = "0.5.0-rc.1", features = ["tls"]}
|
||||
|
|
148
src/main.rs
148
src/main.rs
|
@ -1,7 +1,13 @@
|
|||
#[macro_use] extern crate clap;
|
||||
#[macro_use] extern crate rocket;
|
||||
#[macro_use]
|
||||
extern crate clap;
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
use rocket::fs::NamedFile;
|
||||
use colored::*;
|
||||
use rocket::fairing::{Fairing, Info, Kind};
|
||||
use rocket::{config::TlsConfig, fs::NamedFile};
|
||||
use std::net::IpAddr;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[get("/<path..>")]
|
||||
|
@ -9,9 +15,62 @@ async fn index(path: std::path::PathBuf) -> Option<NamedFile> {
|
|||
NamedFile::open(path).await.ok()
|
||||
}
|
||||
|
||||
#[catch(404)]
|
||||
fn not_found() {}
|
||||
|
||||
struct Logger {}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl Fairing for Logger {
|
||||
fn info(&self) -> Info {
|
||||
Info {
|
||||
name: "Logger",
|
||||
kind: Kind::Liftoff | Kind::Response,
|
||||
}
|
||||
}
|
||||
async fn on_liftoff(&self, rocket: &rocket::Rocket<rocket::Orbit>) {
|
||||
println!(
|
||||
"{}",
|
||||
format!(
|
||||
"Serving {} on {}:{}",
|
||||
std::env::var("ROOT").unwrap_or("[Get Path Error]".to_string()),
|
||||
rocket.config().address.to_string(),
|
||||
rocket.config().port.to_string()
|
||||
)
|
||||
.bright_green()
|
||||
);
|
||||
}
|
||||
async fn on_response<'r>(
|
||||
&self,
|
||||
request: &'r rocket::Request<'_>,
|
||||
response: &mut rocket::Response<'r>,
|
||||
) {
|
||||
print!(
|
||||
"[{}] {} | {} | {} {}",
|
||||
chrono::Local::now()
|
||||
.format("%Y/%m/%d %H:%M:%S")
|
||||
.to_string()
|
||||
.white(),
|
||||
request
|
||||
.client_ip()
|
||||
.unwrap_or(IpAddr::from([0, 0, 0, 0]))
|
||||
.to_string()
|
||||
.white(),
|
||||
if response.status().code < 400 {
|
||||
response.status().code.to_string().bright_green()
|
||||
} else {
|
||||
response.status().code.to_string().bright_red()
|
||||
},
|
||||
request.method().to_string().bright_blue(),
|
||||
request.uri().to_string().bright_blue()
|
||||
);
|
||||
println!("");
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::main]
|
||||
async fn main() {
|
||||
let _matches = clap_app!((crate_name!()) =>
|
||||
let matches = clap_app!((crate_name!()) =>
|
||||
(version: crate_version!())
|
||||
(author: crate_authors!())
|
||||
(about: crate_description!())
|
||||
|
@ -34,18 +93,18 @@ async fn main() {
|
|||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
} "Root directory")
|
||||
(@arg listen: -l --listen +takes_value default_value["127.0.0.1"] {
|
||||
|s| match std::net::IpAddr::from_str(&s) {
|
||||
(@arg address: -a --address +takes_value default_value["127.0.0.1"] {
|
||||
|s| match IpAddr::from_str(&s) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
} "IP address to bind")
|
||||
(@arg port: -p --port +takes_value default_value["8080"] {
|
||||
} "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 number")
|
||||
} "Port to serve on")
|
||||
(@arg auth: --auth +takes_value {
|
||||
|s| {
|
||||
let parts = s.splitn(2, ':').collect::<Vec<&str>>();
|
||||
|
@ -84,8 +143,77 @@ async fn main() {
|
|||
} "Path of TLS/SSL private key")
|
||||
)
|
||||
.get_matches();
|
||||
rocket::build()
|
||||
|
||||
std::env::set_var("ROOT", {
|
||||
let root = Path::canonicalize(Path::new(matches.value_of("ROOT").unwrap()))
|
||||
.unwrap()
|
||||
.display()
|
||||
.to_string();
|
||||
if root.starts_with("\\\\?\\") {
|
||||
root[4..root.len()].to_string()
|
||||
} else {
|
||||
root.to_string()
|
||||
}
|
||||
});
|
||||
|
||||
if matches.is_present("nocolor") {
|
||||
colored::control::set_override(false);
|
||||
}
|
||||
|
||||
let figment = rocket::Config::figment()
|
||||
.merge((
|
||||
"address",
|
||||
IpAddr::from_str(matches.value_of("address").unwrap())
|
||||
.unwrap_or(IpAddr::from([127, 0, 0, 1])),
|
||||
))
|
||||
.merge((
|
||||
"port",
|
||||
matches
|
||||
.value_of("port")
|
||||
.unwrap()
|
||||
.parse::<u16>()
|
||||
.unwrap_or(8000),
|
||||
))
|
||||
.merge((
|
||||
"ident",
|
||||
std::env::var("WEB_SERVER_NAME").unwrap_or("timpaik'server".to_string()),
|
||||
))
|
||||
.merge(("cli_colors", matches.is_present("color")))
|
||||
.merge(("log_level", "off"));
|
||||
|
||||
let enable_tls = matches.is_present("cert") && matches.is_present("key");
|
||||
|
||||
let figment = if enable_tls {
|
||||
let cert = Path::new(matches.value_of("cert").unwrap());
|
||||
let key = Path::new(matches.value_of("key").unwrap());
|
||||
figment.merge(("tls", TlsConfig::from_paths(cert, key)))
|
||||
} else {
|
||||
figment
|
||||
};
|
||||
|
||||
if matches.is_present("open") {
|
||||
let url = if enable_tls {
|
||||
"https://".to_string()
|
||||
} else {
|
||||
"http://".to_string()
|
||||
};
|
||||
let url = url
|
||||
+ &matches.value_of("address").unwrap().to_string()
|
||||
+ ":"
|
||||
+ &matches.value_of("port").unwrap().to_string();
|
||||
if cfg!(target_os = "windows") {
|
||||
std::process::Command::new("explorer").arg(url).spawn().ok();
|
||||
} else if cfg!(target_os = "macos") {
|
||||
std::process::Command::new("open").arg(url).spawn().ok();
|
||||
} else if cfg!(target_os = "linux") {
|
||||
std::process::Command::new("xdg-open").arg(url).spawn().ok();
|
||||
}
|
||||
}
|
||||
|
||||
rocket::custom(figment)
|
||||
.attach(Logger {})
|
||||
.mount("/", routes![index])
|
||||
.register("/", catchers![not_found])
|
||||
.launch()
|
||||
.await
|
||||
.unwrap();
|
||||
|
|
Loading…
Reference in New Issue