30 lines
720 B
Rust
30 lines
720 B
Rust
use std::env;
|
|
use std::sync::Arc;
|
|
use axum::Router;
|
|
use tokio::net::TcpListener;
|
|
use crate::{api, AppState};
|
|
use crate::error::SirenResult;
|
|
|
|
pub struct App {
|
|
app_state: AppState,
|
|
}
|
|
|
|
impl App {
|
|
pub fn new(app_state: AppState) -> Self {
|
|
Self { app_state }
|
|
}
|
|
|
|
pub async fn serve(self) -> SirenResult<()> {
|
|
let app = Router::new()
|
|
.nest("/api", api::get_routes())
|
|
.with_state(Arc::new(self.app_state));
|
|
|
|
let api_port: String = env::var("API_PORT").expect("Expected a port in the environment");
|
|
let addr = format!("0.0.0.0:{}", api_port);
|
|
|
|
let listener = TcpListener::bind(&addr).await?;
|
|
log::info!("API is listening on {}", &addr);
|
|
Ok(axum::serve(listener, app).await?)
|
|
}
|
|
}
|