mirror of
https://github.com/neosubhamoy/pytubepp-helper.git
synced 2026-02-04 11:22:22 +05:30
(feat): added dynamic port config for msghost, new settings page and ui improvements
This commit is contained in:
37
src-tauri/msghost/src/config.rs
Normal file
37
src-tauri/msghost/src/config.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use directories::ProjectDirs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Config {
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
port: 3030,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_config_dir() -> Option<PathBuf> {
|
||||
ProjectDirs::from("com", "neosubhamoy", "pytubepp-helper")
|
||||
.map(|proj_dirs| proj_dirs.config_dir().to_path_buf())
|
||||
}
|
||||
|
||||
pub fn get_config_path() -> Option<PathBuf> {
|
||||
get_config_dir().map(|dir| dir.join("config.json"))
|
||||
}
|
||||
|
||||
pub fn load_config() -> Config {
|
||||
if let Some(config_path) = get_config_path() {
|
||||
if let Ok(content) = fs::read_to_string(config_path) {
|
||||
if let Ok(config) = serde_json::from_str(&content) {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
}
|
||||
Config::default()
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
mod config;
|
||||
use config::load_config;
|
||||
use std::io::{self, Read, Write};
|
||||
use websocket::client::ClientBuilder;
|
||||
use websocket::OwnedMessage;
|
||||
@@ -5,6 +7,11 @@ use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
use serde_json::Value;
|
||||
|
||||
fn get_websocket_url() -> String {
|
||||
let config = load_config();
|
||||
format!("ws://localhost:{}", config.port)
|
||||
}
|
||||
|
||||
fn connect_with_retry(url: &str, max_attempts: u32) -> Result<websocket::sync::Client<std::net::TcpStream>, Box<dyn std::error::Error>> {
|
||||
let mut attempts = 0;
|
||||
loop {
|
||||
@@ -70,10 +77,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
let parsed: Value = serde_json::from_str(&input)?;
|
||||
|
||||
let websocket_url = "ws://localhost:3030";
|
||||
let websocket_url = get_websocket_url();
|
||||
eprintln!("Attempting to connect to {}", websocket_url);
|
||||
|
||||
let mut client = match connect_with_retry(websocket_url, 2) {
|
||||
let mut client = match connect_with_retry(&websocket_url, 2) {
|
||||
Ok(client) => client,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to connect after multiple attempts: {:?}", e);
|
||||
|
||||
Reference in New Issue
Block a user