#How can I create an http server and then shut it down?

34 messages · Page 1 of 1 (latest)

uneven elk
barren falcon
#

@uneven elk hi thanks I know that library but the thing I asked is not that

#

I want to create this server when I wanted to

#

and want to close it with a comamnd after

#

I will invoke invoke('server:open') command, this will open the server and invoke('server:close') will close it, but I couldn't make it work

#

Tried using tauri:state, mutex, arc everything

#

but couldn't succeed

#

I struggled a lot and ended up here

uneven elk
lament leaf
#

@uneven elk hey, is your server living between the tauri app closing and opening again?

uneven elk
#

what you mean closing and opening again?

lament leaf
#

so I have a tauri app the3s.

the3s starts a server
the3s terminates
the3s launches again
is the server still on?

uneven elk
#

it's running only when the app process is running

lament leaf
#

is there a way to "spawn" a powershell command that will outlive the the3s?

uneven elk
#

the3s?

lament leaf
#

the tauri app

uneven elk
#

do you need hide the powershell window?

lament leaf
#

no need

uneven elk
#
start powershell { echo "the command you want to run" }
lament leaf
#

but on this, I am usings command with powershell and a -command and with the msi installed the powershell is flashing each time I use the command in tauri app, is there a way to silently run the command without the powershell window flashing?

uneven elk
#
lament leaf
#

@barren falconoh sry for hijacking your thread but it was related... hope you dont mind

@uneven elk Is there a way to move the offtopic replies to its own thread?

uneven elk
#

idk

barren falcon
#

@uneven elk thanks for the code example, The problem is not about tauri it's about my rust knowledge, I'm closing this thread.

barren falcon
#

,@uneven elk sorry for bothering again, I read your code and came up with this but aborting join handle does not stop the server.

server.rs

pub struct AuthServer {
    server_join_handle: Option<tauri::async_runtime::JoinHandle<()>>,
}

impl AuthServer {
    pub fn new() -> AuthServer {
        AuthServer {
            server_join_handle: None,
        }
    }

    pub fn listen(&mut self, window: Window) {
        self.server_join_handle = Some(tauri::async_runtime::spawn(async move {
            let server = Server::http("0.0.0.0:23846").unwrap();
            for request in server.incoming_requests() {
                handle_code_request(request, &window);
            }
        }));
    }

    pub fn stop(&mut self) {
        if let Some(join_handle) = self.server_join_handle.take() {
            join_handle.abort();
        }
    }
}
#
#[tauri::command]
pub fn start_server(window: Window, state: State<'_, Mutex<AuthServer>>) {
    let mut server = state.lock().unwrap();
    server.listen(window);
}

#[tauri::command]
pub fn stop_server(window: Window, state: State<'_, Mutex<AuthServer>>) {
    let mut server = state.lock().unwrap();
    server.stop();
}
#

What should I do to close the server NOOO

uneven elk
barren falcon
uneven elk
#

it's using futures::future::select.

barren falcon
#

@uneven elk hi again, can you be a little clear? I read futures docs and tried to the thing you said but I'm getting error

I am not experienced rust developer sorry

uneven elk
#

umm Idk this too.
maybe you could use a loop with recv_timeout
then check the receiver

barren falcon