This commit is contained in:
azw 2023-11-10 14:01:10 +00:00
parent 58b4b8e909
commit e22a18c364

View File

@ -5,7 +5,7 @@ use std::time::Duration;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use actix_web::{dev::ServerHandle, middleware, rt, web, HttpRequest, HttpServer}; use actix_web::{dev::ServerHandle, middleware, rt, web, HttpRequest, HttpServer};
use actix_web::App as WebApp; use actix_web::App as WebApp;
use std::{sync::mpsc, thread, time}; use std::{sync::mpsc, thread};
use r9_macro::SharedFromSelf; use r9_macro::SharedFromSelf;
use r9_macro_derive::SharedFromSelf; use r9_macro_derive::SharedFromSelf;
@ -15,6 +15,7 @@ pub trait UserApp {
fn init(&mut self); fn init(&mut self);
fn update(&mut self); fn update(&mut self);
fn uninit(&mut self); fn uninit(&mut self);
fn get_http_listen_port(&self) -> i32;
} }
#[derive(SharedFromSelf)] #[derive(SharedFromSelf)]
@ -27,17 +28,12 @@ pub struct App {
user_app: Option<Rc::<RefCell::<dyn UserApp>>>, user_app: Option<Rc::<RefCell::<dyn UserApp>>>,
} }
async fn index(req: HttpRequest) -> &'static str { async fn run_app(port: u16, tx: mpsc::Sender<ServerHandle>) -> std::io::Result<()> {
"Hello world!asaaa"
}
async fn run_app(tx: mpsc::Sender<ServerHandle>) -> std::io::Result<()> {
let server = HttpServer::new(|| { let server = HttpServer::new(|| {
WebApp::new() WebApp::new()
.service(web::resource("/index.html").to(|| async { "Hello world!" })) .service(web::resource("/index.html").to(|| async { "Hello world!" }))
.service(web::resource("/").to(index))
}) })
.bind(("127.0.0.1", 8080))? .bind(("0.0.0.0", port))?
.workers(2) .workers(2)
.run(); .run();
@ -133,8 +129,9 @@ impl App {
fn init_http_server(&mut self) { fn init_http_server(&mut self) {
let (tx, _) = mpsc::channel(); let (tx, _) = mpsc::channel();
let port = self.user_app.as_ref().unwrap().borrow().get_http_listen_port() as u16;
thread::spawn(move || { thread::spawn(move || {
let server_future = run_app(tx); let server_future = run_app(port, tx);
rt::System::new().block_on(server_future) rt::System::new().block_on(server_future)
}); });
} }