stuff happened
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
pub mod authors;
|
||||
pub mod comments;
|
||||
pub mod posts;
|
||||
pub mod projects;
|
||||
pub mod root;
|
||||
|
@@ -1,7 +1,11 @@
|
||||
use crate::{
|
||||
datasources::posts::PostsDatasource,
|
||||
state::AppState,
|
||||
utils::{datetime::*, rss},
|
||||
utils::{
|
||||
datetime::*,
|
||||
rss,
|
||||
sitemap::{self, SitemapEntry},
|
||||
},
|
||||
};
|
||||
use axum::http::{HeaderMap, HeaderValue};
|
||||
use axum::{
|
||||
@@ -60,6 +64,7 @@ impl PostsRoute {
|
||||
.route("/hot", get(PostsRoute::get_hot_posts))
|
||||
.route("/featured", get(PostsRoute::get_featured_posts))
|
||||
.route("/rss", get(PostsRoute::get_rss_posts))
|
||||
.route("/sitemap", get(PostsRoute::get_sitemap))
|
||||
.with_state(app_state.clone())
|
||||
}
|
||||
|
||||
@@ -330,7 +335,8 @@ impl PostsRoute {
|
||||
|
||||
match PostsDatasource::get_all(&state.database).await {
|
||||
Ok(posts) => {
|
||||
let web_url = std::env::var("BASE_URI_WEB").expect("No environment variable found");
|
||||
let web_url =
|
||||
std::env::var("BASE_URI_WEB").expect("Environment BASE_URI_WEB variable found");
|
||||
let mapped_posts: HashMap<String, Post> = posts
|
||||
.into_iter()
|
||||
.map(|post| (post.post_id.to_string(), post))
|
||||
@@ -343,9 +349,42 @@ impl PostsRoute {
|
||||
);
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
header::CONTENT_DISPOSITION,
|
||||
HeaderValue::from_str(r#"attachment; filename="posts.xml""#).unwrap(),
|
||||
header::CONTENT_TYPE,
|
||||
HeaderValue::from_static("application/xml"),
|
||||
);
|
||||
(headers, xml)
|
||||
}
|
||||
Err(e) => {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("Content-Type", HeaderValue::from_static("text/plain"));
|
||||
(headers, e.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_sitemap(State(app_state): State<AppState>) -> impl IntoResponse {
|
||||
let state = app_state.lock().await;
|
||||
// let cached: Option<Vec<Post>> = None; // TODO: maybe implement cache, later??
|
||||
|
||||
match PostsDatasource::get_all(&state.database).await {
|
||||
Ok(posts) => {
|
||||
let web_url =
|
||||
std::env::var("BASE_URI_WEB").expect("Environment BASE_URI_WEB variable found");
|
||||
let mut entries: HashMap<String, SitemapEntry> = posts
|
||||
.into_iter()
|
||||
.map(|p| {
|
||||
(
|
||||
p.post_id.to_string(),
|
||||
SitemapEntry {
|
||||
location: format!("{}/posts/{}", web_url, p.post_id.to_string()),
|
||||
lastmod: p.created_at.unwrap_or_else(|| chrono::Utc::now()),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
sitemap::get_static_pages(&mut entries, &web_url);
|
||||
let xml: String = sitemap::generate_sitemap(&entries);
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
header::CONTENT_TYPE,
|
||||
HeaderValue::from_static("application/xml"),
|
||||
|
69
backend/public/src/routes/projects.rs
Normal file
69
backend/public/src/routes/projects.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use crate::{datasources::projects::ProjectsDatasource, state::AppState, utils::datetime::*};
|
||||
use axum::http::{HeaderMap, HeaderValue};
|
||||
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Json, Router};
|
||||
use fred::types::Expiration;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
|
||||
pub struct Project {
|
||||
pub project_id: i32,
|
||||
pub title: String,
|
||||
pub repo: Option<String>,
|
||||
pub summary: String,
|
||||
pub tech: String,
|
||||
pub wip: Option<bool>,
|
||||
#[serde(serialize_with = "serialize_datetime")]
|
||||
#[serde(deserialize_with = "deserialize_datetime")]
|
||||
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||
}
|
||||
|
||||
pub struct ProjectsRoute;
|
||||
impl ProjectsRoute {
|
||||
pub fn routes(app_state: &AppState) -> Router {
|
||||
Router::new()
|
||||
.route("/", get(ProjectsRoute::get_all))
|
||||
.with_state(app_state.clone())
|
||||
}
|
||||
|
||||
async fn get_all(State(app_state): State<AppState>) -> impl IntoResponse {
|
||||
let mut state = app_state.lock().await;
|
||||
let cached: Option<Vec<Project>> = state
|
||||
.cache
|
||||
.get(String::from("projects:all"))
|
||||
.await
|
||||
.unwrap_or(None);
|
||||
|
||||
if let Some(projects) = cached {
|
||||
tracing::info!("grabbing all projects from cache");
|
||||
return Ok(Json(projects));
|
||||
};
|
||||
|
||||
match ProjectsDatasource::get_all(&state.database).await {
|
||||
Ok(projects) => {
|
||||
tracing::info!("grabbing all projects from database");
|
||||
if let p = &projects {
|
||||
let projects = p.clone();
|
||||
let state = app_state.clone();
|
||||
|
||||
tracing::info!("storing database data in cache");
|
||||
tokio::spawn(async move {
|
||||
let mut s = state.lock().await;
|
||||
let _ = s
|
||||
.cache
|
||||
.set(
|
||||
String::from("projects:all"),
|
||||
&projects,
|
||||
Some(Expiration::EX(10)),
|
||||
None,
|
||||
false,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
};
|
||||
|
||||
Ok(Json(projects))
|
||||
}
|
||||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,10 +1,15 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
|
||||
use crate::{datasources::posts::PostsDatasource, state::AppState};
|
||||
|
||||
use super::posts::Post;
|
||||
|
||||
pub struct RootRoute;
|
||||
impl RootRoute {
|
||||
pub fn routes() -> Router {
|
||||
|
Reference in New Issue
Block a user