1
0
Fork 0
website/src/main.rs

159 lines
4 KiB
Rust
Raw Normal View History

2023-07-29 12:04:37 +02:00
#![warn(clippy::pedantic)]
#![allow(clippy::unused_async)] // axum handlers needs async, even if no awaiting happens
2023-04-10 00:46:58 +02:00
use std::{collections::HashMap, fmt::Display, sync::Arc, time::Duration};
2022-09-05 00:05:24 +02:00
use axum::{
body::Body,
extract::{MatchedPath, OriginalUri},
http::{uri::PathAndQuery, Request, Uri},
response::Response,
};
2023-07-29 11:51:04 +02:00
use chrono::DateTime;
use color_eyre::eyre::{Error, Result};
2023-03-29 18:20:51 +02:00
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;
2023-04-02 15:26:20 +02:00
use tower_http::{compression::CompressionLayer, cors::CorsLayer};
2023-07-29 12:04:37 +02:00
use tracing::{field::Empty, info_span, log::info, Span};
2023-04-02 15:26:20 +02:00
use tracing_subscriber::{prelude::*, EnvFilter};
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;
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<()> {
color_eyre::install()?;
2023-04-02 15:26:20 +02:00
init_tracing();
2022-06-16 23:44:37 +02:00
info!("Starting server...");
let base_url: Uri = option_env!("SITE_BASE_URL")
2023-07-29 11:51:04 +02:00
.unwrap_or("http://localhost:8080")
.parse()
.unwrap();
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()
};
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-03-29 18:19:39 +02:00
let app = 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()
.make_span_with(make_span)
.on_response(on_response),
)
2023-04-10 00:46:58 +02:00
.with_state(state.clone());
2022-09-05 00:02:58 +02:00
2023-04-10 00:46:58 +02:00
info!("Now listening at {}", state.base_url);
2023-03-25 16:14:53 +01:00
2023-07-29 11:51:04 +02:00
axum::Server::bind(&"0.0.0.0:8080".parse().unwrap())
2023-03-25 16:14:53 +01:00
.serve(app.into_make_service())
.await?;
Ok(())
}
2023-04-02 15:26:20 +02:00
fn init_tracing() {
let filter = EnvFilter::builder()
.with_default_directive("into".parse().unwrap())
2023-04-03 23:33:25 +02:00
.from_env_lossy();
2023-04-02 15:26:20 +02:00
tracing_subscriber::registry()
.with(filter)
.with(tracing_subscriber::fmt::layer())
.init();
}
fn make_span(request: &Request<Body>) -> Span {
let uri = if let Some(OriginalUri(uri)) = request.extensions().get::<OriginalUri>() {
uri
} else {
request.uri()
};
2023-04-02 15:26:20 +02:00
let route = request
.extensions()
.get::<MatchedPath>()
.map_or(uri.path(), axum::extract::MatchedPath::as_str);
2023-04-02 15:26:20 +02:00
let method = request.method().as_str();
2023-07-29 12:12:46 +02:00
let target = uri
.path_and_query()
.map_or(uri.path(), PathAndQuery::as_str);
2023-04-02 15:26:20 +02:00
let name = format!("{method} {route}");
info_span!(
"request",
otel.name = %name,
http.route = %route,
http.method = %method,
http.target = %target,
http.status_code = Empty
)
}
fn on_response(response: &Response, _latency: Duration, span: &Span) {
2023-04-02 15:26:20 +02:00
span.record("http.status_code", response.status().as_str());
}
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
impl From<color_eyre::Report> for WebsiteError {
fn from(value: color_eyre::Report) -> Self {
WebsiteError::InternalError(value)
}
}