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. | # The User-Agent string for HTTP requests, replace the email with yours. | ||||||
| # USER_AGENT="Tildes Statistics (your-email@example.org)" | # 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::{ | use crate::{ | ||||||
|   group_data::{GroupDataActiveModel, GroupDataEntity}, |   group_data::{GroupDataActiveModel, GroupDataEntity}, | ||||||
|   snapshots::{SnapshotActiveModel, SnapshotModel}, |   snapshots::{SnapshotActiveModel, SnapshotModel}, | ||||||
|   utilities::{create_http_client, download_html, today}, |   utilities::{create_http_client, download_html, get_base_url, today}, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| impl SnapshotModel { | impl SnapshotModel { | ||||||
|   /// Create a snapshot for today.
 |   /// Create a snapshot for today.
 | ||||||
|   pub async fn create(db: &DatabaseConnection, force: bool) -> Result<()> { |   pub async fn create(db: &DatabaseConnection, force: bool) -> Result<()> { | ||||||
|  |     let base_url = get_base_url(); | ||||||
|     let snapshot_date = today(); |     let snapshot_date = today(); | ||||||
|     match (force, Self::get_by_date(db, snapshot_date).await?) { |     match (force, Self::get_by_date(db, snapshot_date).await?) { | ||||||
|       (true, Some(existing)) => { |       (true, Some(existing)) => { | ||||||
|  | @ -44,7 +45,7 @@ impl SnapshotModel { | ||||||
| 
 | 
 | ||||||
|     let http = create_http_client()?; |     let http = create_http_client()?; | ||||||
|     let group_list = GroupList::from_html( |     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![]; |     let mut groups_to_insert = vec![]; | ||||||
|  | @ -52,8 +53,7 @@ impl SnapshotModel { | ||||||
|     for summary in group_list.summaries { |     for summary in group_list.summaries { | ||||||
|       debug!(summary = ?summary); |       debug!(summary = ?summary); | ||||||
|       let group = Group::from_html( |       let group = Group::from_html( | ||||||
|         &download_html(&http, format!("https://tildes.net/{}", summary.name)) |         &download_html(&http, format!("{}/{}", base_url, summary.name)).await?, | ||||||
|           .await?, |  | ||||||
|       )?; |       )?; | ||||||
| 
 | 
 | ||||||
|       debug!(group = ?group); |       debug!(group = ?group); | ||||||
|  |  | ||||||
|  | @ -34,7 +34,7 @@ | ||||||
|       {% for group in groups %} |       {% for group in groups %} | ||||||
|       <tr> |       <tr> | ||||||
|         <td> |         <td> | ||||||
|           <a href="https://tildes.net/{{ group.name }}">{{ group.name }}</a> |           <a href="{{ base_url }}/{{ group.name }}">{{ group.name }}</a> | ||||||
|         </td> |         </td> | ||||||
|         <td>{{ group.subscribers }}</td> |         <td>{{ group.subscribers }}</td> | ||||||
|         <td> |         <td> | ||||||
|  | @ -62,7 +62,7 @@ | ||||||
|   </p> |   </p> | ||||||
| 
 | 
 | ||||||
|   <p class="bold"> |   <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. |     community site driven by its users' interests. | ||||||
|   </p> |   </p> | ||||||
| </footer> | </footer> | ||||||
|  |  | ||||||
|  | @ -10,12 +10,18 @@ use { | ||||||
|   color_eyre::Result, |   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.
 | /// The template for the home page.
 | ||||||
| #[derive(Template)] | #[derive(Template)] | ||||||
| #[template(path = "index.html")] | #[template(path = "index.html")] | ||||||
| pub struct HomeTemplate { | pub struct HomeTemplate { | ||||||
|  |   /// The base URL for links to the Tildes instance.
 | ||||||
|  |   pub base_url: String, | ||||||
|  | 
 | ||||||
|   /// Extra HTML to insert in the body.
 |   /// Extra HTML to insert in the body.
 | ||||||
|   pub extra_body_html: String, |   pub extra_body_html: String, | ||||||
| 
 | 
 | ||||||
|  | @ -45,6 +51,7 @@ impl HomeTemplate { | ||||||
|     let extra_head_html = read_to_string("extra-head.html").await; |     let extra_head_html = read_to_string("extra-head.html").await; | ||||||
| 
 | 
 | ||||||
|     Self { |     Self { | ||||||
|  |       base_url: get_base_url(), | ||||||
|       extra_body_html: extra_body_html.unwrap_or_default(), |       extra_body_html: extra_body_html.unwrap_or_default(), | ||||||
|       extra_head_html: extra_head_html.unwrap_or_default(), |       extra_head_html: extra_head_html.unwrap_or_default(), | ||||||
|       groups, |       groups, | ||||||
|  |  | ||||||
|  | @ -52,6 +52,12 @@ pub async fn download_html( | ||||||
|   Ok(Html::parse_document(&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.
 | /// Shorthand for [`std::env::var`] with wrapped error message.
 | ||||||
| pub fn get_env_var(key: &str) -> Result<String> { | pub fn get_env_var(key: &str) -> Result<String> { | ||||||
|   std::env::var(key).wrap_err(key.to_string()) |   std::env::var(key).wrap_err(key.to_string()) | ||||||
|  |  | ||||||
		Reference in New Issue