From b407273f6f8b11dad8d3b329ed960716f847e432 Mon Sep 17 00:00:00 2001 From: Bauke Date: Sun, 9 Oct 2022 23:34:39 +0200 Subject: [PATCH] Add a configurable base URL. --- .env.example | 3 +++ source/snapshots/create.rs | 8 ++++---- source/templates/index.html | 4 ++-- source/templates/mod.rs | 9 ++++++++- source/utilities.rs | 6 ++++++ 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index 1cc7ed8..d269006 100644 --- a/.env.example +++ b/.env.example @@ -3,3 +3,6 @@ # The User-Agent string for HTTP requests, replace the email with yours. # USER_AGENT="Tildes Statistics (your-email@example.org)" + +# A different base URL than the default "https://tildes.net". +# BASE_URL="https://example.org" diff --git a/source/snapshots/create.rs b/source/snapshots/create.rs index 01734de..6873b27 100644 --- a/source/snapshots/create.rs +++ b/source/snapshots/create.rs @@ -10,12 +10,13 @@ use { use crate::{ group_data::{GroupDataActiveModel, GroupDataEntity}, snapshots::{SnapshotActiveModel, SnapshotModel}, - utilities::{create_http_client, download_html, today}, + utilities::{create_http_client, download_html, get_base_url, today}, }; impl SnapshotModel { /// Create a snapshot for today. pub async fn create(db: &DatabaseConnection, force: bool) -> Result<()> { + let base_url = get_base_url(); let snapshot_date = today(); match (force, Self::get_by_date(db, snapshot_date).await?) { (true, Some(existing)) => { @@ -44,7 +45,7 @@ impl SnapshotModel { let http = create_http_client()?; let group_list = GroupList::from_html( - &download_html(&http, "https://tildes.net/groups").await?, + &download_html(&http, format!("{}/groups", base_url)).await?, )?; let mut groups_to_insert = vec![]; @@ -52,8 +53,7 @@ impl SnapshotModel { for summary in group_list.summaries { debug!(summary = ?summary); let group = Group::from_html( - &download_html(&http, format!("https://tildes.net/{}", summary.name)) - .await?, + &download_html(&http, format!("{}/{}", base_url, summary.name)).await?, )?; debug!(group = ?group); diff --git a/source/templates/index.html b/source/templates/index.html index 9a04060..c27ae6e 100644 --- a/source/templates/index.html +++ b/source/templates/index.html @@ -34,7 +34,7 @@ {% for group in groups %} - {{ group.name }} + {{ group.name }} {{ group.subscribers }} @@ -62,7 +62,7 @@

- Consider joining Tildes, a non-profit + Consider joining Tildes, a non-profit community site driven by its users' interests.

diff --git a/source/templates/mod.rs b/source/templates/mod.rs index 1a7d63a..f797fd9 100644 --- a/source/templates/mod.rs +++ b/source/templates/mod.rs @@ -10,12 +10,18 @@ use { color_eyre::Result, }; -use crate::{group_data::GroupDataModel, utilities::today}; +use crate::{ + group_data::GroupDataModel, + utilities::{get_base_url, today}, +}; /// The template for the home page. #[derive(Template)] #[template(path = "index.html")] pub struct HomeTemplate { + /// The base URL for links to the Tildes instance. + pub base_url: String, + /// Extra HTML to insert in the body. pub extra_body_html: String, @@ -45,6 +51,7 @@ impl HomeTemplate { let extra_head_html = read_to_string("extra-head.html").await; Self { + base_url: get_base_url(), extra_body_html: extra_body_html.unwrap_or_default(), extra_head_html: extra_head_html.unwrap_or_default(), groups, diff --git a/source/utilities.rs b/source/utilities.rs index 6907552..e9e4288 100644 --- a/source/utilities.rs +++ b/source/utilities.rs @@ -52,6 +52,12 @@ pub async fn download_html( Ok(Html::parse_document(&html)) } +/// Get the `BASE_URL` environment variable or the default `https://tildes.net` +/// base URL. +pub fn get_base_url() -> String { + get_env_var("BASE_URL").unwrap_or_else(|_| "https://tildes.net".to_string()) +} + /// Shorthand for [`std::env::var`] with wrapped error message. pub fn get_env_var(key: &str) -> Result { std::env::var(key).wrap_err(key.to_string())