diff --git a/f9/Cargo.toml b/f9/Cargo.toml index da24370..f38df25 100644 --- a/f9/Cargo.toml +++ b/f9/Cargo.toml @@ -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" } diff --git a/f9/src/app.rs b/f9/src/app.rs index 3e29e12..8bf41b5 100644 --- a/f9/src/app.rs +++ b/f9/src/app.rs @@ -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>>, } +async fn index(req: HttpRequest) -> &'static str { + "Hello world!asaaa" +} + +async fn run_app(tx: mpsc::Sender) -> 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::> { @@ -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::>) { + //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) + }); + } + }