Skip to content

Commit 48f8ff9

Browse files
committed
wip
1 parent ae96c6d commit 48f8ff9

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/config/server.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ pub struct Server {
8787
pub html_render_cache_max_capacity: u64,
8888

8989
pub content_security_policy: Option<HeaderValue>,
90+
91+
pub docs_rs_url: url::Url,
92+
pub docs_rs_api_token: Option<String>,
9093
}
9194

9295
impl Server {
@@ -233,6 +236,9 @@ impl Server {
233236
og_image_base_url: var_parsed("OG_IMAGE_BASE_URL")?,
234237
html_render_cache_max_capacity: var_parsed("HTML_RENDER_CACHE_CAP")?.unwrap_or(1024),
235238
content_security_policy: Some(content_security_policy.parse()?),
239+
docs_rs_url: var_parsed("DOCS_RS_HOSTNAME")?
240+
.unwrap_or_else(|| url::Url::parse("https://docs.rs").unwrap()),
241+
docs_rs_api_token: var("DOCS_RS_API_TOKEN")?,
236242
})
237243
}
238244
}

src/controllers/version.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod authors;
22
pub mod dependencies;
3+
pub mod docs;
34
pub mod downloads;
45
pub mod metadata;
56
pub mod readme;

src/controllers/version/docs.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//! Endpoint for triggering a docs.rs rebuild
2+
3+
use super::update::authenticate;
4+
use super::CrateVersionPath;
5+
use crate::app::AppState;
6+
use crate::controllers::helpers::ok_true;
7+
use crate::rate_limiter::LimitedAction;
8+
use crate::util::errors::{forbidden, AppResult};
9+
use axum::response::Response;
10+
use http::request::Parts;
11+
12+
/// Trigger a rebuild for the crate documentation on docs.rs.
13+
#[utoipa::path(
14+
post,
15+
path = "/api/v1/crates/{name}/{version}/rebuild_docs",
16+
params(CrateVersionPath),
17+
security(
18+
("api_token" = []),
19+
("cookie" = []),
20+
),
21+
tag = "versions",
22+
responses((status = 201, description = "Successful Response")),
23+
)]
24+
pub async fn rebuild_version_docs(
25+
app: AppState,
26+
path: CrateVersionPath,
27+
req: Parts,
28+
) -> AppResult<Response> {
29+
let Some(ref docs_rs_api_token) = app.config.docs_rs_api_token else {
30+
return Err(forbidden("docs.rs integration is not configured"));
31+
};
32+
33+
let mut conn = app.db_read().await?;
34+
// FIXME: which scope to use?
35+
let auth = authenticate(&req, &mut conn, &path.name).await?;
36+
37+
// FIXME: rate limiting needed here? which Action?
38+
app.rate_limiter
39+
.check_rate_limit(auth.user_id(), LimitedAction::YankUnyank, &mut conn)
40+
.await?;
41+
42+
let target_url = app
43+
.config
44+
.docs_rs_url
45+
.join(&format!("/crate/{}/{}/rebuild", path.name, path.version))
46+
.unwrap(); // FIXME: handle error
47+
48+
let client = reqwest::Client::new();
49+
let response = client
50+
.post(target_url.as_str())
51+
.bearer_auth(docs_rs_api_token)
52+
.send()
53+
.await?;
54+
55+
let status= = response.status();
56+
57+
// reqwest::
58+
59+
// perform_version_yank_update(
60+
// &state,
61+
// &mut conn,
62+
// &mut version,
63+
// &krate,
64+
// &auth,
65+
// Some(yanked),
66+
// None,
67+
// )
68+
// .await?;
69+
70+
ok_true()
71+
}

0 commit comments

Comments
 (0)