Skip to main content

teaql_tool_extra/
server.rs

1use teaql_tool_core::{Result, TeaQLToolError};
2use tower_http::services::ServeDir;
3use tokio::runtime::Runtime;
4
5#[derive(Debug, Clone)]
6pub struct ServerTool;
7
8impl ServerTool {
9    pub fn new() -> Self { Self }
10
11    pub fn serve_dir(&self, dir: &str, port: u16) -> Result<()> {
12        let rt = Runtime::new().map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))?;
13        let addr = format!("0.0.0.0:{}", port);
14        
15        rt.block_on(async move {
16            let app = axum::Router::new().fallback_service(ServeDir::new(dir));
17            let listener = tokio::net::TcpListener::bind(&addr).await.map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))?;
18            println!("Static server listening on http://{}", addr);
19            axum::serve(listener, app).await.map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))
20        })
21    }
22}
23
24impl Default for ServerTool {
25    fn default() -> Self { Self::new() }
26}