1
0
Fork 0
website/src/main.rs

62 lines
1.3 KiB
Rust
Raw Normal View History

2023-03-25 16:14:53 +01:00
use std::{collections::HashMap, sync::Arc};
2022-09-05 00:05:24 +02:00
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-03-29 21:48:27 +02:00
use tower_http::{compression::CompressionLayer, trace::TraceLayer, cors::CorsLayer};
use tracing::log::*;
2022-06-16 23:44:37 +02:00
2022-08-31 23:20:59 +02:00
mod handlers;
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
pub struct AppState {
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()?;
tracing_subscriber::fmt::init();
info!("Starting server...");
let tera = Tera::new("templates/**/*")?;
let posts = post::load_all().await?;
2023-03-29 21:48:27 +02:00
let tags = tag::get_tags(posts.values());
let state = Arc::new(AppState { tera, posts, tags });
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(TraceLayer::new_for_http())
.layer(CompressionLayer::new())
.with_state(state);
2022-09-05 00:02:58 +02:00
2023-03-25 16:14:53 +01:00
info!("Now listening at http://localhost:8180");
axum::Server::bind(&"0.0.0.0:8180".parse().unwrap())
.serve(app.into_make_service())
.await?;
Ok(())
}
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
}
impl<E> From<E> for WebsiteError
where
2023-03-26 12:40:25 +02:00
E: Into<Error>,
2023-03-25 21:38:16 +01:00
{
fn from(value: E) -> Self {
WebsiteError::InternalError(value.into())
}
}