45 lines
948 B
Rust
45 lines
948 B
Rust
|
mod user;
|
||
|
|
||
|
use axum::Router;
|
||
|
use tokio::net::TcpListener;
|
||
|
|
||
|
use crate::{db::DbPool, error::AppError, models::Environment};
|
||
|
|
||
|
#[derive(Clone)]
|
||
|
pub struct AppState {
|
||
|
pool: DbPool,
|
||
|
env: Environment,
|
||
|
}
|
||
|
|
||
|
impl AppState {
|
||
|
pub fn new(pool: DbPool, env: Environment) -> Self {
|
||
|
Self { pool, env }
|
||
|
}
|
||
|
|
||
|
pub fn pool(&self) -> &DbPool {
|
||
|
&self.pool
|
||
|
}
|
||
|
|
||
|
pub fn env(&self) -> &Environment {
|
||
|
&self.env
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub async fn start_app(pool: DbPool, env: Environment) -> Result<(), AppError> {
|
||
|
let address = "localhost";
|
||
|
let port = "42069";
|
||
|
let listener = TcpListener::bind(format!("{address}:{port}"))
|
||
|
.await
|
||
|
.unwrap();
|
||
|
|
||
|
let app_state = AppState::new(pool, env);
|
||
|
let app = Router::new().merge(user::requests(app_state.clone()));
|
||
|
|
||
|
axum::serve(listener, app)
|
||
|
.await
|
||
|
.map_err(AppError::app_startup)?;
|
||
|
|
||
|
println!("Application started successfully.");
|
||
|
Ok(())
|
||
|
}
|