allow specifing port, not address

This commit is contained in:
Piotr Siuszko 2023-08-29 20:45:01 +02:00
parent 417800fcad
commit 569bb8884e
5 changed files with 28 additions and 34 deletions

2
Cargo.lock generated
View File

@ -891,7 +891,7 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]] [[package]]
name = "lwa_simple_server" name = "lwa_simple_server"
version = "0.2.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix-cors", "actix-cors",
"actix-files", "actix-files",

View File

@ -1,8 +1,16 @@
[package] [package]
name = "lwa_simple_server" name = "lwa_simple_server"
version = "0.2.0" version = "0.1.0"
authors = ["Mev Lyshkin <mev_lyshkin@protonmail.com>"] authors = ["Mev Lyshkin <mev_lyshkin@protonmail.com>"]
edition = "2018" edition = "2018"
repository = "https://github.com/Leinnan/lwa_simple_server"
homepage = "https://github.com/Leinnan/lwa_simple_server"
readme = "README.md"
license = "MIT"
keywords = ["server", "static", "single-page", "https"]
categories = ["command-line-utilities", "development-tools"]
description = "Simple server made with hosting locally webgl games in mind."
exclude = ["/.github"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -11,16 +11,11 @@ Arguments:
[FOLDER_TO_HOST] Folder to host, current by default [FOLDER_TO_HOST] Folder to host, current by default
Options: Options:
--ssl --ssl Should use SSL, false by default
Should use SSL, false by default -p, --port <PORT> Specifies hosting port, "8080" by default
-a, --address <ADDRESS>
Specifies hosting address, "localhost:8080" by default
-c, --certificates-folder <CERTIFICATES_FOLDER> -c, --certificates-folder <CERTIFICATES_FOLDER>
-h, --help Print help
-h, --help -V, --version Print version
Print help
-V, --version
Print version
``` ```
It makes testing Unity webgl games easy, even allows connecting with different domains(less CORS issues during tests). It makes testing Unity webgl games easy, even allows connecting with different domains(less CORS issues during tests).

View File

@ -10,10 +10,10 @@ pub struct SimpleServer {
/// Should use SSL, false by default /// Should use SSL, false by default
#[arg(long)] #[arg(long)]
pub ssl: bool, pub ssl: bool,
/// Specifies hosting address, "localhost:8080" by default /// Specifies hosting port, "8080" by default
#[arg(short, long)] #[arg(short, long)]
pub address: Option<String>, pub port: Option<i32>,
// Specifies folder containing "key.pem" and "cert.pem" required for ssl hosting, defaults to current folder /// Specifies folder containing "key.pem" and "cert.pem" required for ssl hosting, defaults to current folder
#[arg(short, long)] #[arg(short, long)]
certificates_folder: Option<PathBuf>, certificates_folder: Option<PathBuf>,
} }
@ -27,18 +27,13 @@ impl SimpleServer {
} }
} }
pub fn get_address(&self) -> String { pub fn get_address(&self) -> String {
if self.address.is_some() { format!("localhost:{}", self.port.clone().unwrap_or(8080))
return self.address.clone().unwrap();
}
"localhost:8080".to_string()
} }
fn get_certificates_folder(&self) -> PathBuf { fn get_certificates_folder(&self) -> PathBuf {
if self.certificates_folder.is_some() { self.certificates_folder
self.certificates_folder.clone().unwrap() .clone()
} else { .unwrap_or(PathBuf::from("."))
PathBuf::from(".")
}
} }
pub fn get_private_key_path(&self) -> PathBuf { pub fn get_private_key_path(&self) -> PathBuf {

View File

@ -12,18 +12,14 @@ async fn main() -> std::io::Result<()> {
let app_args = crate::app::SimpleServer::parse(); let app_args = crate::app::SimpleServer::parse();
env::set_var("RUST_LOG", "actix_web=debug,actix_server=info"); env::set_var("RUST_LOG", "actix_web=debug,actix_server=info");
env_logger::init(); env_logger::init();
let path = app_args.get_folder_to_host();
{
// `openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'` // `openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'`
println!( println!(
"Starting server on address: {}://{} with hosted folder: {} [SSL={}]", "Starting server on address: {}://{}, folder to host: {}",
if app_args.ssl{ "https" } else {"http"}, if app_args.ssl { "https" } else { "http" },
app_args.get_address(), app_args.get_address(),
path.display(), app_args.get_folder_to_host().display()
app_args.ssl
); );
} let path = app_args.get_folder_to_host();
let server = HttpServer::new(move || { let server = HttpServer::new(move || {
App::new() App::new()