Add a configurable base URL.
This commit is contained in:
parent
e41763a229
commit
b407273f6f
|
@ -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"
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
{% for group in groups %}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="https://tildes.net/{{ group.name }}">{{ group.name }}</a>
|
||||
<a href="{{ base_url }}/{{ group.name }}">{{ group.name }}</a>
|
||||
</td>
|
||||
<td>{{ group.subscribers }}</td>
|
||||
<td>
|
||||
|
@ -62,7 +62,7 @@
|
|||
</p>
|
||||
|
||||
<p class="bold">
|
||||
Consider joining <a href="https://tildes.net">Tildes</a>, a non-profit
|
||||
Consider joining <a href="{{ base_url }}">Tildes</a>, a non-profit
|
||||
community site driven by its users' interests.
|
||||
</p>
|
||||
</footer>
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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<String> {
|
||||
std::env::var(key).wrap_err(key.to_string())
|
||||
|
|
Loading…
Reference in New Issue