-
Notifications
You must be signed in to change notification settings - Fork 210
Add new endpoint giving simple rustdoc status for a version #2147
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d7b5f78
Don't record metrics for feed and builds.json as static resources
Nemo157 04eb1b3
Add new endpoint giving simple rustdoc status for a version
Nemo157 8b388a7
Rename assume_exact methods
Nemo157 7d0c82f
Allow semver in status paths
Nemo157 77677d1
Use redirects for semver status requests
Nemo157 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
use super::cache::CachePolicy; | ||
use crate::{ | ||
db::Pool, | ||
utils::spawn_blocking, | ||
web::{axum_redirect, error::AxumResult, match_version_axum, MatchSemver}, | ||
}; | ||
use axum::{ | ||
extract::{Extension, Path}, | ||
http::header::ACCESS_CONTROL_ALLOW_ORIGIN, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
|
||
pub(crate) async fn status_handler( | ||
Path((name, req_version)): Path<(String, String)>, | ||
Extension(pool): Extension<Pool>, | ||
) -> impl IntoResponse { | ||
( | ||
Extension(CachePolicy::NoStoreMustRevalidate), | ||
[(ACCESS_CONTROL_ALLOW_ORIGIN, "*")], | ||
// We use an async block to emulate a try block so that we can apply the above CORS header | ||
// and cache policy to both successful and failed responses | ||
async move { | ||
let (version, id) = match match_version_axum(&pool, &name, Some(&req_version)) | ||
.await? | ||
.exact_name_only()? | ||
{ | ||
MatchSemver::Exact((version, id)) | MatchSemver::Latest((version, id)) => { | ||
(version, id) | ||
} | ||
MatchSemver::Semver((version, _)) => { | ||
let redirect = axum_redirect(format!("/crate/{name}/{version}/status.json"))?; | ||
return Ok(redirect.into_response()); | ||
} | ||
}; | ||
|
||
let rustdoc_status: bool = spawn_blocking({ | ||
move || { | ||
Ok(pool | ||
.get()? | ||
.query_one( | ||
"SELECT releases.rustdoc_status | ||
FROM releases | ||
WHERE releases.id = $1 | ||
", | ||
&[&id], | ||
)? | ||
.get("rustdoc_status")) | ||
} | ||
}) | ||
.await?; | ||
|
||
let json = Json(serde_json::json!({ | ||
"version": version, | ||
"doc_status": rustdoc_status, | ||
})); | ||
|
||
AxumResult::Ok(json.into_response()) | ||
} | ||
.await, | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{ | ||
test::{assert_cache_control, assert_redirect, wrapper}, | ||
web::cache::CachePolicy, | ||
}; | ||
use reqwest::StatusCode; | ||
use test_case::test_case; | ||
|
||
#[test_case("latest")] | ||
#[test_case("0.1")] | ||
#[test_case("0.1.0")] | ||
#[test_case("=0.1.0"; "exact_version")] | ||
fn status(version: &str) { | ||
wrapper(|env| { | ||
env.fake_release().name("foo").version("0.1.0").create()?; | ||
|
||
let response = env | ||
.frontend() | ||
.get(&format!("/crate/foo/{version}/status.json")) | ||
.send()?; | ||
assert_cache_control(&response, CachePolicy::NoStoreMustRevalidate, &env.config()); | ||
assert_eq!(response.headers()["access-control-allow-origin"], "*"); | ||
assert_eq!(response.status(), StatusCode::OK); | ||
let value: serde_json::Value = serde_json::from_str(&response.text()?)?; | ||
|
||
assert_eq!( | ||
value, | ||
serde_json::json!({ | ||
"version": "0.1.0", | ||
"doc_status": true, | ||
}) | ||
); | ||
|
||
Ok(()) | ||
}); | ||
} | ||
|
||
#[test_case("0.1")] | ||
#[test_case("*")] | ||
fn redirect(version: &str) { | ||
wrapper(|env| { | ||
env.fake_release().name("foo").version("0.1.0").create()?; | ||
|
||
let redirect = assert_redirect( | ||
&format!("/crate/foo/{version}/status.json"), | ||
"/crate/foo/0.1.0/status.json", | ||
env.frontend(), | ||
)?; | ||
assert_cache_control(&redirect, CachePolicy::NoStoreMustRevalidate, &env.config()); | ||
assert_eq!(redirect.headers()["access-control-allow-origin"], "*"); | ||
|
||
Ok(()) | ||
}); | ||
} | ||
|
||
#[test_case("latest")] | ||
#[test_case("0.1")] | ||
#[test_case("0.1.0")] | ||
#[test_case("=0.1.0"; "exact_version")] | ||
fn failure(version: &str) { | ||
wrapper(|env| { | ||
env.fake_release() | ||
.name("foo") | ||
.version("0.1.0") | ||
.build_result_failed() | ||
.create()?; | ||
|
||
let response = env | ||
.frontend() | ||
.get(&format!("/crate/foo/{version}/status.json")) | ||
.send()?; | ||
assert_cache_control(&response, CachePolicy::NoStoreMustRevalidate, &env.config()); | ||
assert_eq!(response.headers()["access-control-allow-origin"], "*"); | ||
assert_eq!(response.status(), StatusCode::OK); | ||
let value: serde_json::Value = serde_json::from_str(&response.text()?)?; | ||
|
||
assert_eq!( | ||
value, | ||
serde_json::json!({ | ||
"version": "0.1.0", | ||
"doc_status": false, | ||
}) | ||
); | ||
|
||
Ok(()) | ||
}); | ||
} | ||
|
||
// crate not found | ||
#[test_case("bar", "0.1")] | ||
#[test_case("bar", "0.1.0")] | ||
// version not found | ||
#[test_case("foo", "=0.1.0"; "exact_version")] | ||
#[test_case("foo", "0.2")] | ||
#[test_case("foo", "0.2.0")] | ||
// invalid semver | ||
#[test_case("foo", "0,1")] | ||
#[test_case("foo", "0,1,0")] | ||
fn not_found(krate: &str, version: &str) { | ||
wrapper(|env| { | ||
env.fake_release().name("foo").version("0.1.1").create()?; | ||
|
||
let response = env | ||
.frontend() | ||
.get(&format!("/crate/{krate}/{version}/status.json")) | ||
.send()?; | ||
assert_cache_control(&response, CachePolicy::NoStoreMustRevalidate, &env.config()); | ||
assert_eq!(response.headers()["access-control-allow-origin"], "*"); | ||
assert_eq!(response.status(), StatusCode::NOT_FOUND); | ||
Ok(()) | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.