Fix accidental breakage with serve handle stop method

This commit is contained in:
Filip Tibell 2025-04-28 22:02:37 +02:00
parent e79ffc11a5
commit 5ddbc64fbb
No known key found for this signature in database
2 changed files with 27 additions and 3 deletions

View file

@ -10,7 +10,7 @@ pub(crate) mod url;
use self::{
client::ws_stream::WsStream,
server::{config::ServeConfig, handle::ServeHandle},
server::config::ServeConfig,
shared::{request::Request, response::Response, websocket::Websocket},
};
@ -50,8 +50,10 @@ async fn net_socket(_: Lua, url: String) -> LuaResult<Websocket<WsStream>> {
self::client::connect_websocket(url).await
}
async fn net_serve(lua: Lua, (port, config): (u16, ServeConfig)) -> LuaResult<ServeHandle> {
self::server::serve(lua, port, config).await
async fn net_serve(lua: Lua, (port, config): (u16, ServeConfig)) -> LuaResult<LuaTable> {
self::server::serve(lua.clone(), port, config)
.await?
.into_lua_table(lua)
}
fn net_url_encode(

View file

@ -8,6 +8,7 @@ use std::{
use async_channel::{unbounded, Receiver, Sender};
use lune_utils::TableBuilder;
use mlua::prelude::*;
#[derive(Debug, Clone)]
@ -27,6 +28,27 @@ impl ServeHandle {
};
(this, receiver)
}
// TODO: Remove this in the next major release to use colon/self
// based call syntax and userdata implementation below instead
pub fn into_lua_table(self, lua: Lua) -> LuaResult<LuaTable> {
let shutdown = self.shutdown.clone();
let sender = self.sender.clone();
TableBuilder::new(lua)?
.with_value("ip", self.addr.ip().to_string())?
.with_value("port", self.addr.port())?
.with_function("stop", move |_, ()| {
if shutdown.load(Ordering::SeqCst) {
Err(LuaError::runtime("Server already stopped"))
} else {
shutdown.store(true, Ordering::SeqCst);
sender.try_send(()).ok();
sender.close();
Ok(())
}
})?
.build()
}
}
impl LuaUserData for ServeHandle {