2024-09-23 18:13:48 +02:00
|
|
|
use listenfd::ListenFd;
|
2024-09-16 22:54:02 +02:00
|
|
|
use thiserror::Error;
|
2024-09-23 18:13:48 +02:00
|
|
|
use tokio::net::TcpListener;
|
|
|
|
|
2024-09-16 22:54:02 +02:00
|
|
|
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]
|
2024-09-16 22:54:02 +02:00
|
|
|
async fn main() -> Result<(), BackendError> {
|
|
|
|
init()?;
|
|
|
|
|
2024-09-23 18:02:07 +02:00
|
|
|
let app = get_router();
|
2024-09-23 18:13:48 +02:00
|
|
|
|
|
|
|
let mut listenfd = ListenFd::from_env();
|
|
|
|
|
2024-09-16 22:54:02 +02:00
|
|
|
let listener = match listenfd.take_tcp_listener(0)? {
|
2024-09-23 18:13:48 +02:00
|
|
|
// if we are given a tcp listener on listen fd 0, we use that one
|
|
|
|
Some(listener) => {
|
2024-09-16 22:54:02 +02:00
|
|
|
listener.set_nonblocking(true)?;
|
|
|
|
TcpListener::from_std(listener)?
|
2024-09-23 18:13:48 +02:00
|
|
|
}
|
|
|
|
// otherwise fall back to local listening
|
2024-09-16 22:54:02 +02:00
|
|
|
None => TcpListener::bind("0.0.0.0:8080").await?,
|
2024-09-23 18:13:48 +02:00
|
|
|
};
|
2024-09-23 18:02:07 +02:00
|
|
|
|
2024-09-16 22:54:02 +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
|
|
|
}
|