Re-implementing the API

This commit is contained in:
2024-12-19 13:50:31 -05:00
parent 9344979d72
commit 4a18af9014
17 changed files with 486 additions and 152 deletions

29
src/api/app.rs Normal file
View File

@@ -0,0 +1,29 @@
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?)
}
}