First commit

This commit is contained in:
Mev Lyshkin 2021-04-21 18:26:12 +02:00
commit 0e4481f9c4
5 changed files with 2183 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
*pem

2086
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

25
Cargo.toml Executable file
View File

@ -0,0 +1,25 @@
[package]
name = "lwa_simple_server"
version = "0.1.0"
authors = ["Mev Lyshkin <mev_lyshkin@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[profile.release]
opt-level = 'z'
panic = 'abort'
lto = true
[profile.dev.package."*"]
opt-level = 2
[dependencies]
actix-web = { version = "~3", features = ["openssl"] }
openssl = { version = "0.10" }
actix-files = "^0.5.0"
actix-cors = "^0.5.4"
env_logger = "^0.8.3"
confy = "^0.4.0"
serde = "^1.0"
serde_derive = "^1.0"

20
src/config.rs Normal file
View File

@ -0,0 +1,20 @@
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ServerConfig {
pub folder_to_host: String,
pub bind_address: String,
pub private_key_file: String,
pub certificate_chain_file: String,
pub use_ssl: bool
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
folder_to_host: String::from(std::env::current_dir().unwrap().to_str().unwrap()),
bind_address: "localhost:8000".to_string(),
private_key_file: "key.pem".to_string(),
certificate_chain_file: "cert.pem".to_string(),
use_ssl: false,
}
}
}

50
src/main.rs Executable file
View File

@ -0,0 +1,50 @@
#[macro_use]
extern crate serde_derive;
extern crate confy;
use actix_cors::Cors;
use actix_files as fs;
use actix_web::{middleware, App, HttpServer};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
use std::env;
mod config;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env::set_var("RUST_LOG", "actix_web=debug,actix_server=info");
env_logger::init();
let cfg: config::ServerConfig = confy::load("lwa_simple_server").unwrap();
{
println!("The configuration is:\n{:#?}", cfg);
// `openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'`
println!(
"Starting server on address: {} with hosted folder: {} wit SSL: {}",
cfg.bind_address, cfg.folder_to_host, cfg.use_ssl
);
}
let server = HttpServer::new(move || {
let cfg: config::ServerConfig = confy::load("lwa_simple_server").unwrap();
App::new()
.wrap(middleware::Logger::default())
.wrap(Cors::default())
.service(fs::Files::new("/", &cfg.folder_to_host).index_file("index.html"))
});
if cfg.use_ssl {
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder
.set_private_key_file(&cfg.private_key_file, SslFiletype::PEM)
.unwrap();
builder.set_certificate_chain_file(&cfg.certificate_chain_file).unwrap();
server.bind_openssl(&cfg.bind_address, builder)?
.run()
.await
} else {
server.bind(&cfg.bind_address)?
.run()
.await
}
}