This commit is contained in:
azw 2023-11-10 13:48:30 +00:00
parent a3415fcd42
commit 58b4b8e909
2 changed files with 39 additions and 0 deletions

View File

@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1", features = ["full"] }
actix-web = "4"
r9 = { path = "../r9" }
r9_macro = { path = "../r9_macro" }
r9_macro_derive = { path = "../r9_macro_derive" }

View File

@ -2,6 +2,11 @@ use std::rc::{Rc, Weak};
use std::cell::RefCell;
use std::thread::sleep;
use std::time::Duration;
use tokio::runtime::Runtime;
use actix_web::{dev::ServerHandle, middleware, rt, web, HttpRequest, HttpServer};
use actix_web::App as WebApp;
use std::{sync::mpsc, thread, time};
use r9_macro::SharedFromSelf;
use r9_macro_derive::SharedFromSelf;
@ -18,9 +23,29 @@ pub struct App {
zone_id: i32,
node_id: i32,
instance_id: i32,
tokio_rt: Runtime,
user_app: Option<Rc::<RefCell::<dyn UserApp>>>,
}
async fn index(req: HttpRequest) -> &'static str {
"Hello world!asaaa"
}
async fn run_app(tx: mpsc::Sender<ServerHandle>) -> std::io::Result<()> {
let server = HttpServer::new(|| {
WebApp::new()
.service(web::resource("/index.html").to(|| async { "Hello world!" }))
.service(web::resource("/").to(index))
})
.bind(("127.0.0.1", 8080))?
.workers(2)
.run();
let _ = tx.send(server.handle());
server.await
}
impl App {
pub fn instance() -> Rc::<RefCell::<Self>> {
@ -36,6 +61,7 @@ impl App {
zone_id: 0,
node_id: 0,
instance_id: 0,
tokio_rt: Runtime::new().unwrap(),
user_app: None,
_self_wp: Default::default(),
}
@ -49,6 +75,8 @@ impl App {
}
pub fn init(&mut self, user_app: Rc::<RefCell::<dyn UserApp>>) {
//self.tokio_rt.enter();
self.init_http_server();
self.user_app = Some(user_app);
crate::Timer::instance().borrow_mut().init();
self.user_app.as_ref().unwrap().borrow_mut().init();
@ -102,4 +130,13 @@ impl App {
return 0;
}
fn init_http_server(&mut self) {
let (tx, _) = mpsc::channel();
thread::spawn(move || {
let server_future = run_app(tx);
rt::System::new().block_on(server_future)
});
}
}