Re-implementing the API
This commit is contained in:
29
src/api/app.rs
Normal file
29
src/api/app.rs
Normal 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?)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user