1
0
Fork 0
website/src/main.rs

118 lines
2.9 KiB
Rust
Raw Normal View History

2023-07-29 12:04:37 +02:00
#![warn(clippy::pedantic)]
2024-04-17 19:12:59 +02:00
use std::{collections::HashMap, fmt::Display, sync::Arc};
use axum::http::Uri;
2023-07-29 11:51:04 +02:00
use chrono::DateTime;
2023-03-29 18:20:51 +02:00
2023-07-29 20:22:13 +02:00
use config::Config;
2023-03-25 12:23:11 +01:00
use post::Post;
2023-03-29 18:20:51 +02:00
2023-03-29 21:48:27 +02:00
use tag::Tag;
2022-08-31 23:20:59 +02:00
use tera::Tera;
2024-04-17 19:12:59 +02:00
2023-04-02 15:26:20 +02:00
use tower_http::{compression::CompressionLayer, cors::CorsLayer};
2024-04-17 19:12:59 +02:00
use tracing::{instrument, log::info};
2023-07-29 12:04:37 +02:00
2024-04-17 19:12:59 +02:00
use anyhow::{Error, Result};
2022-06-16 23:44:37 +02:00
2023-04-03 23:33:43 +02:00
mod feed;
2022-08-31 23:20:59 +02:00
mod handlers;
mod helpers;
2023-06-18 11:34:08 +02:00
mod hilighting;
mod markdown;
2024-04-17 19:12:59 +02:00
mod observability;
2023-03-25 12:23:11 +01:00
mod post;
2023-03-29 21:48:27 +02:00
mod tag;
2022-08-31 23:20:59 +02:00
2023-04-10 00:46:58 +02:00
#[derive(Default)]
pub struct AppState {
2023-07-29 11:51:04 +02:00
startup_time: DateTime<chrono::offset::Utc>,
base_url: Uri,
2023-03-25 16:14:53 +01:00
posts: HashMap<String, Post>,
2023-03-29 21:48:27 +02:00
tags: HashMap<String, Tag>,
2022-08-31 23:20:59 +02:00
tera: Tera,
}
2022-06-16 23:44:37 +02:00
#[tokio::main]
async fn main() -> Result<()> {
2023-07-29 20:22:13 +02:00
let cfg = Config::builder()
.add_source(config::File::with_name("config.toml"))
2023-11-10 23:09:00 +01:00
.add_source(config::Environment::with_prefix("WEBSITE"))
2023-07-29 20:22:13 +02:00
.build()?;
2024-04-17 19:12:59 +02:00
observability::init_tracing(&cfg);
2022-06-16 23:44:37 +02:00
info!("Starting server...");
2024-04-17 19:12:59 +02:00
let addr = cfg.get_string("bind_address")?;
2023-11-10 23:09:00 +01:00
let app = init_app(&cfg).await?;
2024-04-17 19:12:59 +02:00
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app.into_make_service()).await?;
2023-11-10 23:09:00 +01:00
Ok(())
}
#[instrument]
async fn init_app(cfg: &Config) -> Result<axum::routing::Router> {
2024-04-17 19:12:59 +02:00
let base_url: Uri = cfg.get_string("base_url")?.parse().unwrap();
2023-07-29 20:22:13 +02:00
let tera = Tera::new("templates/**/*")?;
let mut state = AppState {
2023-07-29 11:51:04 +02:00
startup_time: chrono::offset::Utc::now(),
2023-04-03 23:33:43 +02:00
base_url,
tera,
..Default::default()
};
2023-07-29 20:22:13 +02:00
let posts = post::load_all(&state).await?;
let tags = tag::get_tags(posts.values());
state.posts = posts;
state.tags = tags;
let state = Arc::new(state);
2023-11-10 23:09:00 +01:00
info!("Listening at {}", state.base_url);
Ok(handlers::routes(&state)
2023-03-29 21:48:27 +02:00
.layer(CorsLayer::permissive())
.layer(CompressionLayer::new())
2023-04-02 15:27:06 +02:00
.layer(
tower_http::trace::TraceLayer::new_for_http()
2024-04-17 19:12:59 +02:00
.make_span_with(observability::make_span)
.on_response(observability::on_response),
2023-04-02 15:27:06 +02:00
)
2023-11-10 23:09:00 +01:00
.with_state(state.clone()))
2023-03-25 16:14:53 +01:00
}
2023-03-25 21:38:16 +01:00
#[derive(Debug)]
pub enum WebsiteError {
NotFound,
2023-03-26 12:40:25 +02:00
InternalError(Error),
2023-03-25 21:38:16 +01:00
}
2023-04-10 00:46:58 +02:00
impl std::error::Error for WebsiteError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
WebsiteError::NotFound => None,
WebsiteError::InternalError(e) => Some(e.as_ref()),
}
}
}
impl Display for WebsiteError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
WebsiteError::NotFound => write!(f, "Not found"),
2023-07-29 12:04:37 +02:00
WebsiteError::InternalError(e) => write!(f, "Internal error: {e}"),
2023-04-10 00:46:58 +02:00
}
}
}
impl From<tera::Error> for WebsiteError {
fn from(value: tera::Error) -> Self {
2023-03-25 21:38:16 +01:00
WebsiteError::InternalError(value.into())
}
}
2023-04-10 00:46:58 +02:00
2024-04-17 19:12:59 +02:00
impl From<Error> for WebsiteError {
fn from(value: Error) -> Self {
2023-04-10 00:46:58 +02:00
WebsiteError::InternalError(value)
}
}