Folder to host as optional argument

This commit is contained in:
Piotr Siuszko 2023-08-29 20:17:01 +02:00
parent f39a34a9eb
commit fac40797b7
3 changed files with 32 additions and 6 deletions

View File

@ -2,7 +2,26 @@
[![build](https://github.com/Leinnan/lwa_simple_server/actions/workflows/rust.yml/badge.svg)](https://github.com/Leinnan/lwa_simple_server/actions/workflows/rust.yml) [![build](https://github.com/Leinnan/lwa_simple_server/actions/workflows/rust.yml/badge.svg)](https://github.com/Leinnan/lwa_simple_server/actions/workflows/rust.yml)
Simple server made with hosting locally webgl games in mind. ```
Simple server made with hosting locally webgl games in mind
Usage: lwa_simple_server [OPTIONS] [FOLDER_TO_HOST]
Arguments:
[FOLDER_TO_HOST] Folder to host, current by default
Options:
--ssl
Should use SSL, false by default
-a, --address <ADDRESS>
Specifies hosting address, "localhost:8080" by default
-c, --certificates-folder <CERTIFICATES_FOLDER>
-h, --help
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).
@ -28,7 +47,7 @@ To start run it in folder that should be root folder of hosted site:
```bash ```bash
cd desired/folder cd desired/folder
lwa_simple_server "folder_to_host" lwa_simple_server "folder_to_host/current_by_default"
``` ```

View File

@ -5,8 +5,8 @@ use std::path::PathBuf;
#[derive(Debug, Parser, Clone)] #[derive(Debug, Parser, Clone)]
#[command(name = "lwa_simple_server", version = "0.1.0", author = "Mev Lyshkin")] #[command(name = "lwa_simple_server", version = "0.1.0", author = "Mev Lyshkin")]
pub struct SimpleServer { pub struct SimpleServer {
/// Folder to host /// Folder to host, current by default
pub folder_to_host: String, pub folder_to_host: Option<PathBuf>,
/// Should use SSL, false by default /// Should use SSL, false by default
#[arg(long)] #[arg(long)]
pub ssl: bool, pub ssl: bool,
@ -19,6 +19,13 @@ pub struct SimpleServer {
} }
impl SimpleServer { impl SimpleServer {
pub fn get_folder_to_host(&self) -> PathBuf {
if self.folder_to_host.is_some() {
self.folder_to_host.clone().unwrap()
} else {
PathBuf::from(".")
}
}
pub fn get_address(&self) -> String { pub fn get_address(&self) -> String {
if self.address.is_some() { if self.address.is_some() {
return self.address.clone().unwrap(); return self.address.clone().unwrap();

View File

@ -12,7 +12,7 @@ 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.folder_to_host.clone(); 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'`
@ -20,7 +20,7 @@ async fn main() -> std::io::Result<()> {
"Starting server on address: {}://{} with hosted folder: {} [SSL={}]", "Starting server on address: {}://{} with hosted folder: {} [SSL={}]",
if app_args.ssl{ "https" } else {"http"}, if app_args.ssl{ "https" } else {"http"},
app_args.get_address(), app_args.get_address(),
path, path.display(),
app_args.ssl app_args.ssl
); );
} }