Skip to content

Commit

Permalink
use open crate for autolaunching ui in browser (#64)
Browse files Browse the repository at this point in the history
* add option to automatically open UI on launch
  • Loading branch information
cgcha authored Aug 26, 2023
1 parent 6212b57 commit c8607e9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ axum = "0.6.18"
clap = { version = "4.3.11", features = ["derive"] }
clap_lex = "0.5.0"
directories-next = "2.0.0"
open = "5.0.0"
rcon = { version = "0.5.2", features = ["rt-tokio"], git = "https://github.com/MegaAntiCheat/rust-rcon" }
regex = "1.8.4"
serde = { version = "1.0.164", features = ["rc"] }
Expand Down
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use player_records::PlayerRecords;
use std::path::Path;
use std::time::Duration;
use steamapi::steam_api_loop;
use steamid_ng::SteamID;
Expand Down Expand Up @@ -52,6 +53,9 @@ pub struct Args {
/// Do not panic on detecting missing launch options or failure to read/parse the localconfig.vdf file.
#[arg(short, long = "ignore_launch_opts", action=ArgAction::SetTrue, default_value_t=false)]
pub ignore_launch_options: bool,
/// Launch the web-ui in the default browser on startup
#[arg(long = "autolaunch_ui", action=ArgAction::SetTrue, default_value_t=false)]
pub autolaunch_ui: bool,
}

#[tokio::main]
Expand Down Expand Up @@ -169,6 +173,9 @@ async fn main() {
tracing::error!("Failed to save playerlist: {:?}", e);
}

// Check autolaunch ui setting before settings is borrowed
let autolaunch_ui = args.autolaunch_ui || (&settings).get_autolaunch_ui();

// Initialize State
State::initialize_state(State::new(settings, playerlist));
let io = IOManager::new();
Expand All @@ -180,6 +187,12 @@ async fn main() {
web::web_main(port).await;
});

if autolaunch_ui {
if let Err(e) = open::that(Path::new(&format!("http://localhost:{}", port))) {
tracing::error!("Failed to open web browser: {:?}", e);
}
}

// Steam API loop
let (steam_api_requester, steam_api_receiver) = tokio::sync::mpsc::unbounded_channel();
tokio::task::spawn(async move {
Expand Down
7 changes: 7 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct Settings {
rcon_password: Arc<str>,
steam_api_key: Arc<str>,
port: u16,
autolaunch_ui: bool,
external: serde_json::Value,
#[serde(skip)]
override_tf2_dir: Option<PathBuf>,
Expand Down Expand Up @@ -276,6 +277,11 @@ impl Settings {
self.port = port;
self.save_ok();
}

pub fn get_autolaunch_ui(&self) -> bool {
self.autolaunch_ui
}

pub fn set_steam_api_key(&mut self, key: Arc<str>) {
self.steam_api_key = key;
self.save_ok();
Expand Down Expand Up @@ -314,6 +320,7 @@ impl Default for Settings {
rcon_password: "mac_rcon".into(),
steam_api_key: "YOUR_API_KEY_HERE".into(),
port: 3621,
autolaunch_ui: false,
override_tf2_dir: None,
override_rcon_password: None,
override_steam_api_key: None,
Expand Down

0 comments on commit c8607e9

Please sign in to comment.