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)
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).
@ -28,7 +47,7 @@ To start run it in folder that should be root folder of hosted site:
```bash
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)]
#[command(name = "lwa_simple_server", version = "0.1.0", author = "Mev Lyshkin")]
pub struct SimpleServer {
/// Folder to host
pub folder_to_host: String,
/// Folder to host, current by default
pub folder_to_host: Option<PathBuf>,
/// Should use SSL, false by default
#[arg(long)]
pub ssl: bool,
@ -19,6 +19,13 @@ pub struct 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 {
if self.address.is_some() {
return self.address.clone().unwrap();

View File

@ -12,7 +12,7 @@ async fn main() -> std::io::Result<()> {
let app_args = crate::app::SimpleServer::parse();
env::set_var("RUST_LOG", "actix_web=debug,actix_server=info");
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'`
@ -20,7 +20,7 @@ async fn main() -> std::io::Result<()> {
"Starting server on address: {}://{} with hosted folder: {} [SSL={}]",
if app_args.ssl{ "https" } else {"http"},
app_args.get_address(),
path,
path.display(),
app_args.ssl
);
}