Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.
/ api Public archive

Commit 1d6c5dd

Browse files
committed
add "index"
1 parent ffe9314 commit 1d6c5dd

File tree

76 files changed

+410
-76
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+410
-76
lines changed

Cargo.lock

Lines changed: 8 additions & 7 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
@@ -1,6 +1,6 @@
11
[package]
22
name = "api"
3-
version = "3.2.4"
3+
version = "3.3.0"
44
edition = "2024"
55

66
[dependencies]
@@ -16,7 +16,7 @@ utoipa = { version = "5.3.1", features = ["axum_extras", "preserve_order", "chro
1616
utoipa-axum = "0.2.0"
1717
chrono = { version = "0.4.40", features = ["serde"] }
1818
indexmap = { version = "2.7.1", features = ["serde"] }
19-
tower-http = { version = "0.6.2", features = ["catch-panic", "trace", "cors"] }
19+
tower-http = { version = "0.6.2", features = ["catch-panic", "trace", "cors", "normalize-path"] }
2020
sentry = { version = "0.36.0", default-features = false, features = ["rustls", "reqwest", "backtrace", "contexts", "debug-images", "panic"] }
2121
tracing = "0.1.41"
2222
reqwest = { version = "0.12.12", default-features = false, features = ["json", "rustls-tls"] }
@@ -30,3 +30,4 @@ image = "0.25.5"
3030
tower-cookies = "0.11.0"
3131
rust-s3 = { version = "0.35.1", default-features = false, features = ["tokio-rustls-tls"] }
3232
futures-util = "0.3.31"
33+
tower = "0.5.2"

src/main.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod routes;
88
mod s3;
99

1010
use axum::{
11+
ServiceExt,
1112
body::Body,
1213
extract::{Path, Request},
1314
http::{HeaderMap, StatusCode},
@@ -21,8 +22,12 @@ use routes::{ApiError, GetState};
2122
use sentry_tower::SentryHttpLayer;
2223
use sha1::Digest;
2324
use std::{net::IpAddr, sync::Arc, time::Instant};
25+
use tower::Layer;
2426
use tower_cookies::CookieManagerLayer;
25-
use tower_http::{catch_panic::CatchPanicLayer, cors::CorsLayer, trace::TraceLayer};
27+
use tower_http::{
28+
catch_panic::CatchPanicLayer, cors::CorsLayer, normalize_path::NormalizePathLayer,
29+
trace::TraceLayer,
30+
};
2631
use utoipa::openapi::security::{ApiKey, ApiKeyValue, SecurityScheme};
2732
use utoipa_axum::router::OpenApiRouter;
2833

@@ -192,7 +197,7 @@ async fn main() {
192197

193198
let app =
194199
OpenApiRouter::new()
195-
.nest("/api", routes::router(&state))
200+
.merge(routes::router(&state))
196201
.route(
197202
"/",
198203
get(|| async move {
@@ -203,7 +208,7 @@ async fn main() {
203208
(
204209
StatusCode::OK,
205210
headers,
206-
include_str!("../static/index.html"),
211+
include_str!("../static/api.html"),
207212
)
208213
}),
209214
)
@@ -347,7 +352,12 @@ async fn main() {
347352

348353
let router = router.route("/openapi.json", get(|| async move { axum::Json(openapi) }));
349354

350-
axum::serve(listener, router.into_make_service())
351-
.await
352-
.unwrap();
355+
axum::serve(
356+
listener,
357+
ServiceExt::<Request>::into_make_service(
358+
NormalizePathLayer::trim_trailing_slash().layer(router),
359+
),
360+
)
361+
.await
362+
.unwrap();
353363
}

src/models/build.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ impl BaseModel for Build {
172172
}
173173

174174
impl Build {
175+
pub fn installation_size(&self) -> u64 {
176+
self.installation
177+
.iter()
178+
.flat_map(|step| step.iter())
179+
.filter_map(|step| match step {
180+
InstallationStep::Download(step) => Some(step.size),
181+
_ => None,
182+
})
183+
.sum()
184+
}
185+
175186
pub async fn by_v1_identifier(
176187
database: &crate::database::Database,
177188
cache: &crate::cache::Cache,

src/models/type.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ impl ServerType {
201201

202202
result
203203
}
204+
205+
pub fn infos(&self) -> &ServerTypeInfo {
206+
TYPE_INFOS.get(self).unwrap()
207+
}
204208
}
205209

206210
static TYPE_INFOS: LazyLock<IndexMap<ServerType, ServerTypeInfo>> = LazyLock::new(|| {

src/requests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ impl RequestLogger {
7474
};
7575

7676
let mut ratelimit: Option<RateLimitData> = None;
77-
7877
if organization.is_none() || !organization.as_ref().unwrap().verified {
7978
let ratelimit_key = format!("mcjars_api::ratelimit::{}", ip);
8079

@@ -103,6 +102,7 @@ impl RequestLogger {
103102
.map(|q| q.contains("tracking=none"))
104103
.unwrap_or(false)
105104
|| ACCEPTED_METHODS.iter().all(|m| *m != request.method)
105+
|| !request.uri.path().starts_with("/api")
106106
|| request.uri.path().starts_with("/api/github")
107107
{
108108
return Ok((None, ratelimit));

src/routes/api/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use super::{ApiError, GetState, State};
2+
use utoipa_axum::router::OpenApiRouter;
3+
4+
mod github;
5+
mod organization;
6+
mod user;
7+
mod v1;
8+
mod v2;
9+
10+
pub fn router(state: &State) -> OpenApiRouter<State> {
11+
OpenApiRouter::new()
12+
.nest("/v1", v1::router(state))
13+
.nest("/v2", v2::router(state))
14+
.nest("/organization", organization::router(state))
15+
.nest("/github", github::router(state))
16+
.nest("/user", user::router(state))
17+
.with_state(state.clone())
18+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod types;
66
mod get {
77
use crate::{
88
models::r#type::ServerType,
9-
routes::{GetState, organization::GetOrganization},
9+
routes::{GetState, api::organization::GetOrganization},
1010
};
1111
use serde::{Deserialize, Serialize};
1212
use sqlx::Row;

0 commit comments

Comments
 (0)