mirror of
https://github.com/Tim-Paik/srv.git
synced 2024-10-13 00:29:43 +00:00
Compare commits
10 Commits
v1.0.0-rc.
...
v1.0.2
Author | SHA1 | Date | |
---|---|---|---|
f82e85aa66
|
|||
c98ef43525
|
|||
f1921d184d
|
|||
d1d849d93b
|
|||
0217f3f1f6
|
|||
c575147891
|
|||
9d81ee2291 | |||
d6f7ec2380 | |||
b4c980db32 | |||
3953820138 |
414
Cargo.lock
generated
414
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
11
Cargo.toml
11
Cargo.toml
@ -3,28 +3,27 @@ authors = ["Tim_Paik <timpaikc@outlook.com>"]
|
|||||||
description = "simple http server written in rust"
|
description = "simple http server written in rust"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
name = "srv"
|
name = "srv"
|
||||||
version = "1.0.0-rc"
|
version = "1.0.1"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-files = "0.6"
|
actix-files = "0.6"
|
||||||
actix-http = "3.0"
|
actix-web = {version = "4.1", features = ["rustls"]}
|
||||||
actix-web = {version = "4.0", features = ["rustls"]}
|
|
||||||
actix-web-httpauth = "0.6"
|
actix-web-httpauth = "0.6"
|
||||||
chrono = "0.4"
|
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"
|
env_logger = "0.9"
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime_guess = "2.0"
|
mime_guess = "2.0"
|
||||||
regex = "1.5"
|
|
||||||
rustls = "0.20"
|
rustls = "0.20"
|
||||||
rustls-pemfile = "1.0"
|
rustls-pemfile = "1.0"
|
||||||
serde = {version = "1.0", features = ["derive"]}
|
serde = {version = "1.0", features = ["derive"]}
|
||||||
sha2 = "0.10"
|
sha2 = "0.10"
|
||||||
tera = "1.15"
|
tera = "1.16"
|
||||||
toml = "0.5"
|
toml = "0.5"
|
||||||
urlencoding = "2.1"
|
urlencoding = "2.1"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
opt-level = "z"
|
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
|
### 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
|
- [clap](https://github.com/clap-rs/clap) Provide command line parameter analysis
|
||||||
- [tera](https://github.com/Keats/tera) Provide template support
|
- [tera](https://github.com/Keats/tera) Provide template support
|
||||||
- [actix-web](https://github.com/actix/actix-web) Main frame
|
- [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.
|
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
|
```shell
|
||||||
wget https://github.com/Tim-Paik/srv/releases/download/v1.0.0-rc.6/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-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
|
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
|
for reference only
|
||||||
|
|
||||||
@ -78,7 +77,6 @@ You Need:
|
|||||||
git clone git@github.com:Tim-Paik/srv.git
|
git clone git@github.com:Tim-Paik/srv.git
|
||||||
cd srv
|
cd srv
|
||||||
cargo build --release
|
cargo build --release
|
||||||
strip target/release/srv
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Then you can find the compiled executable file named `srv` in the `target/release/` folder.
|
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...
|
Waiting to be added...
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
All contributions are welcome and I will reply as soon as I see it :)
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
```text
|
```text
|
||||||
|
158
src/main.rs
158
src/main.rs
@ -197,11 +197,11 @@ fn render_index(
|
|||||||
let res = actix_files::NamedFile::open(index)?
|
let res = actix_files::NamedFile::open(index)?
|
||||||
.set_content_type(mime_guess::mime::TEXT_HTML_UTF_8)
|
.set_content_type(mime_guess::mime::TEXT_HTML_UTF_8)
|
||||||
.into_response(req);
|
.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" {
|
if var("NOINDEX").unwrap_or_else(|_| "false".to_string()) == "true" {
|
||||||
return Ok(ServiceResponse::new(
|
return Ok(ServiceResponse::new(
|
||||||
req.clone(),
|
req.to_owned(),
|
||||||
HttpResponse::NotFound().body(""),
|
HttpResponse::NotFound().body(""),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -292,7 +292,7 @@ fn render_index(
|
|||||||
let res = HttpResponse::Ok()
|
let res = HttpResponse::Ok()
|
||||||
.content_type("text/html; charset=utf-8")
|
.content_type("text/html; charset=utf-8")
|
||||||
.body(index);
|
.body(index);
|
||||||
Ok(ServiceResponse::new(req.clone(), res))
|
Ok(ServiceResponse::new(req.to_owned(), res))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -479,72 +479,8 @@ async fn main() -> std::io::Result<()> {
|
|||||||
open_in_browser(&url);
|
open_in_browser(&url);
|
||||||
}
|
}
|
||||||
|
|
||||||
let addr = if let Some(matches) = matches.subcommand_matches("doc") {
|
|
||||||
let mut cargo_toml = match std::fs::File::open("./Cargo.toml") {
|
if let Some(matches) = matches.subcommand_matches("doc") {
|
||||||
Ok(file) => file,
|
|
||||||
Err(e) => {
|
|
||||||
error!("[ERROR] {}", e.to_string());
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let mut contents = String::new();
|
|
||||||
match cargo_toml.read_to_string(&mut contents) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
error!("[ERROR] {}", e.to_string());
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let contents: CargoToml = match toml::from_str(&contents) {
|
|
||||||
Ok(t) => t,
|
|
||||||
Err(e) => {
|
|
||||||
error!("[ERROR] {}", e.to_string());
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let crate_name = contents.package.name;
|
|
||||||
info!("[INFO] Generating document (may take a while)");
|
|
||||||
match std::process::Command::new("cargo").arg("doc").output() {
|
|
||||||
Ok(output) => {
|
|
||||||
let output = std::str::from_utf8(&output.stderr).unwrap_or("");
|
|
||||||
if output.starts_with("error: could not find `Cargo.toml` in") {
|
|
||||||
error!("[ERROR] Cargo.toml Not Found");
|
|
||||||
return Ok(());
|
|
||||||
} else if output.starts_with("error: ") {
|
|
||||||
error!(
|
|
||||||
"[ERROR] {}",
|
|
||||||
output.strip_prefix("error: ").unwrap_or(output)
|
|
||||||
);
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("[ERROR] Cargo Error: {}", e.to_string());
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let path = Path::new("./target/doc/");
|
|
||||||
let mut index_path = path.to_path_buf();
|
|
||||||
index_path.push(crate_name.to_string() + "/index.html");
|
|
||||||
if !index_path.exists() || !index_path.is_file() {
|
|
||||||
error!("[ERROR] Cargo Error: doc path not found");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
set_var("ROOT", display_path(path));
|
|
||||||
let ip = matches
|
|
||||||
.value_of("address")
|
|
||||||
.unwrap_or("127.0.0.1")
|
|
||||||
.to_string();
|
|
||||||
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 },
|
|
||||||
matches.value_of("port").unwrap_or("8000"),
|
|
||||||
crate_name,
|
|
||||||
);
|
|
||||||
if !matches.is_present("noopen") {
|
|
||||||
open_in_browser(&url);
|
|
||||||
}
|
|
||||||
if !matches.is_present("log") {
|
if !matches.is_present("log") {
|
||||||
set_var("RUST_LOG", "info,actix_web::middleware::logger=off");
|
set_var("RUST_LOG", "info,actix_web::middleware::logger=off");
|
||||||
}
|
}
|
||||||
@ -554,12 +490,8 @@ async fn main() -> std::io::Result<()> {
|
|||||||
if matches.is_present("nocolor") {
|
if matches.is_present("nocolor") {
|
||||||
set_var("RUST_LOG_STYLE", "never");
|
set_var("RUST_LOG_STYLE", "never");
|
||||||
}
|
}
|
||||||
addr
|
}
|
||||||
} else {
|
|
||||||
addr
|
|
||||||
};
|
|
||||||
|
|
||||||
let addr_copy = addr.clone();
|
|
||||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
|
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
|
||||||
.format(move |buf, record| {
|
.format(move |buf, record| {
|
||||||
let data = record.args().to_string();
|
let data = record.args().to_string();
|
||||||
@ -613,7 +545,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
let data = format!(
|
let data = format!(
|
||||||
"[INFO] Serving {} on {}",
|
"[INFO] Serving {} on {}",
|
||||||
var("ROOT").unwrap_or_else(|_| ".".to_string()),
|
var("ROOT").unwrap_or_else(|_| ".".to_string()),
|
||||||
addr_copy
|
var("LISTEN_ADDRESS").unwrap_or_else(|_| "0.0.0.0:8000".to_string())
|
||||||
);
|
);
|
||||||
return writeln!(buf, "\r{}", green.value(data));
|
return writeln!(buf, "\r{}", green.value(data));
|
||||||
}
|
}
|
||||||
@ -638,6 +570,78 @@ async fn main() -> std::io::Result<()> {
|
|||||||
})
|
})
|
||||||
.init();
|
.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,
|
||||||
|
Err(e) => {
|
||||||
|
error!("[ERROR] {}", e.to_string());
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut contents = String::new();
|
||||||
|
match cargo_toml.read_to_string(&mut contents) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => {
|
||||||
|
error!("[ERROR] {}", e.to_string());
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let contents: CargoToml = match toml::from_str(&contents) {
|
||||||
|
Ok(t) => t,
|
||||||
|
Err(e) => {
|
||||||
|
error!("[ERROR] {}", e.to_string());
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let crate_name = contents.package.name;
|
||||||
|
info!("[INFO] Generating document (may take a while)");
|
||||||
|
match std::process::Command::new("cargo").arg("doc").output() {
|
||||||
|
Ok(output) => {
|
||||||
|
let output = std::str::from_utf8(&output.stderr).unwrap_or("");
|
||||||
|
if output.starts_with("error: could not find `Cargo.toml` in") {
|
||||||
|
error!("[ERROR] Cargo.toml Not Found");
|
||||||
|
return Ok(());
|
||||||
|
} else if output.starts_with("error: ") {
|
||||||
|
error!(
|
||||||
|
"[ERROR] {}",
|
||||||
|
output.strip_prefix("error: ").unwrap_or(output)
|
||||||
|
);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("[ERROR] Cargo Error: {}", e.to_string());
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let path = Path::new("./target/doc/");
|
||||||
|
let mut index_path = path.to_path_buf();
|
||||||
|
index_path.push(crate_name.to_string() + "/index.html");
|
||||||
|
if !index_path.exists() || !index_path.is_file() {
|
||||||
|
error!("[ERROR] Cargo Error: doc path not found");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
set_var("ROOT", display_path(path));
|
||||||
|
let ip = matches
|
||||||
|
.value_of("address")
|
||||||
|
.unwrap_or("127.0.0.1")
|
||||||
|
.to_string();
|
||||||
|
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 },
|
||||||
|
matches.value_of("port").unwrap_or("8000"),
|
||||||
|
crate_name,
|
||||||
|
);
|
||||||
|
if !matches.is_present("noopen") {
|
||||||
|
open_in_browser(&url);
|
||||||
|
}
|
||||||
|
addr
|
||||||
|
} else {
|
||||||
|
addr
|
||||||
|
};
|
||||||
|
set_var("LISTEN_ADDRESS", addr);
|
||||||
|
|
||||||
let server = HttpServer::new(move || {
|
let server = HttpServer::new(move || {
|
||||||
let app = App::new()
|
let app = App::new()
|
||||||
.wrap_fn(|req, srv| {
|
.wrap_fn(|req, srv| {
|
||||||
@ -729,9 +733,9 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.with_no_client_auth()
|
.with_no_client_auth()
|
||||||
.with_single_cert(cert, key)
|
.with_single_cert(cert, key)
|
||||||
.expect("bad certificate/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 {
|
} else {
|
||||||
server.bind(addr)
|
server.bind(var("LISTEN_ADDRESS").unwrap_or_else(|_| "0.0.0.0:8000".to_string()))
|
||||||
};
|
};
|
||||||
server?.run().await
|
server?.run().await
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user