From e0ad3f29aedd01a0166134c92ae6df075ec06b7a Mon Sep 17 00:00:00 2001 From: Michal Vanko Date: Thu, 18 Jan 2024 20:53:39 +0100 Subject: [PATCH] environment variables for port and filtering posts --- axum_server/src/main.rs | 6 ++++-- axum_server/src/pages/post_list.rs | 12 +++++++++++- axum_server/templates/post_list.html | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/axum_server/src/main.rs b/axum_server/src/main.rs index 926c570..dcb3547 100644 --- a/axum_server/src/main.rs +++ b/axum_server/src/main.rs @@ -1,3 +1,4 @@ +use askama::filters::format; use axum; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; @@ -22,9 +23,10 @@ async fn main() { // build our application with a single route let app = router::get_router(); // run our app with hyper, listening globally on port 3080 - let listener = tokio::net::TcpListener::bind("0.0.0.0:3080").await.unwrap(); + let port = std::option_env!("PORT").unwrap_or("3080"); + let addr = format!("0.0.0.0:{}", port); + let listener = tokio::net::TcpListener::bind(addr).await.unwrap(); axum::serve(listener, app).await.unwrap(); } -// TODO Port from env variable // TODO displaying Image from netlify CDN diff --git a/axum_server/src/pages/post_list.rs b/axum_server/src/pages/post_list.rs index b6d646b..8d27288 100644 --- a/axum_server/src/pages/post_list.rs +++ b/axum_server/src/pages/post_list.rs @@ -37,7 +37,7 @@ pub async fn render_post_list(tag: Option>) -> Result posts .into_iter() .filter(|post| { @@ -52,6 +52,16 @@ pub async fn render_post_list(tag: Option>) -> Result posts, }; + if std::env::var("TARGET") + .unwrap_or_else(|_| "DEV".to_owned()) + .eq("PROD") + { + posts = posts + .into_iter() + .filter(|post| !post.slug.starts_with("dev")) + .collect() + } + Ok(PostListTemplate { title: "Posts".to_owned(), posts, diff --git a/axum_server/templates/post_list.html b/axum_server/templates/post_list.html index 1d2e2d4..56d5480 100644 --- a/axum_server/templates/post_list.html +++ b/axum_server/templates/post_list.html @@ -19,4 +19,4 @@ {% endfor %} -{# #} {% endblock %} +{% endblock %}