Skip to content

Commit a66fe51

Browse files
authored
Merge pull request #2134 from Urgau/axum
Migrate to axum and hyper 1 🎉
2 parents 517aecc + d637251 commit a66fe51

File tree

14 files changed

+819
-780
lines changed

14 files changed

+819
-780
lines changed

Cargo.lock

Lines changed: 184 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ parser = { path = "parser" }
1717
rust_team_data = { git = "https://github.com/rust-lang/team" }
1818
glob = "0.3.0"
1919
toml = "0.8.20"
20-
hyper = { version = "0.14.4", features = ["server", "stream", "http1", "tcp"] }
20+
axum = "0.8.4"
21+
hyper = { version = "1.6", features = ["server", "http1"] }
2122
tokio = { version = "1", features = ["macros", "time", "rt"] }
2223
futures = { version = "0.3", default-features = false, features = ["std"] }
2324
async-trait = "0.1.31"
@@ -33,10 +34,10 @@ x509-cert = { version = "0.2.5", features = ["pem"] }
3334
serde_path_to_error = "0.1.2"
3435
octocrab = { version = "0.44", features = ["stream"] }
3536
comrak = { version = "0.38", default-features = false }
36-
route-recognizer = "0.3.0"
3737
cynic = "3"
3838
itertools = "0.14.0"
3939
tower = { version = "0.5", features = ["util", "limit", "buffer", "load-shed"] }
40+
tower-http = { version = "0.6", features = ["compression-br", "compression-gzip", "request-id", "catch-panic"] }
4041
github-graphql = { path = "github-graphql" }
4142
rand = "0.8.5"
4243
ignore = "0.4.18"

src/agenda.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1+
use axum::response::Html;
2+
13
use crate::actions::{Action, Query, QueryKind, QueryMap, Step};
24
use crate::github;
5+
use crate::utils::AppError;
36
use std::sync::Arc;
47

8+
pub async fn lang_http() -> axum::response::Result<Html<String>, AppError> {
9+
Ok(Html(lang().call().await?))
10+
}
11+
12+
pub async fn lang_planning_http() -> axum::response::Result<Html<String>, AppError> {
13+
Ok(Html(lang_planning().call().await?))
14+
}
15+
16+
pub async fn types_planning_http() -> axum::response::Result<String, AppError> {
17+
Ok(types_planning().call().await?)
18+
}
19+
520
pub fn prioritization<'a>() -> Box<dyn Action> {
621
Box::new(Step {
722
name: "prioritization_agenda",

src/bors.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::sync::Arc;
2+
3+
use axum::{Json, extract::State};
4+
5+
use crate::{db, handlers::Context, utils::AppError};
6+
7+
pub async fn bors_commit_list(
8+
State(ctx): State<Arc<Context>>,
9+
) -> axum::response::Result<Json<Vec<db::rustc_commits::Commit>>, AppError> {
10+
Ok(Json(
11+
db::rustc_commits::get_commits_with_artifacts(&*ctx.db.get().await).await?,
12+
))
13+
}

src/gha_logs.rs

Lines changed: 58 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use crate::github::{self, WorkflowRunJob};
22
use crate::handlers::Context;
3+
use crate::utils::AppError;
34
use anyhow::Context as _;
5+
use axum::extract::{Path, State};
6+
use axum::http::HeaderValue;
7+
use axum::response::IntoResponse;
48
use hyper::header::{CACHE_CONTROL, CONTENT_SECURITY_POLICY, CONTENT_TYPE};
5-
use hyper::{Body, Response, StatusCode};
9+
use hyper::{HeaderMap, StatusCode};
610
use std::collections::VecDeque;
7-
use std::str::FromStr;
811
use std::sync::Arc;
912
use uuid::Uuid;
1013

@@ -64,50 +67,29 @@ impl GitHubActionLogsCache {
6467
}
6568

6669
pub async fn gha_logs(
67-
ctx: Arc<Context>,
68-
owner: &str,
69-
repo: &str,
70-
log_id: &str,
71-
) -> Result<Response<Body>, hyper::Error> {
72-
let res = process_logs(ctx, owner, repo, log_id).await;
73-
let res = match res {
74-
Ok(r) => r,
75-
Err(e) => {
76-
tracing::error!("gha_logs: unable to serve logs for {owner}/{repo}#{log_id}: {e:?}");
77-
return Ok(Response::builder()
78-
.status(StatusCode::INTERNAL_SERVER_ERROR)
79-
.body(Body::from(format!("{:?}", e)))
80-
.unwrap());
81-
}
82-
};
83-
84-
Ok(res)
85-
}
86-
87-
async fn process_logs(
88-
ctx: Arc<Context>,
89-
owner: &str,
90-
repo: &str,
91-
log_id: &str,
92-
) -> anyhow::Result<Response<Body>> {
93-
let log_id = u128::from_str(log_id).context("log_id is not a number")?;
94-
70+
Path((owner, repo, log_id)): Path<(String, String, u128)>,
71+
State(ctx): State<Arc<Context>>,
72+
) -> axum::response::Result<impl IntoResponse, AppError> {
9573
let repos = ctx
9674
.team
9775
.repos()
9876
.await
9977
.context("unable to retrieve team repos")?;
10078

101-
let Some(repos) = repos.repos.get(owner) else {
102-
return Ok(bad_request(format!(
103-
"organization `{owner}` is not part of the Rust Project team repos"
104-
)));
79+
let Some(repos) = repos.repos.get(&owner) else {
80+
return Ok((
81+
StatusCode::BAD_REQUEST,
82+
HeaderMap::new(),
83+
format!("organization `{owner}` is not part of the Rust Project team repos"),
84+
));
10585
};
10686

10787
if !repos.iter().any(|r| r.name == repo) {
108-
return Ok(bad_request(format!(
109-
"repository `{owner}` is not part of the Rust Project team repos"
110-
)));
88+
return Ok((
89+
StatusCode::BAD_REQUEST,
90+
HeaderMap::new(),
91+
format!("repository `{owner}` is not part of the Rust Project team repos"),
92+
));
11193
}
11294

11395
let log_uuid = format!("{owner}/{repo}${log_id}");
@@ -368,54 +350,57 @@ body {{
368350

369351
tracing::info!("gha_logs: serving logs for {log_uuid}");
370352

371-
return Ok(Response::builder()
372-
.status(StatusCode::OK)
373-
.header(CONTENT_TYPE, "text/html; charset=utf-8")
374-
.header(
375-
CONTENT_SECURITY_POLICY,
376-
format!(
377-
"default-src 'none'; script-src 'nonce-{nonce}' 'self'; style-src 'unsafe-inline'; img-src 'self' www.rust-lang.org"
378-
),
379-
)
380-
.body(Body::from(html))?);
353+
let mut headers = HeaderMap::new();
354+
headers.insert(
355+
CONTENT_TYPE,
356+
HeaderValue::from_static("text/html; charset=utf-8"),
357+
);
358+
headers.insert(
359+
CONTENT_SECURITY_POLICY,
360+
HeaderValue::from_str(&*
361+
format!(
362+
"default-src 'none'; script-src 'nonce-{nonce}' 'self'; style-src 'unsafe-inline'; img-src 'self' www.rust-lang.org"
363+
)).unwrap(),
364+
);
365+
366+
Ok((StatusCode::OK, headers, html))
381367
}
382368

383-
pub fn ansi_up_min_js() -> anyhow::Result<Response<Body>, hyper::Error> {
369+
pub async fn ansi_up_min_js() -> impl IntoResponse {
384370
const ANSI_UP_MIN_JS: &str = include_str!("gha_logs/[email protected]");
385371

386-
Ok(Response::builder()
387-
.status(StatusCode::OK)
388-
.header(CACHE_CONTROL, "public, max-age=15552000, immutable")
389-
.header(CONTENT_TYPE, "text/javascript; charset=utf-8")
390-
.body(Body::from(ANSI_UP_MIN_JS))
391-
.unwrap())
372+
(
373+
immutable_headers("text/javascript; charset=utf-8"),
374+
ANSI_UP_MIN_JS,
375+
)
392376
}
393377

394-
pub fn success_svg() -> anyhow::Result<Response<Body>, hyper::Error> {
378+
pub async fn success_svg() -> impl IntoResponse {
395379
const SUCCESS_SVG: &str = include_str!("gha_logs/success.svg");
396380

397-
Ok(Response::builder()
398-
.status(StatusCode::OK)
399-
.header(CACHE_CONTROL, "public, max-age=15552000, immutable")
400-
.header(CONTENT_TYPE, "image/svg+xml; charset=utf-8")
401-
.body(Body::from(SUCCESS_SVG))
402-
.unwrap())
381+
(
382+
immutable_headers("image/svg+xml; charset=utf-8"),
383+
SUCCESS_SVG,
384+
)
403385
}
404386

405-
pub fn failure_svg() -> anyhow::Result<Response<Body>, hyper::Error> {
387+
pub async fn failure_svg() -> impl IntoResponse {
406388
const FAILURE_SVG: &str = include_str!("gha_logs/failure.svg");
407389

408-
Ok(Response::builder()
409-
.status(StatusCode::OK)
410-
.header(CACHE_CONTROL, "public, max-age=15552000, immutable")
411-
.header(CONTENT_TYPE, "image/svg+xml; charset=utf-8")
412-
.body(Body::from(FAILURE_SVG))
413-
.unwrap())
390+
(
391+
immutable_headers("image/svg+xml; charset=utf-8"),
392+
FAILURE_SVG,
393+
)
414394
}
415395

416-
fn bad_request(body: String) -> Response<Body> {
417-
Response::builder()
418-
.status(StatusCode::BAD_REQUEST)
419-
.body(Body::from(body))
420-
.unwrap()
396+
fn immutable_headers(content_type: &'static str) -> HeaderMap {
397+
let mut headers = HeaderMap::new();
398+
399+
headers.insert(
400+
CACHE_CONTROL,
401+
HeaderValue::from_static("public, max-age=15552000, immutable"),
402+
);
403+
headers.insert(CONTENT_TYPE, HeaderValue::from_static(content_type));
404+
405+
headers
421406
}

src/github.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ use std::{
1616
};
1717
use tracing as log;
1818

19+
mod webhook;
20+
21+
pub use webhook::webhook;
22+
1923
pub type UserId = u64;
2024
pub type PullRequestNumber = u64;
2125

0 commit comments

Comments
 (0)