Krys4lide/crates/backend/src/main.rs

39 lines
1.0 KiB
Rust
Raw Normal View History

use listenfd::ListenFd;
use thiserror::Error;
use tokio::net::TcpListener;
use backend::{get_router, init, InitError};
#[derive(Error, Debug)]
pub enum BackendError {
#[error("Error while setting up or serving the TCP listener")]
ServeTCPListener(#[from] std::io::Error),
#[error("Error while initialising the backend")]
InitError(#[from] InitError),
}
2024-09-23 18:02:07 +02:00
#[tokio::main]
async fn main() -> Result<(), BackendError> {
init()?;
2024-09-23 18:02:07 +02:00
let app = get_router();
let mut listenfd = ListenFd::from_env();
let listener = match listenfd.take_tcp_listener(0)? {
// if we are given a tcp listener on listen fd 0, we use that one
Some(listener) => {
listener.set_nonblocking(true)?;
TcpListener::from_std(listener)?
}
// otherwise fall back to local listening
None => TcpListener::bind("0.0.0.0:8080").await?,
};
2024-09-23 18:02:07 +02:00
let local_addr = listener.local_addr()?;
println!("Listening on {}", local_addr);
axum::serve(listener, app).await?;
Ok(())
2024-09-23 18:02:07 +02:00
}