feat: add a function to init properly app library
This commit is contained in:
@ -7,8 +7,11 @@ edition = "2021"
|
||||
anyhow = "1.0.89"
|
||||
axum = "0.7.6"
|
||||
listenfd = "1.0.1"
|
||||
thiserror.workspace = true
|
||||
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
|
||||
|
||||
utils = { path = "../utils" }
|
||||
|
||||
[dev-dependencies]
|
||||
cargo-watch = "8.5.2"
|
||||
systemfd = "0.4.3"
|
||||
|
@ -2,6 +2,20 @@ use anyhow::Error as AnyError;
|
||||
use axum::http::{StatusCode, Uri};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::{routing::get, Router};
|
||||
use thiserror::Error;
|
||||
|
||||
use ::utils::config::{load_config, ConfigError};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum InitError {
|
||||
#[error(transparent)]
|
||||
ConfigError(#[from] ConfigError),
|
||||
}
|
||||
|
||||
pub fn init() -> Result<(), InitError> {
|
||||
load_config(None)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_router() -> Router {
|
||||
Router::new()
|
||||
|
@ -1,24 +1,38 @@
|
||||
use listenfd::ListenFd;
|
||||
use thiserror::Error;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
use backend::get_router;
|
||||
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),
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
async fn main() -> Result<(), BackendError> {
|
||||
init()?;
|
||||
|
||||
let app = get_router();
|
||||
|
||||
let mut listenfd = ListenFd::from_env();
|
||||
|
||||
let listener = match listenfd.take_tcp_listener(0).unwrap() {
|
||||
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).unwrap();
|
||||
TcpListener::from_std(listener).unwrap()
|
||||
listener.set_nonblocking(true)?;
|
||||
TcpListener::from_std(listener)?
|
||||
}
|
||||
// otherwise fall back to local listening
|
||||
None => TcpListener::bind("0.0.0.0:8080").await.unwrap(),
|
||||
None => TcpListener::bind("0.0.0.0:8080").await?,
|
||||
};
|
||||
|
||||
println!("Listening on {}", listener.local_addr().unwrap());
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
let local_addr = listener.local_addr()?;
|
||||
println!("Listening on {}", local_addr);
|
||||
axum::serve(listener, app).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user