feat: open browser during serve with "--open"

This commit is contained in:
Antonio Curavalea committed 2023-04-30 23:13:46 +03:00
1 parent 9ce5fc508d
commit f6cd48c612
5 files changed
+71 -12

No files matched your search

+5
View File
@@ -45,6 +45,11 @@ pub struct ConfigOptsServe {
#[clap(default_value_t = 8080)]
pub port: u16,
/// Open the app in the default browser [default: false]
#[clap(long)]
#[serde(default)]
pub open: bool,
/// Build a example [default: ""]
#[clap(long)]
pub example: Option<String>,
+1 -1
View File
@@ -68,7 +68,7 @@ impl Serve {
Serve::regen_dev_page(&crate_config)?;
// start the develop server
server::startup(self.serve.port, crate_config.clone()).await?;
server::startup(self.serve.port, crate_config.clone(), self.serve.open).await?;
Ok(())
}
+28 -11
View File
@@ -55,7 +55,7 @@ struct WsReloadState {
update: broadcast::Sender<()>,
}
pub async fn startup(port: u16, config: CrateConfig) -> Result<()> {
pub async fn startup(port: u16, config: CrateConfig, start_browser: bool) -> Result<()> {
// ctrl-c shutdown checker
let crate_config = config.clone();
let _ = ctrlc::set_handler(move || {
@@ -64,10 +64,11 @@ pub async fn startup(port: u16, config: CrateConfig) -> Result<()> {
});
let ip = get_ip().unwrap_or(String::from("0.0.0.0"));
if config.hot_reload {
startup_hot_reload(ip, port, config).await?
startup_hot_reload(ip, port, config, start_browser).await?
} else {
startup_default(ip, port, config).await?
startup_default(ip, port, config, start_browser).await?
}
Ok(())
}
@@ -129,7 +130,7 @@ pub async fn hot_reload_handler(
}
#[allow(unused_assignments)]
pub async fn startup_hot_reload(ip: String, port: u16, config: CrateConfig) -> Result<()> {
pub async fn startup_hot_reload(ip: String, port: u16, config: CrateConfig, start_browser: bool) -> Result<()> {
let first_build_result = crate::builder::build(&config, false)?;
log::info!("🚀 Starting development server...");
@@ -319,14 +320,25 @@ pub async fn startup_hot_reload(ip: String, port: u16, config: CrateConfig) -> R
.layer(Extension(ws_reload_state))
.layer(Extension(hot_reload_state));
axum::Server::bind(&format!("0.0.0.0:{}", port).parse().unwrap())
.serve(router.into_make_service())
.await?;
let addr = format!("0.0.0.0:{}", port).parse().unwrap();
let server = axum::Server::bind(&addr).serve(router.into_make_service());
if start_browser {
let _ = open::that(format!("http://{}", addr));
}
server.await?;
Ok(())
}
pub async fn startup_default(ip: String, port: u16, config: CrateConfig) -> Result<()> {
pub async fn startup_default(
ip: String,
port: u16,
config: CrateConfig,
start_browser: bool,
) -> Result<()> {
let first_build_result = crate::builder::build(&config, false)?;
log::info!("🚀 Starting development server...");
@@ -461,9 +473,14 @@ pub async fn startup_default(ip: String, port: u16, config: CrateConfig) -> Resu
)
.layer(Extension(ws_reload_state));
axum::Server::bind(&format!("0.0.0.0:{}", port).parse().unwrap())
.serve(router.into_make_service())
.await?;
let addr = format!("0.0.0.0:{}", port).parse().unwrap();
let server = axum::Server::bind(&addr).serve(router.into_make_service());
if start_browser {
let _ = open::that(format!("http://{}", addr));
}
server.await?;
Ok(())
}