Skip to content

Commit

Permalink
Fix webserver and webview starting
Browse files Browse the repository at this point in the history
  • Loading branch information
Marekkon5 committed Jan 9, 2024
1 parent d7aa47c commit cddf7c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
10 changes: 6 additions & 4 deletions crates/onetagger-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::net::SocketAddr;
use std::time::Duration;
use anyhow::Error;
use axum::body::Body;
use axum::extract::{Query, WebSocketUpgrade, State, Request, Path};
use axum::extract::{Query, WebSocketUpgrade, State, Request};
use axum::http::StatusCode;
use axum::http::header::CONTENT_TYPE;
use axum::response::IntoResponse;
Expand Down Expand Up @@ -38,7 +38,7 @@ pub struct StartContext {
pub browser: bool,
}

fn start_async_thread(context: StartContext) -> Result<(), Error> {
fn start_async_runtime(context: StartContext) -> Result<(), Error> {
let expose = context.expose;
Builder::new_multi_thread().enable_all().build()?.block_on(async move {
// Register routes
Expand All @@ -48,6 +48,7 @@ fn start_async_thread(context: StartContext) -> Result<(), Error> {
.route("/ws", get(get_ws))
.route("/spotify", get(get_spotify_callback))
.route("/*path", get(get_static_file))
.route("/", get(get_static_file))
.with_state(context);

// Start http server
Expand All @@ -65,7 +66,8 @@ fn start_async_thread(context: StartContext) -> Result<(), Error> {
}

/// Serve assets file
async fn get_static_file(Path(mut path): Path<String>) -> impl IntoResponse {
async fn get_static_file(request: Request<Body>) -> impl IntoResponse {
let mut path = request.uri().to_string();
// Index HTML
if path == "/" {
path = "/index.html".to_string();
Expand Down Expand Up @@ -155,7 +157,7 @@ pub fn start_all(context: StartContext) -> Result<(), Error> {
});
}

start_async_thread(context.clone())?;
start_async_runtime(context.clone())?;
Ok(())
}

16 changes: 12 additions & 4 deletions crates/onetagger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ fn main() {
browser: cli.browser
};

onetagger_ui::start_all(context).expect("Failed to start servers!");
if !cli.server {
start_webview().expect("Failed to start webview!");
// Server mode
if cli.server {
onetagger_ui::start_all(context).expect("Failed to start servers!");
return;
}

// GUI Mode
std::thread::spawn(move || {
onetagger_ui::start_all(context).expect("Failed to start servers!");
});
start_webview().expect("Failed to start webview!");

}

#[derive(Parser, Debug, Clone)]
Expand Down Expand Up @@ -170,7 +178,7 @@ pub fn start_webview() -> Result<(), Error> {

// Configure
let mut webview = builder
.with_url(&format!("http://127.0.0.1:{PORT}"))?
.with_url(&format!("http://127.0.0.1:{PORT}/"))?
.with_devtools(Settings::load().map(|s| s.devtools()).unwrap_or(false))
.with_ipc_handler(move |message| {
let proxy = &p;
Expand Down

0 comments on commit cddf7c8

Please sign in to comment.