mirror of
https://github.com/Tim-Paik/srv.git
synced 2024-10-13 00:29:43 +00:00
Compare commits
15 Commits
v1.0.0-rc.
...
v1.0.3
Author | SHA1 | Date | |
---|---|---|---|
46b7c6379f
|
|||
39bcd56608
|
|||
f82e85aa66
|
|||
c98ef43525
|
|||
f1921d184d
|
|||
d1d849d93b
|
|||
0217f3f1f6
|
|||
c575147891
|
|||
9d81ee2291 | |||
d6f7ec2380 | |||
b4c980db32 | |||
3953820138 | |||
92a69aa058
|
|||
fa3ca96fb7
|
|||
edc7fad925
|
3
.github/workflows/release.yml
vendored
3
.github/workflows/release.yml
vendored
@ -22,6 +22,8 @@ jobs:
|
||||
os: ubuntu-latest
|
||||
- target: x86_64-unknown-linux-musl
|
||||
os: ubuntu-latest
|
||||
- target: aarch64-apple-darwin
|
||||
os: macos-latest
|
||||
- target: x86_64-apple-darwin
|
||||
os: macos-latest
|
||||
- target: x86_64-pc-windows-msvc
|
||||
@ -32,6 +34,7 @@ jobs:
|
||||
- uses: taiki-e/upload-rust-binary-action@v1
|
||||
with:
|
||||
bin: srv
|
||||
archive: $bin-$tag-$target
|
||||
target: ${{ matrix.target }}
|
||||
tar: unix
|
||||
zip: windows
|
||||
|
457
Cargo.lock
generated
457
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
13
Cargo.toml
13
Cargo.toml
@ -3,28 +3,27 @@ authors = ["Tim_Paik <timpaikc@outlook.com>"]
|
||||
description = "simple http server written in rust"
|
||||
edition = "2018"
|
||||
name = "srv"
|
||||
version = "1.0.0-rc"
|
||||
version = "1.0.2"
|
||||
|
||||
[dependencies]
|
||||
actix-files = "0.6"
|
||||
actix-http = "3.0"
|
||||
actix-web = {version = "4.0", features = ["rustls"]}
|
||||
actix-web = {version = "4.1", features = ["rustls"]}
|
||||
actix-web-httpauth = "0.6"
|
||||
chrono = "0.4"
|
||||
clap = {version = "3.1", features = ["wrap_help", "color", "cargo"]}
|
||||
clap = {version = "3.2", features = ["wrap_help", "color", "cargo"]}
|
||||
env_logger = "0.9"
|
||||
lazy_static = "1.4"
|
||||
log = "0.4"
|
||||
mime_guess = "2.0"
|
||||
regex = "1.5"
|
||||
rustls = "0.20"
|
||||
rustls-pemfile = "1.0"
|
||||
serde = {version = "1.0", features = ["derive"]}
|
||||
sha2 = "0.10"
|
||||
tera = "1.15"
|
||||
tera = "1.16"
|
||||
time = { version = "0.3", features = ["formatting", "parsing"] }
|
||||
toml = "0.5"
|
||||
urlencoding = "2.1"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
opt-level = "z"
|
||||
strip = true
|
||||
|
12
README.md
12
README.md
@ -7,7 +7,6 @@ This is a simple HTTP Server for use in a development environment, inspired by [
|
||||
|
||||
### Built With
|
||||
|
||||
- ~~[rocket](https://github.com/SergioBenitez/Rocket)~~ Framework used in previous versions
|
||||
- [clap](https://github.com/clap-rs/clap) Provide command line parameter analysis
|
||||
- [tera](https://github.com/Keats/tera) Provide template support
|
||||
- [actix-web](https://github.com/actix/actix-web) Main frame
|
||||
@ -51,10 +50,10 @@ yay -S srv-bin
|
||||
Download the pre-compiled `srv-x86_64-unknown-linux-musl.tar.gz` on the [releases](https://github.com/Tim-Paik/srv/releases/latest), and copy the srv file in the compressed package to `/usr/bin` as a ROOT user with 755 permissions.
|
||||
|
||||
```shell
|
||||
wget https://github.com/Tim-Paik/srv/releases/download/v1.0.0-rc.6/srv-x86_64-unknown-linux-musl.tar.gz
|
||||
tar -xzvf srv-x86_64-unknown-linux-musl.tar.gz
|
||||
wget https://github.com/Tim-Paik/srv/releases/download/v1.0.1/srv-v1.0.1-x86_64-unknown-linux-musl.tar.gz
|
||||
tar -xzvf srv-v1.0.1-x86_64-unknown-linux-musl.tar.gz
|
||||
install -Dm0755 -t /usr/bin/ srv
|
||||
rm srv srv-x86_64-unknown-linux-musl.tar.gz
|
||||
rm srv srv-v1.0.1-x86_64-unknown-linux-musl.tar.gz
|
||||
```
|
||||
for reference only
|
||||
|
||||
@ -78,7 +77,6 @@ You Need:
|
||||
git clone git@github.com:Tim-Paik/srv.git
|
||||
cd srv
|
||||
cargo build --release
|
||||
strip target/release/srv
|
||||
```
|
||||
|
||||
Then you can find the compiled executable file named `srv` in the `target/release/` folder.
|
||||
@ -89,6 +87,10 @@ Execute `srv --help` to get all the usage methods
|
||||
|
||||
Waiting to be added...
|
||||
|
||||
## Contributing
|
||||
|
||||
All contributions are welcome and I will reply as soon as I see it :)
|
||||
|
||||
## License
|
||||
|
||||
```text
|
||||
|
236
src/main.rs
236
src/main.rs
@ -197,11 +197,11 @@ fn render_index(
|
||||
let res = actix_files::NamedFile::open(index)?
|
||||
.set_content_type(mime_guess::mime::TEXT_HTML_UTF_8)
|
||||
.into_response(req);
|
||||
return Ok(ServiceResponse::new(req.clone(), res));
|
||||
return Ok(ServiceResponse::new(req.to_owned(), res));
|
||||
}
|
||||
if var("NOINDEX").unwrap_or_else(|_| "false".to_string()) == "true" {
|
||||
return Ok(ServiceResponse::new(
|
||||
req.clone(),
|
||||
req.to_owned(),
|
||||
HttpResponse::NotFound().body(""),
|
||||
));
|
||||
}
|
||||
@ -252,9 +252,11 @@ fn render_index(
|
||||
}
|
||||
};
|
||||
let modified = match metadata.modified() {
|
||||
Ok(time) => chrono::DateTime::<chrono::Local>::from(time)
|
||||
.format("%Y/%m/%d %H:%M:%S")
|
||||
.to_string(),
|
||||
Ok(time) => time::OffsetDateTime::from(time)
|
||||
.format(time::macros::format_description!(
|
||||
"[year]/[month]/[day] [hour]:[minute]:[second]"
|
||||
))
|
||||
.unwrap_or_else(|_| "".to_string()),
|
||||
Err(e) => {
|
||||
error!(target: "read_dir", "[ERROR] Read modified time error: {}", e.to_string());
|
||||
continue;
|
||||
@ -292,7 +294,7 @@ fn render_index(
|
||||
let res = HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(index);
|
||||
Ok(ServiceResponse::new(req.clone(), res))
|
||||
Ok(ServiceResponse::new(req.to_owned(), res))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -412,7 +414,6 @@ async fn main() -> std::io::Result<()> {
|
||||
set_var("SPA", matches.is_present("spa").to_string());
|
||||
set_var("DOTFILES", matches.is_present("dotfiles").to_string());
|
||||
set_var("NOCACHE", matches.is_present("nocache").to_string());
|
||||
set_var("COMPRESS", matches.is_present("compress").to_string());
|
||||
|
||||
if matches.is_present("quiet") {
|
||||
set_var("RUST_LOG", "info,actix_web::middleware::logger=off");
|
||||
@ -448,11 +449,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.value_of("address")
|
||||
.unwrap_or("127.0.0.1")
|
||||
.to_string();
|
||||
let addr = format!(
|
||||
"{}:{}",
|
||||
ip,
|
||||
matches.value_of("port").unwrap_or("8000")
|
||||
);
|
||||
let addr = format!("{}:{}", ip, matches.value_of("port").unwrap_or("8000"));
|
||||
let url = format!(
|
||||
"{}{}:{}",
|
||||
if enable_tls {
|
||||
@ -484,6 +481,104 @@ async fn main() -> std::io::Result<()> {
|
||||
open_in_browser(&url);
|
||||
}
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("doc") {
|
||||
if !matches.is_present("log") {
|
||||
set_var("RUST_LOG", "info,actix_web::middleware::logger=off");
|
||||
}
|
||||
if matches.is_present("quietall") {
|
||||
set_var("RUST_LOG", "off");
|
||||
}
|
||||
if matches.is_present("nocolor") {
|
||||
set_var("RUST_LOG_STYLE", "never");
|
||||
}
|
||||
}
|
||||
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
|
||||
.format(move |buf, record| {
|
||||
let data = record.args().to_string();
|
||||
let mut style = buf.style();
|
||||
let blue = style.set_color(Color::Cyan);
|
||||
let mut style = buf.style();
|
||||
let red = style.set_color(Color::Red);
|
||||
let mut style = buf.style();
|
||||
let green = style.set_color(Color::Green);
|
||||
if record.target() == "actix_web::middleware::logger" {
|
||||
let data: Vec<&str> = data.splitn(5, '^').collect();
|
||||
let time = blue.value(
|
||||
time::OffsetDateTime::parse(
|
||||
data[0],
|
||||
&time::format_description::well_known::Rfc3339,
|
||||
)
|
||||
.unwrap_or(time::OffsetDateTime::UNIX_EPOCH)
|
||||
.format(time::macros::format_description!(
|
||||
"[year]/[month]/[day] [hour]:[minute]:[second]"
|
||||
))
|
||||
.unwrap_or_else(|_| "".to_string()),
|
||||
);
|
||||
let ipaddr = blue.value(data[1]);
|
||||
let status_code = data[2].parse().unwrap_or(500);
|
||||
let status_code = if status_code < 400 {
|
||||
green.value(status_code)
|
||||
} else {
|
||||
red.value(status_code)
|
||||
};
|
||||
let process_time: Vec<&str> = data[3].splitn(2, '.').collect();
|
||||
let process_time = process_time[0].to_string() + "ms";
|
||||
let process_time = blue.value(if process_time.len() == 3 {
|
||||
" ".to_string() + &process_time
|
||||
} else if process_time.len() == 4 {
|
||||
" ".to_string() + &process_time
|
||||
} else {
|
||||
process_time
|
||||
});
|
||||
let content = blue.value(
|
||||
urlencoding::decode(data[4])
|
||||
.unwrap_or(std::borrow::Cow::Borrowed("[Parse URL Error]"))
|
||||
.into_owned(),
|
||||
);
|
||||
return writeln!(
|
||||
buf,
|
||||
"[{}] {} | {} | {} | {}",
|
||||
time, ipaddr, status_code, process_time, content
|
||||
);
|
||||
} else if record.target() == "actix_server::builder" {
|
||||
if data.starts_with("Starting ") && data.ends_with(" workers") {
|
||||
return Ok(());
|
||||
}
|
||||
} else if record.target() == "actix_server::server" {
|
||||
if data == "Actix runtime found; starting in Actix runtime" {
|
||||
let data = format!(
|
||||
"[INFO] Serving {} on {}",
|
||||
var("ROOT").unwrap_or_else(|_| ".".to_string()),
|
||||
var("LISTEN_ADDRESS").unwrap_or_else(|_| "0.0.0.0:8000".to_string())
|
||||
);
|
||||
return writeln!(buf, "\r{}", green.value(data));
|
||||
}
|
||||
if data == "SIGINT received; starting forced shutdown" {
|
||||
return writeln!(
|
||||
buf,
|
||||
"\r{}",
|
||||
green.value("[INFO] SIGINT received; starting forced shutdown")
|
||||
);
|
||||
// Add '\r' to remove the input ^C
|
||||
}
|
||||
return Ok(());
|
||||
} else if record.target() == "actix_server::worker"
|
||||
|| record.target() == "actix_server::accept"
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
if data.starts_with("[ERROR]")
|
||||
|| data.starts_with("TLS alert")
|
||||
|| data.starts_with("Failed")
|
||||
{
|
||||
writeln!(buf, "\r{}", red.value(data))
|
||||
} else {
|
||||
writeln!(buf, "\r{}", green.value(data))
|
||||
}
|
||||
})
|
||||
.init();
|
||||
|
||||
let addr = if let Some(matches) = matches.subcommand_matches("doc") {
|
||||
let mut cargo_toml = match std::fs::File::open("./Cargo.toml") {
|
||||
Ok(file) => file,
|
||||
@ -540,11 +635,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.value_of("address")
|
||||
.unwrap_or("127.0.0.1")
|
||||
.to_string();
|
||||
let addr = format!(
|
||||
"{}:{}",
|
||||
ip,
|
||||
matches.value_of("port").unwrap_or("8000")
|
||||
);
|
||||
let addr = format!("{}:{}", ip, matches.value_of("port").unwrap_or("8000"));
|
||||
let url = format!(
|
||||
"http://{}:{}/{}/index.html",
|
||||
if ip == "0.0.0.0" { "127.0.0.1" } else { &ip },
|
||||
@ -554,99 +645,11 @@ async fn main() -> std::io::Result<()> {
|
||||
if !matches.is_present("noopen") {
|
||||
open_in_browser(&url);
|
||||
}
|
||||
if !matches.is_present("log") {
|
||||
set_var("RUST_LOG", "info,actix_web::middleware::logger=off");
|
||||
}
|
||||
if matches.is_present("quietall") {
|
||||
set_var("RUST_LOG", "off");
|
||||
}
|
||||
if matches.is_present("nocolor") {
|
||||
set_var("RUST_LOG_STYLE", "never");
|
||||
}
|
||||
addr
|
||||
} else {
|
||||
addr
|
||||
};
|
||||
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
|
||||
.format(|buf, record| {
|
||||
let data = record.args().to_string();
|
||||
let mut style = buf.style();
|
||||
let blue = style.set_color(Color::Cyan);
|
||||
let mut style = buf.style();
|
||||
let red = style.set_color(Color::Red);
|
||||
let mut style = buf.style();
|
||||
let green = style.set_color(Color::Green);
|
||||
if record.target() == "actix_web::middleware::logger" {
|
||||
let data: Vec<&str> = data.splitn(5, '^').collect();
|
||||
let time = blue.value(
|
||||
chrono::NaiveDateTime::from_str(data[0])
|
||||
.unwrap()
|
||||
.format("%Y/%m/%d %H:%M:%S")
|
||||
.to_string(),
|
||||
);
|
||||
let ipaddr = blue.value(data[1]);
|
||||
let status_code = data[2].parse().unwrap_or(500);
|
||||
let status_code = if status_code < 400 {
|
||||
green.value(status_code)
|
||||
} else {
|
||||
red.value(status_code)
|
||||
};
|
||||
let process_time: Vec<&str> = data[3].splitn(2, '.').collect();
|
||||
let process_time = process_time[0].to_string() + "ms";
|
||||
let process_time = blue.value(if process_time.len() == 3 {
|
||||
" ".to_string() + &process_time
|
||||
} else if process_time.len() == 4 {
|
||||
" ".to_string() + &process_time
|
||||
} else {
|
||||
process_time
|
||||
});
|
||||
let content = blue.value(
|
||||
urlencoding::decode(data[4])
|
||||
.unwrap_or(std::borrow::Cow::Borrowed("[Parse URL Error]"))
|
||||
.into_owned(),
|
||||
);
|
||||
return writeln!(
|
||||
buf,
|
||||
"[{}] {} | {} | {} | {}",
|
||||
time, ipaddr, status_code, process_time, content
|
||||
);
|
||||
} else if record.target() == "actix_server::builder" {
|
||||
if data.starts_with("SIGINT received, exiting") {
|
||||
return writeln!(buf, "\r{}", green.value("[INFO] SIGINT received, exiting"));
|
||||
// Add '\r' to remove the input ^C
|
||||
} else {
|
||||
let data = data.replace("actix-web-service-", "");
|
||||
let re1 = regex::Regex::new("Starting (.*) workers").unwrap();
|
||||
if re1.is_match(&data) {
|
||||
return Ok(());
|
||||
}
|
||||
let re2 = regex::Regex::new("Starting \"(.*)\" service on (.*)").unwrap();
|
||||
if re2.is_match(&data) {
|
||||
let addr = re2
|
||||
.captures(&data)
|
||||
.unwrap()
|
||||
.get(1)
|
||||
.map_or("", |m| m.as_str());
|
||||
let data = format!(
|
||||
"[INFO] Serving {} on {}",
|
||||
var("ROOT").unwrap_or_else(|_| ".".to_string()),
|
||||
addr
|
||||
);
|
||||
return writeln!(buf, "\r{}", green.value(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
if data.starts_with("[ERROR]")
|
||||
|| data.starts_with("TLS alert")
|
||||
|| data.starts_with("Failed")
|
||||
{
|
||||
writeln!(buf, "\r{}", red.value(data))
|
||||
} else {
|
||||
writeln!(buf, "\r{}", green.value(data))
|
||||
}
|
||||
})
|
||||
.init();
|
||||
set_var("LISTEN_ADDRESS", addr);
|
||||
|
||||
let server = HttpServer::new(move || {
|
||||
let app = App::new()
|
||||
@ -722,16 +725,29 @@ async fn main() -> std::io::Result<()> {
|
||||
let key = &mut BufReader::new(
|
||||
std::fs::File::open(Path::new(matches.value_of("key").unwrap())).unwrap(),
|
||||
);
|
||||
let cert = rustls_pemfile::certs(cert).unwrap().iter().map(|x| rustls::Certificate(x.to_vec())).collect::<Vec<_>>();
|
||||
let key = rustls::PrivateKey(rustls_pemfile::pkcs8_private_keys(key).unwrap().first().expect("no private key found").to_owned());
|
||||
let cert = rustls_pemfile::certs(cert)
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|x| rustls::Certificate(x.to_vec()))
|
||||
.collect::<Vec<_>>();
|
||||
let key = rustls::PrivateKey(
|
||||
rustls_pemfile::pkcs8_private_keys(key)
|
||||
.unwrap()
|
||||
.first()
|
||||
.expect("no private key found")
|
||||
.to_owned(),
|
||||
);
|
||||
let config = rustls::ServerConfig::builder()
|
||||
.with_safe_defaults()
|
||||
.with_no_client_auth()
|
||||
.with_single_cert(cert, key)
|
||||
.expect("bad certificate/key");
|
||||
server.bind_rustls(addr, config)
|
||||
server.bind_rustls(
|
||||
var("LISTEN_ADDRESS").unwrap_or_else(|_| "0.0.0.0:8000".to_string()),
|
||||
config,
|
||||
)
|
||||
} else {
|
||||
server.bind(addr)
|
||||
server.bind(var("LISTEN_ADDRESS").unwrap_or_else(|_| "0.0.0.0:8000".to_string()))
|
||||
};
|
||||
server?.run().await
|
||||
}
|
||||
|
Reference in New Issue
Block a user