Skip to content

Commit

Permalink
added open support(WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
1vipulgupta authored and 1vipulgupta committed Aug 7, 2023
1 parent eea34f0 commit 2ebc56a
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 3 deletions.
185 changes: 185 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ tokio = { version = "1.29", features = ["full"], optional = true }
tower-http = { version = "0.4", features = ["full"], optional = true }
tower = { version = "0.4", optional = true }
http = { version = "0.2", optional = true }
clap = { version = "4.3.19", features = ["derive"] }
open = { version = "5.0.0", optional = true }

[features]
hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"]
Expand All @@ -43,6 +45,7 @@ ssr = [
"dep:tower-http",
"dep:axum-macros",
"dep:http",
"dep:open"
]

# Defines a size-optimized profile for the WASM bundle in release mode
Expand Down
14 changes: 14 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use clap::Parser;

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
#[arg(short = 'n', long="no-open", default_value_t = false)]
pub no_open: bool,
}

pub fn parse() -> Args {
let args = Args::parse();
return args;
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
mod app;
mod nix;
mod cli;
#[cfg(feature = "ssr")]
mod server;

#[cfg(feature = "ssr")]
#[tokio::main]
async fn main() {
server::main().await
let args = cli::parse();
print!("{}",args.no_open);
server::main(args.no_open).await
}
12 changes: 10 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use axum::{body::Body, http::Request, response::IntoResponse};
use axum::{routing::post, Router};
use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes};
use open;
use tower_http::services::ServeDir;

pub async fn main() {
pub async fn main(no_open: bool) {
let conf = get_configuration(None).await.unwrap();
let routes = generate_route_list(|cx| view! { cx, <App/> }).await;
let client_dist = ServeDir::new(conf.leptos_options.site_root.clone());
Expand All @@ -24,7 +25,14 @@ pub async fn main() {
// error handler)
.fallback_service(client_dist.clone().not_found_service(not_found_service))
.with_state(conf.leptos_options.clone());
println!("Launching http://{}", &conf.leptos_options.site_addr);
let path = ["http://", &conf.leptos_options.site_addr.to_string()].join("");
println!("Launching {}", &path);
if !no_open {
match open::that(&path) {
Ok(()) => println!("Opened '{}' successfully.", &path),
Err(err) => eprintln!("An error occurred when opening '{}': {}", &path, err),
}
}
axum::Server::bind(&conf.leptos_options.site_addr)
.serve(app.into_make_service())
.await
Expand Down

0 comments on commit 2ebc56a

Please sign in to comment.