[RELEASE] Version 0.0.7 #9
@ -1,9 +1,12 @@
|
|||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::HashMap, sync::Arc, thread::sleep, time::Duration};
|
||||||
|
|
||||||
use loadstar::{controller::Controller, entities::request::Url, router::Router, template::Template, utilities::responses::{respond_not_found, respond_template}};
|
use loadstar::{controller::Controller, entities::request::Url, router::Router, template::Template, utilities::{async_block::run_async, responses::{respond_not_found, respond_template}}};
|
||||||
use maud::html;
|
use maud::html;
|
||||||
use tiny_http::Request;
|
use tiny_http::Request;
|
||||||
|
|
||||||
|
async fn example_async() {
|
||||||
|
sleep(Duration::from_secs(1));
|
||||||
|
}
|
||||||
|
|
||||||
struct MainRouter {
|
struct MainRouter {
|
||||||
routes: HashMap<String, fn(Request)>
|
routes: HashMap<String, fn(Request)>
|
||||||
@ -42,6 +45,7 @@ struct MainController;
|
|||||||
|
|
||||||
impl MainController {
|
impl MainController {
|
||||||
fn index_route(request: Request) {
|
fn index_route(request: Request) {
|
||||||
|
run_async(example_async());
|
||||||
respond_template(request, MainTemplate {})
|
respond_template(request, MainTemplate {})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
src/utilities/async_block.rs
Normal file
12
src/utilities/async_block.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use std::future::Future;
|
||||||
|
|
||||||
|
|
||||||
|
pub fn run_async<T>(async_operation: impl Future<Output = T>) -> T {
|
||||||
|
// Use `block_in_place` to ensure runtime is dropped in a blocking context
|
||||||
|
tokio::task::block_in_place(|| {
|
||||||
|
// Get the current Tokio runtime handle
|
||||||
|
let handle = tokio::runtime::Handle::current();
|
||||||
|
// Run the async operation using the handle and wait for it to complete
|
||||||
|
handle.block_on(async_operation)
|
||||||
|
})
|
||||||
|
}
|
@ -1 +1,2 @@
|
|||||||
pub mod responses;
|
pub mod responses;
|
||||||
|
pub mod async_block;
|
||||||
|
Loading…
Reference in New Issue
Block a user