contact-form/src/main.rs

66 lines
1.5 KiB
Rust
Raw Normal View History

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;
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
2023-02-23 14:37:18 +00:00
use anyhow::{Context, Result};
2023-02-22 23:42:08 +00:00
use axum::{
http::StatusCode,
routing::{get, get_service, Router},
Server,
};
use std::{future::ready, net::SocketAddr, process};
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
use crate::states::AppState;
2022-12-03 16:15:49 +00:00
async fn init() -> Result<()> {
let app_state = AppState::build()?;
let path_prefix = app_state.config.path_prefix.clone();
let socket_address = app_state
.config
2023-02-23 02:22:31 +00:00
.socket_address
.parse::<SocketAddr>()
2023-02-23 14:37:18 +00:00
.context("Failed to parse the socket address: {e:?}")?;
2023-02-23 02:22:31 +00:00
logging::init_logger(
&app_state.config.log_file,
app_state.config.utc_offset.hours,
app_state.config.utc_offset.minutes,
2023-02-23 02:22:31 +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");
Server::bind(&socket_address)
2022-10-31 01:13:42 +00:00
.serve(app.into_make_service())
2023-02-23 14:37:18 +00:00
.await?;
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
}
/// Single thread.
2023-02-22 23:41:26 +00:00
#[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 {
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
}