mod captcha_solutions; mod config; mod errors; mod forms; mod logging; mod mailer; mod routes; mod states; mod templates; use anyhow::{Context, Result}; use axum::{ http::StatusCode, routing::{get, get_service, Router}, Server, }; use std::{future::ready, net::SocketAddr, process}; use tower_http::services::ServeDir; use tracing::{error, info}; use crate::{config::Config, states::AppState}; async fn init(logger_initialized: &mut bool) -> Result<()> { let config = Config::build()?; logging::init_logger( &config.log_file, config.utc_offset.hours, config.utc_offset.minutes, )?; *logger_initialized = true; // The path prefix of all routes. let path_prefix = config.state_config.path_prefix.clone(); let socket_address = config .socket_address .parse::() .context("Failed to parse the socket address: {e:?}")?; let app_state = AppState::build(config).await?; // The service for serving the static files. let static_service = get_service(ServeDir::new("static")).handle_error(|_| ready(StatusCode::NOT_FOUND)); let routes = Router::new() .route("/", get(routes::index).post(routes::submit)) .with_state(app_state) .nest_service("/static", static_service); let app = Router::new().nest(&path_prefix, routes); info!("Starting server"); Server::bind(&socket_address) .serve(app.into_make_service()) .await?; Ok(()) } /// Single thread. #[tokio::main(flavor = "current_thread")] async fn main() { let mut logger_initialized = false; if let Err(e) = init(&mut logger_initialized).await { if logger_initialized { error!("{e:?}"); } else { eprintln!("{e:?}"); } process::exit(1); }; }