Skip to content

Commit d618085

Browse files
committed
Simplify OG_IMAGE_BASE_URL usage
If `OG_IMAGE_BASE_URL` is not set we can fall back to the default image instead of using `.unwrap()` and assuming that the option is always set.
1 parent 2cf8783 commit d618085

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

src/config/server.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::config::cdn_log_storage::CdnLogStorageConfig;
1212
use crate::config::CdnLogQueueConfig;
1313
use crate::middleware::cargo_compat::StatusCodeConfig;
1414
use crate::storage::StorageConfig;
15-
use crates_io_env_vars::{list, list_parsed, required_var, required_var_parsed, var, var_parsed};
15+
use crates_io_env_vars::{list, list_parsed, required_var, var, var_parsed};
1616
use http::HeaderValue;
1717
use std::collections::{HashMap, HashSet};
1818
use std::convert::Infallible;
@@ -74,8 +74,6 @@ pub struct Server {
7474

7575
/// Should the server serve the frontend `index.html` for all
7676
/// non-API requests?
77-
/// Setting this parameter requires setting
78-
/// [`Self::og_image_base_url`] as well.
7977
pub serve_html: bool,
8078

8179
/// Base URL for the service from which the OpenGraph images
@@ -184,14 +182,6 @@ impl Server {
184182
cdn_domain = storage.cdn_prefix.as_ref().map(|cdn_prefix| format!("https://{cdn_prefix}")).unwrap_or_default()
185183
);
186184

187-
let serve_html = var_parsed("SERVE_HTML")?.unwrap_or(true);
188-
let og_image_base_url = serve_html
189-
.then(|| {
190-
required_var_parsed("OG_IMAGE_BASE_URL")
191-
.context("OG_IMAGE_BASE_URL must be set when using INDEX_HTML_TEMPLATE_PATH")
192-
})
193-
.transpose()?;
194-
195185
Ok(Server {
196186
db: DatabasePools::full_from_environment(&base)?,
197187
storage,
@@ -236,7 +226,7 @@ impl Server {
236226
.unwrap_or(StatusCodeConfig::AdjustAll),
237227
serve_dist: true,
238228
serve_html: true,
239-
og_image_base_url,
229+
og_image_base_url: var_parsed("OG_IMAGE_BASE_URL")?,
240230
html_render_cache_max_capacity: var_parsed("HTML_RENDER_CACHE_CAP")?.unwrap_or(1024),
241231
content_security_policy: Some(content_security_policy.parse()?),
242232
})

src/middleware/ember_html.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ pub async fn serve_html(state: AppState, request: Request, next: Next) -> Respon
7676
// `state.config.og_image_base_url` will always be `Some` as that's required
7777
// if `state.config.serve_html` is `true`, and otherwise this
7878
// middleware won't be executed; see `crate::middleware::apply_axum_middleware`.
79-
let og_image_base_url = state.config.og_image_base_url.as_ref().unwrap();
80-
let og_image_url = generate_og_image_url(path, og_image_base_url);
79+
let og_image_url = generate_og_image_url(path, &state.config.og_image_base_url);
8180

8281
// Fetch the HTML from cache given `og_image_url` as key or render it
8382
let html = RENDERED_HTML_CACHE
@@ -125,7 +124,11 @@ fn extract_crate_name(path: &str) -> Option<&str> {
125124
/// Come up with an Open Graph image URL. In case a crate page is requested,
126125
/// we use the crate's name as extracted from the request path and the OG image
127126
/// base URL from config to generate one, otherwise we use the fallback image.
128-
fn generate_og_image_url(path: &str, og_image_base_url: &Url) -> Cow<'static, str> {
127+
fn generate_og_image_url(path: &str, og_image_base_url: &Option<Url>) -> Cow<'static, str> {
128+
let Some(og_image_base_url) = og_image_base_url else {
129+
return OG_IMAGE_FALLBACK_URL.into();
130+
};
131+
129132
if let Some(krate) = extract_crate_name(path) {
130133
if let Ok(og_img_url) = og_image_base_url
131134
.join(krate)
@@ -182,6 +185,7 @@ mod tests {
182185
];
183186

184187
let og_image_base_url: Url = "http://localhost:3000/og/".parse().unwrap();
188+
let og_image_base_url = Some(og_image_base_url);
185189

186190
for (path, expected) in PATHS.iter().copied() {
187191
assert_that!(

0 commit comments

Comments
 (0)