Skip to content

Commit 0470060

Browse files
committed
Start & Destroy server only needed
1 parent d2ac135 commit 0470060

File tree

6 files changed

+66
-19
lines changed

6 files changed

+66
-19
lines changed

src-tauri/src/commands.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use std::{fs::File, io::BufReader};
1+
use std::{fs::File, io::BufReader, sync::Mutex};
22

33
use rodio::{Decoder, OutputStream, Sink};
4-
use tauri::AppHandle;
4+
use tauri::{AppHandle, State, Window};
5+
6+
use crate::server::AuthServer;
57

68
#[tauri::command]
79
pub fn play_notification_sound(app: AppHandle) {
@@ -32,3 +34,15 @@ pub fn set_icon_template(is_template: bool, app: AppHandle) {
3234
))
3335
.unwrap();
3436
}
37+
38+
#[tauri::command]
39+
pub fn start_server(window: Window, state: State<'_, Mutex<AuthServer>>) {
40+
let mut server = state.lock().unwrap();
41+
server.listen(window);
42+
}
43+
44+
#[tauri::command]
45+
pub fn stop_server(window: Window, state: State<'_, Mutex<AuthServer>>) {
46+
let mut server = state.lock().unwrap();
47+
server.stop();
48+
}

src-tauri/src/main.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
all(not(debug_assertions), target_os = "windows"),
33
windows_subsystem = "windows"
44
)]
5+
#![allow(warnings, unused)]
56

67
mod commands;
7-
mod http;
8+
mod server;
89

9-
use commands::{play_notification_sound, set_icon_template};
10-
use http::apply_http;
10+
use std::sync::Mutex;
11+
12+
use commands::{play_notification_sound, set_icon_template, start_server, stop_server};
13+
use server::AuthServer;
1114

1215
use tauri::{
1316
ActivationPolicy, App, AppHandle, GlobalWindowEvent, Manager, PhysicalPosition, SystemTray,
@@ -59,8 +62,6 @@ fn handle_setup(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
5962
)
6063
.expect("Unsupported platform! 'apply_vibrancy' is only supported on macOS");
6164

62-
apply_http(&win);
63-
6465
Ok(())
6566
}
6667

@@ -86,9 +87,12 @@ fn main() {
8687
let tray = SystemTray::new();
8788

8889
tauri::Builder::default()
90+
.manage(Mutex::new(AuthServer::new()))
8991
.invoke_handler(tauri::generate_handler![
9092
play_notification_sound,
91-
set_icon_template
93+
set_icon_template,
94+
start_server,
95+
stop_server
9296
])
9397
.plugin(tauri_plugin_autostart::init(
9498
MacosLauncher::LaunchAgent,

src-tauri/src/http.rs renamed to src-tauri/src/server.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::{io::Cursor, str::FromStr};
1+
use tauri::{async_runtime::JoinHandle, Window};
2+
3+
use std::{io::Cursor, str::FromStr, sync::Arc};
24

35
use ::ascii::AsciiString;
4-
use tauri::Window;
56
use tiny_http::{Header, HeaderField, Method, Request, Response, Server};
67

78
const STYLE: &str = r#"
@@ -100,14 +101,36 @@ fn handle_code_request(request: Request, window: &Window) {
100101
request.respond(response).unwrap();
101102
}
102103

103-
pub fn apply_http(window: &Window) {
104-
let win = window.clone();
104+
pub struct AuthServer {
105+
server: Option<Arc<Server>>,
106+
}
105107

106-
tauri::async_runtime::spawn(async move {
107-
let server = Server::http("0.0.0.0:23846").unwrap();
108+
impl AuthServer {
109+
pub fn new() -> AuthServer {
110+
AuthServer { server: None }
111+
}
108112

109-
for request in server.incoming_requests() {
110-
handle_code_request(request, &win);
113+
pub fn listen(&mut self, window: Window) {
114+
if self.server.is_some() {
115+
return;
111116
}
112-
});
117+
118+
let server = Arc::new(Server::http("0.0.0.0:23846").unwrap());
119+
std::thread::spawn({
120+
let server = Arc::clone(&server);
121+
move || {
122+
for request in server.incoming_requests() {
123+
handle_code_request(request, &window);
124+
}
125+
}
126+
});
127+
128+
self.server = Some(server)
129+
}
130+
131+
pub fn stop(&mut self) {
132+
if let Some(server) = self.server.take() {
133+
server.unblock();
134+
}
135+
}
113136
}

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export enum Page {
1313
export enum InvokeCommand {
1414
PlayNotificationSound = 'play_notification_sound',
1515
SetIconTemplate = 'set_icon_template',
16+
StartServer = 'start_server',
17+
StopServer = 'stop_server',
1618
}
1719

1820
export enum NotificationSubject {

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import relativeTime from 'dayjs/plugin/relativeTime'
99
import App from './App.vue'
1010
import { AppStorage, cacheStorageFromDisk } from './storage'
1111
import { useStore } from './stores/store'
12-
import { Page } from './constants'
1312
import { initDevtools } from './utils/initDevtools'
1413
import { useKey } from './composables/useKey'
14+
import { Page } from './constants'
1515

1616
(async () => {
1717
dayjs.extend(relativeTime)

src/pages/LandingPage.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script lang="ts" setup>
22
import { open } from '@tauri-apps/api/shell'
33
import { ref } from 'vue'
4+
import { invoke } from '@tauri-apps/api/tauri'
45
import AppButton from '../components/AppButton.vue'
5-
import { Page } from '../constants'
6+
import { InvokeCommand, Page } from '../constants'
67
import { useStore } from '../stores/store'
78
import { useTauriEvent } from '../composables/useTauriEvent'
89
import { getAccessToken } from '../api/token'
@@ -15,6 +16,8 @@ const store = useStore()
1516
1617
const processing = ref(false)
1718
19+
invoke(InvokeCommand.StartServer)
20+
1821
useTauriEvent<string>('code', async ({ payload }) => {
1922
if (processing.value)
2023
return
@@ -32,6 +35,7 @@ useTauriEvent<string>('code', async ({ payload }) => {
3235
3336
AppStorage.set('accessToken', access_token)
3437
AppStorage.set('user', user)
38+
invoke(InvokeCommand.StopServer)
3539
store.setPage(Page.Home)
3640
store.fetchNotifications(true)
3741
}

0 commit comments

Comments
 (0)