web_pylon.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
mod configuration; mod content; mod email; mod errors; mod forms; mod handlers; mod state; mod templates; use crate::errors::{ Error, Result }; use crate::state::{ ApplicationState }; use axum::{ Router, serve }; use axum::routing::{ get, post }; use tera::{ Tera }; use tokio::net::{ TcpListener }; use tower_http::services::{ ServeDir }; use tower_http::trace::{ TraceLayer }; use std::process::{ ExitCode }; use std::sync::{ Arc }; #[tokio::main] async fn main() -> ExitCode { let result = run_server().await; match result { Ok(_) => ExitCode::SUCCESS, Err(e) => { tracing::error!("{}", e); ExitCode::FAILURE } } } async fn run_server() -> Result<()> { let configuration = configuration::configure()?; let templates_root = { let path = configuration.content_root.join("templates/**/*.tera.*"); match path.into_os_string().into_string() { Ok(s) => s, Err(_) => return Err(Error::PathContainsInvalidUtf8(configuration.content_root)), } }; let state = ApplicationState { email_client: email::create_client(&configuration), content: content::Provider::new(&configuration.content_root, &configuration.content_metadata_file)?, tera: Tera::new(&templates_root)?, }; let router = Router::new() .layer(TraceLayer::new_for_http()) .route("/", get(handlers::index_handler)) .route("/atom.xml", get(handlers::index_atom_feed_handler)) .route("/form/contact", post(handlers::contact_form_handler)) .route("/list/:name", get(handlers::list_handler)) .route("/list/:name/atom.xml", get(handlers::list_atom_feed_handler)) .route("/page/:name", get(handlers::page_handler)) .route("/post/:name", get(handlers::post_handler)) .nest_service("/static", ServeDir::new(configuration.content_root.join("static"))) .fallback(handlers::not_found_handler) .with_state(Arc::new(state)); let address = format!("{}:{}", configuration.network_host, configuration.network_port); let listener = TcpListener::bind(address).await?; tracing::info!("web-pylon v{} (hash {}) now listening on {}", env!("CARGO_PKG_VERSION"), env!("GIT_HASH"), listener.local_addr()?); serve(listener, router).await?; Ok(()) }