Skip to content

Commit

Permalink
extracted app to module
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbchron committed Jan 29, 2024
1 parent 37cf9c9 commit 27e6357
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
23 changes: 23 additions & 0 deletions engine/crates/engine/src/app.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
16 changes: 4 additions & 12 deletions engine/crates/engine/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
use axum::{routing::get, Router};
pub mod app;

use color_eyre::eyre::Result;
use tracing::info;

#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install()?;
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(())
}

0 comments on commit 27e6357

Please sign in to comment.