Skip to content

Use the /releases/latest redirect of the blog #2143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 5 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use cache::Cached;
use rocket::catch;
use rocket::get;
use rocket::tokio::sync::RwLock;
use rust_version::RustReleasePost;
use rust_version::RustVersion;
use serde::Serialize;
use teams::RustTeams;
Expand Down Expand Up @@ -156,20 +155,13 @@ fn robots_txt() -> Option<content::RawText<&'static str>> {
}

#[get("/")]
async fn index(
version_cache: &Cache<RustVersion>,
release_post_cache: &Cache<RustReleasePost>,
) -> Template {
render_index(ENGLISH.into(), version_cache, release_post_cache).await
async fn index(version_cache: &Cache<RustVersion>) -> Template {
render_index(ENGLISH.into(), version_cache).await
}

#[get("/<locale>", rank = 3)]
async fn index_locale(
locale: SupportedLocale,
version_cache: &Cache<RustVersion>,
release_post_cache: &Cache<RustReleasePost>,
) -> Template {
render_index(locale.0, version_cache, release_post_cache).await
async fn index_locale(locale: SupportedLocale, version_cache: &Cache<RustVersion>) -> Template {
render_index(locale.0, version_cache).await
}

#[get("/<category>")]
Expand Down Expand Up @@ -328,26 +320,15 @@ fn concat_app_js(files: Vec<&str>) -> String {
String::from(&js_path[1..])
}

async fn render_index(
lang: String,
version_cache: &Cache<RustVersion>,
release_post_cache: &Cache<RustReleasePost>,
) -> Template {
async fn render_index(lang: String, version_cache: &Cache<RustVersion>) -> Template {
#[derive(Serialize)]
struct IndexData {
rust_version: String,
rust_release_post: String,
}

let page = "index";
let release_post = rust_version::rust_release_post(release_post_cache).await;
let data = IndexData {
rust_version: rust_version::rust_version(version_cache).await,
rust_release_post: if !release_post.is_empty() {
format!("https://blog.rust-lang.org/{}", release_post)
} else {
String::new()
},
};
let context = Context::new(page, "", true, data, lang);
Template::render(page, context)
Expand Down Expand Up @@ -438,14 +419,12 @@ async fn rocket() -> _ {
});

let rust_version = RustVersion::fetch().await.unwrap_or_default();
let rust_release_post = RustReleasePost::fetch().await.unwrap_or_default();
let teams = RustTeams::fetch().await.unwrap_or_default();

rocket::build()
.attach(templating)
.attach(headers::InjectHeaders)
.manage(Arc::new(RwLock::new(rust_version)))
.manage(Arc::new(RwLock::new(rust_release_post)))
.manage(Arc::new(RwLock::new(teams)))
.mount(
"/",
Expand Down
31 changes: 0 additions & 31 deletions src/rust_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use std::time::Instant;
use crate::cache::{Cache, Cached};

static MANIFEST_URL: &str = "https://static.rust-lang.org/dist/channel-rust-stable.toml";
static RELEASES_FEED_URL: &str = "https://blog.rust-lang.org/releases.json";

enum FetchTarget {
Manifest,
ReleasesFeed,
}

async fn fetch(target: FetchTarget) -> Result<reqwest::Response, Box<dyn Error + Send + Sync>> {
Expand All @@ -27,7 +25,6 @@ async fn fetch(target: FetchTarget) -> Result<reqwest::Response, Box<dyn Error +

let url = match target {
FetchTarget::Manifest => MANIFEST_URL,
FetchTarget::ReleasesFeed => RELEASES_FEED_URL,
};

Ok(client.get(url).send().await?)
Expand Down Expand Up @@ -58,34 +55,6 @@ impl Cached for RustVersion {
}
}

#[derive(Debug, Clone)]
pub struct RustReleasePost(pub String, pub Instant);

impl Default for RustReleasePost {
fn default() -> Self {
Self(Default::default(), Instant::now())
}
}

impl Cached for RustReleasePost {
fn get_timestamp(&self) -> Instant {
self.1
}
async fn fetch() -> Result<Self, Box<dyn Error + Send + Sync>> {
let releases = fetch(FetchTarget::ReleasesFeed)
.await?
.text()
.await?
.parse::<serde_json::Value>()?;
let url = releases["releases"][0]["url"].as_str().unwrap().to_string();
Ok(RustReleasePost(url, Instant::now()))
}
}

pub async fn rust_version(version_cache: &Cache<RustVersion>) -> String {
RustVersion::get(version_cache).await.0
}

pub async fn rust_release_post(release_post_cache: &Cache<RustReleasePost>) -> String {
RustReleasePost::get(release_post_cache).await.0
}
2 changes: 1 addition & 1 deletion templates/index.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{{fluent "get-started"}}
</a>
<p class="tc f3 f2-l mt3">
<a href="{{data.rust_release_post}}" class="download-link">{{#fluent "homepage-version"}}{{#fluentparam "number"}}{{data.rust_version}}{{/fluentparam}}{{/fluent}}</a>
<a href="https://blog.rust-lang.org/releases/latest" class="download-link">{{#fluent "homepage-version"}}{{#fluentparam "number"}}{{data.rust_version}}{{/fluentparam}}{{/fluent}}</a>
</p>
</div>
</div>
Expand Down