diff --git a/engine/crates/engine/src/app.rs b/engine/crates/engine/src/app.rs new file mode 100644 index 0000000..a15c626 --- /dev/null +++ b/engine/crates/engine/src/app.rs @@ -0,0 +1,23 @@ +use axum::{routing::get, Router}; +use color_eyre::eyre::Result; +use tracing::info; + +pub struct App; + +impl App { + pub fn new() -> Self { Self } + + pub async fn serve(&self) -> Result<()> { + let app = Router::new().route("/", get(|| async { "Hello, World!" })); + + let port = std::env::var("PORT").unwrap_or_else(|_| "3000".to_string()); + let host = std::env::var("HOST").unwrap_or_else(|_| "0.0.0.0".to_string()); + let address = format!("{}:{}", host, port); + let listener = tokio::net::TcpListener::bind(&address).await?; + + info!("listening on {}", address); + axum::serve(listener, app).await?; + + Ok(()) + } +} diff --git a/engine/crates/engine/src/main.rs b/engine/crates/engine/src/main.rs index 09a4b10..529c2c1 100644 --- a/engine/crates/engine/src/main.rs +++ b/engine/crates/engine/src/main.rs @@ -1,6 +1,6 @@ -use axum::{routing::get, Router}; +pub mod app; + use color_eyre::eyre::Result; -use tracing::info; #[tokio::main] async fn main() -> Result<()> { @@ -8,16 +8,8 @@ async fn main() -> Result<()> { let subscriber = tracing_subscriber::fmt().finish(); tracing::subscriber::set_global_default(subscriber)?; - let app = Router::new().route("/", get(|| async { "Hello, World!" })); - - // run our app with hyper, listening globally on port 3000 - let port = std::env::var("PORT").unwrap_or_else(|_| "3000".to_string()); - let host = std::env::var("HOST").unwrap_or_else(|_| "0.0.0.0".to_string()); - let address = format!("{}:{}", host, port); - let listener = tokio::net::TcpListener::bind(&address).await?; - - info!("Listening on {}", address); - axum::serve(listener, app).await?; + let app = app::App::new(); + app.serve().await?; Ok(()) }