contact-form/src/main.rs

68 lines
1.6 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
2022-11-01 19:45:06 +00:00
use anyhow::Result;
2022-12-03 16:50:22 +00:00
use axum::routing::get;
2022-10-31 01:13:42 +00:00
use axum::{Router, Server};
2022-11-02 19:50:23 +00:00
use axum_extra::routing::SpaRouter;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2022-10-29 17:09:56 +00:00
use std::process;
2022-10-31 01:13:42 +00:00
use std::sync::Arc;
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)?);
let address = config.socket_address.address;
let socket_address = SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(
address[0], address[1], address[2], address[3],
)),
config.socket_address.port,
);
2022-12-03 16:15:49 +00:00
let _tracing_gurad = logging::init_logger(&config.logging);
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
let routes = Router::new()
2022-12-03 16:50:22 +00:00
.route("/", get(routes::index).post(routes::submit))
2022-12-03 16:08:23 +00:00
.with_state(app_state);
2022-11-02 19:50:23 +00:00
2022-12-03 16:15:49 +00:00
let spa = SpaRouter::new(&format!("{}/static", &path_prefix), "static");
2022-12-03 16:08:23 +00:00
let app = Router::new().nest(&path_prefix, routes).merge(spa);
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())
.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
}
2022-10-31 01:13:42 +00:00
#[tokio::main]
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
}