Update
This commit is contained in:
parent
964534d0d9
commit
e262fc8ab2
6 changed files with 213 additions and 139 deletions
|
@ -8,19 +8,18 @@ use crate::{cache, db};
|
|||
|
||||
pub mod templates;
|
||||
|
||||
use templates::TemplateToResponseWithStatusCode;
|
||||
|
||||
pub mod static_dir {
|
||||
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
|
||||
}
|
||||
|
||||
async fn not_found() -> HttpResponse {
|
||||
let mut resp = templates::StatusCode {
|
||||
templates::StatusCode {
|
||||
status_code: http::StatusCode::NOT_FOUND,
|
||||
message: Some("maybe try a correct url?".to_string()),
|
||||
}
|
||||
.to_response();
|
||||
*resp.status_mut() = http::StatusCode::NOT_FOUND;
|
||||
|
||||
resp
|
||||
.to_response_with_status_code(http::StatusCode::NOT_FOUND)
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
|
@ -89,7 +88,7 @@ async fn post_by_slug(
|
|||
|
||||
if let Some(post_id) = match db::schema::posts::table
|
||||
.select(db::schema::posts::id)
|
||||
.filter(db::schema::posts::slug.eq(&slug))
|
||||
.filter(db::schema::posts::slug.eq(slug))
|
||||
.filter(db::schema::posts::active)
|
||||
.first::<i32>(db_conn)
|
||||
.optional()
|
||||
|
@ -104,125 +103,34 @@ async fn post_by_slug(
|
|||
|
||||
templates::PostBySlug { post }.to_response()
|
||||
} else {
|
||||
let mut resp = templates::StatusCode {
|
||||
templates::StatusCode {
|
||||
status_code: http::StatusCode::NOT_FOUND,
|
||||
message: Some("this post does not exists... yet".to_string()),
|
||||
}
|
||||
.to_response();
|
||||
*resp.status_mut() = http::StatusCode::NOT_FOUND;
|
||||
|
||||
return resp;
|
||||
.to_response_with_status_code(http::StatusCode::NOT_FOUND)
|
||||
}
|
||||
}
|
||||
|
||||
// let post_stripped: Option<(i32, String, NaiveDate, Option<NaiveDate>)> =
|
||||
// match db::schema::posts::table
|
||||
// .select((
|
||||
// db::schema::posts::id,
|
||||
// db::schema::posts::name,
|
||||
// db::schema::posts::published_at,
|
||||
// db::schema::posts::edited_at,
|
||||
// ))
|
||||
// .filter(db::schema::posts::slug.eq(&slug))
|
||||
// .filter(db::schema::posts::active)
|
||||
// .first::<(i32, String, NaiveDate, Option<NaiveDate>)>(db_conn)
|
||||
// .optional()
|
||||
// {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
// };
|
||||
//
|
||||
// match post_stripped {
|
||||
// Some(stripped) => {
|
||||
// let (stripped_id, stripped_name, stripped_published_at, stripped_edited_at) = stripped;
|
||||
#[get("/tags")]
|
||||
async fn tags(
|
||||
db_pool: web::Data<db::DbPool>,
|
||||
redis_pool: web::Data<cache::RedisPool>,
|
||||
) -> HttpResponse {
|
||||
let db_conn = &mut match db_pool.get() {
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
let redis_conn = &mut match redis_pool.get() {
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
|
||||
// let key = cache::keys::post_content(stripped_id);
|
||||
let x = match cache::cache_tags(db_conn, redis_conn) {
|
||||
Ok(x) => x,
|
||||
Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
};
|
||||
|
||||
// match match redis::cmd("GET")
|
||||
// .arg(&key)
|
||||
// .query::<Option<String>>(redis_conn.deref_mut())
|
||||
// {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => return HttpResponse::InternalServerError().body(format!("{:?}", e)),
|
||||
// } {
|
||||
// Some(s) => {
|
||||
// let tags = match get_tags_by_post(stripped_id, db_conn) {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => {
|
||||
// return HttpResponse::InternalServerError().body(format!("{:?}", e))
|
||||
// }
|
||||
// };
|
||||
|
||||
// templates::PostBySlug {
|
||||
// name: stripped_name,
|
||||
// slug,
|
||||
// published_at: stripped_published_at,
|
||||
// edited_at: stripped_edited_at,
|
||||
// tags,
|
||||
// content: s,
|
||||
// }
|
||||
// }
|
||||
// .to_response(),
|
||||
// None => {
|
||||
// let post = match db::schema::posts::table
|
||||
// .filter(db::schema::posts::id.eq(stripped_id))
|
||||
// .first::<db::models::Post>(db_conn)
|
||||
// {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => {
|
||||
// return HttpResponse::InternalServerError().body(format!("{:?}", e))
|
||||
// }
|
||||
// };
|
||||
|
||||
// let html = markdown::to_html(&post.content);
|
||||
|
||||
// match redis::cmd("SET")
|
||||
// .arg(&key)
|
||||
// .arg(&html)
|
||||
// .query::<Option<String>>(redis_conn.deref_mut())
|
||||
// {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => {
|
||||
// return HttpResponse::InternalServerError().body(format!("{:?}", e))
|
||||
// }
|
||||
// };
|
||||
// if let Err(e) = redis::cmd("EXPIRE")
|
||||
// .arg(key)
|
||||
// .arg(config::CONFIG.cache_ttl)
|
||||
// .query::<()>(redis_conn.deref_mut())
|
||||
// {
|
||||
// return HttpResponse::InternalServerError().body(format!("{:?}", e));
|
||||
// }
|
||||
|
||||
// let tags = match get_tags_by_post(stripped_id, db_conn) {
|
||||
// Ok(x) => x,
|
||||
// Err(e) => {
|
||||
// return HttpResponse::InternalServerError().body(format!("{:?}", e))
|
||||
// }
|
||||
// };
|
||||
|
||||
// templates::PostBySlug {
|
||||
// name: post.name,
|
||||
// slug: post.slug,
|
||||
// published_at: stripped_published_at,
|
||||
// edited_at: stripped_edited_at,
|
||||
// tags,
|
||||
// content: html,
|
||||
// }
|
||||
// .to_response()
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// None => {
|
||||
// let mut resp = templates::StatusCode {
|
||||
// status_code: http::StatusCode::NOT_FOUND,
|
||||
// message: Some("this post does not exists... yet".to_string()),
|
||||
// }
|
||||
// .to_response();
|
||||
// *resp.status_mut() = http::StatusCode::NOT_FOUND;
|
||||
|
||||
// resp
|
||||
// }
|
||||
// }
|
||||
templates::Tags { tags: x }.to_response()
|
||||
}
|
||||
|
||||
#[get("/tags/{name}")]
|
||||
|
@ -272,6 +180,7 @@ fn setup_routes(cfg: &mut web::ServiceConfig) {
|
|||
.service(posts)
|
||||
.service(ResourceFiles::new("/static", generated))
|
||||
.service(post_by_slug)
|
||||
.service(tags)
|
||||
.service(tag_by_name)
|
||||
.default_service(web::route().to(not_found));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue