2022-10-28 22:41:49 +00:00
|
|
|
mod captcha_solutions;
|
|
|
|
mod config;
|
2022-11-01 19:45:06 +00:00
|
|
|
mod errors;
|
2022-10-28 22:41:49 +00:00
|
|
|
mod forms;
|
2022-11-01 23:24:17 +00:00
|
|
|
mod logging;
|
2022-10-28 22:41:49 +00:00
|
|
|
mod mailer;
|
2022-10-26 00:23:55 +00:00
|
|
|
mod routes;
|
2022-12-03 16:08:23 +00:00
|
|
|
mod states;
|
2022-11-01 19:45:06 +00:00
|
|
|
mod templates;
|
2022-10-26 00:23:55 +00:00
|
|
|
|
2022-11-01 19:45:06 +00:00
|
|
|
use anyhow::Result;
|
2023-02-22 23:42:08 +00:00
|
|
|
use axum::{
|
|
|
|
http::StatusCode,
|
|
|
|
routing::{get, get_service, Router},
|
|
|
|
Server,
|
|
|
|
};
|
2023-02-23 02:22:31 +00:00
|
|
|
use std::{future::ready, net::SocketAddr, process, sync::Arc};
|
2023-02-22 23:42:08 +00:00
|
|
|
use tower_http::services::ServeDir;
|
2022-12-03 16:08:23 +00:00
|
|
|
use tracing::info;
|
2022-10-31 01:13:42 +00:00
|
|
|
|
2022-12-03 16:15:49 +00:00
|
|
|
async fn init() -> Result<()> {
|
2022-10-29 17:09:56 +00:00
|
|
|
let mut config = config::Config::new()?;
|
2022-10-31 01:13:42 +00:00
|
|
|
let path_prefix = config.path_prefix.clone();
|
|
|
|
let mailer = Arc::new(mailer::Mailer::new(&mut config)?);
|
2022-11-01 23:24:17 +00:00
|
|
|
|
2023-02-23 02:22:31 +00:00
|
|
|
let socket_address = config
|
|
|
|
.socket_address
|
|
|
|
.parse::<SocketAddr>()
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
format!("Failed to parse the socket address: {e:?}");
|
|
|
|
process::exit(1);
|
|
|
|
});
|
2022-11-01 23:24:17 +00:00
|
|
|
|
2023-02-23 02:22:31 +00:00
|
|
|
logging::init_logger(
|
|
|
|
&config.log_file,
|
|
|
|
config.utc_offset.hours,
|
|
|
|
config.utc_offset.minutes,
|
|
|
|
)?;
|
2022-11-01 23:24:17 +00:00
|
|
|
|
2022-10-31 01:13:42 +00:00
|
|
|
let config = Arc::new(config);
|
2022-12-03 16:08:23 +00:00
|
|
|
let captcha_solutions = Arc::new(captcha_solutions::SharedCaptchaSolutions::default());
|
|
|
|
|
|
|
|
let app_state = states::AppState {
|
|
|
|
config,
|
|
|
|
mailer,
|
|
|
|
captcha_solutions,
|
|
|
|
};
|
2022-10-31 01:13:42 +00:00
|
|
|
|
2023-02-22 23:42:08 +00:00
|
|
|
let static_service =
|
|
|
|
get_service(ServeDir::new("static")).handle_error(|_| ready(StatusCode::NOT_FOUND));
|
|
|
|
|
2022-10-31 01:13:42 +00:00
|
|
|
let routes = Router::new()
|
2022-12-03 16:50:22 +00:00
|
|
|
.route("/", get(routes::index).post(routes::submit))
|
2023-02-22 23:42:08 +00:00
|
|
|
.with_state(app_state)
|
|
|
|
.nest_service("/static", static_service);
|
2022-12-03 16:15:49 +00:00
|
|
|
|
2023-02-22 23:42:08 +00:00
|
|
|
let app = Router::new().nest(&path_prefix, routes);
|
2022-10-31 01:13:42 +00:00
|
|
|
|
2022-12-03 16:08:23 +00:00
|
|
|
info!("Starting server");
|
2022-11-01 23:24:17 +00:00
|
|
|
Server::bind(&socket_address)
|
2022-10-31 01:13:42 +00:00
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-10-28 22:41:49 +00:00
|
|
|
|
2022-12-03 16:15:49 +00:00
|
|
|
Ok(())
|
2022-10-29 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 23:41:26 +00:00
|
|
|
/// Single thread
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
2022-10-31 01:13:42 +00:00
|
|
|
async fn main() {
|
2023-01-12 19:55:56 +00:00
|
|
|
if let Err(e) = init().await {
|
2022-11-01 23:24:17 +00:00
|
|
|
eprintln!("{e:?}");
|
2022-10-29 17:09:56 +00:00
|
|
|
process::exit(1);
|
2023-01-12 19:55:56 +00:00
|
|
|
};
|
2022-10-26 00:23:55 +00:00
|
|
|
}
|