feat(webapi): request trace layer and openapi spec export tool
ZhenYi committed
This change is very large, so some diffs are not shown. Clone the repository to see the full change.
Added app/openapi/src/main.rs app/openapi/src/main.rs +21 −0 View app/openapi/src/main.rs
@@ -0,0 +1,21 @@
1use std::path::PathBuf;
2
3use anyhow::{Context, anyhow};
4use webapi::openapi::api_doc;
5
6fn main() -> anyhow::Result<()> {
7 let json = api_doc()
8 .to_pretty_json()
9 .context("serializing openapi doc")?;
10
11 let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
12 let root = crate_dir
13 .ancestors()
14 .nth(2)
15 .ok_or_else(|| anyhow!("workspace root not found above {}", crate_dir.display()))?;
16 let output = root.join("openapi.json");
17
18 std::fs::write(&output, json).with_context(|| format!("writing {}", output.display()))?;
19 println!("wrote {}", output.display());
20 Ok(())
21}
Added app/openapi/Cargo.toml app/openapi/Cargo.toml +9 −0 View app/openapi/Cargo.toml
@@ -0,0 +1,9 @@
1[package]
2name = "openapi"
3version.workspace = true
4edition.workspace = true
5publish = false
6
7[dependencies]
8webapi = { workspace = true }
9anyhow = { workspace = true }
Modified lib/webapi/src/lib.rs lib/webapi/src/lib.rs +9 −1 View lib/webapi/src/lib.rs
@@ -3,6 +3,8 @@
33use axum::Router;
44use export::AppState;
55use session::{store, SessionLayer};
6use tower_http::trace::{DefaultMakeSpan, DefaultOnRequest, DefaultOnResponse, TraceLayer};
7use tracing::Level;
68
79pub async fn router(state: AppState) -> Result<Router, axum_session::SessionError> {
810 let store = store::connect(state.redis.clone(), &state.config.session).await?;
@@ -19,7 +21,13 @@
1921 .merge(space::routes::router(space.clone()).with_state(space))
2022 .merge(user::routes::router(user.clone()).with_state(user))
2123 .merge(git::routes::router(git.clone()).with_state(git))
22 .layer(SessionLayer::new(store));
24 .layer(SessionLayer::new(store))
25 .layer(
26 TraceLayer::new_for_http()
27 .make_span_with(DefaultMakeSpan::new().level(Level::INFO).include_headers(false))
28 .on_request(DefaultOnRequest::new().level(Level::INFO))
29 .on_response(DefaultOnResponse::new().level(Level::INFO)),
30 );
2331
2432 Ok(router)
2533}
No newline at end of file
Modified lib/webapi/Cargo.toml lib/webapi/Cargo.toml +2 −0 View lib/webapi/Cargo.toml
@@ -13,4 +13,6 @@
1313session = { workspace = true }
1414axum = { workspace = true }
1515axum_session = { workspace = true }
16tower-http = { workspace = true }
17tracing = { workspace = true }
1618utoipa = { workspace = true }
No newline at end of file
Modified Cargo.lock Cargo.lock +27 −0 View Cargo.lock
@@ -2576,6 +2576,7 @@
25762576 "gix",
25772577 "globset",
25782578 "http-body-util",
2579 "maud",
25792580 "models",
25802581 "rand 0.8.7",
25812582 "redis",
@@ -5332,6 +5333,14 @@
53325333checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381"
53335334
53345335[[package]]
5336name = "openapi"
5337version = "1.0.0"
5338dependencies = [
5339 "anyhow",
5340 "webapi",
5341]
5342
5343[[package]]
53355344name = "openssl"
53365345version = "0.10.81"
53375346source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -8096,6 +8105,22 @@
80968105]
80978106
80988107[[package]]
8108name = "tower-http"
8109version = "0.6.11"
8110source = "registry+https://github.com/rust-lang/crates.io-index"
8111checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840"
8112dependencies = [
8113 "bitflags 2.13.1",
8114 "bytes",
8115 "http 1.5.0",
8116 "http-body 1.1.0",
8117 "pin-project-lite",
8118 "tower-layer",
8119 "tower-service",
8120 "tracing",
8121]
8122
8123[[package]]
80998124name = "tower-layer"
81008125version = "0.3.3"
81018126source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -8574,6 +8599,8 @@
85748599 "issues",
85758600 "session",
85768601 "space",
8602 "tower-http",
8603 "tracing",
85778604 "user",
85788605 "utoipa",
85798606]
Modified Cargo.toml Cargo.toml +3 −1 View Cargo.toml
@@ -18,6 +18,7 @@
1818 "app/githost",
1919 "app/mails",
2020 "app/migrate",
21 "app/openapi",
2122 "app/synchost"
2223]
2324
@@ -51,6 +52,7 @@
5152serde_json = "1.0.151"
5253axum = "0.8.9"
5354axum_session = { version = "0.21.0", features = ["key-store"] }
55tower-http = { version = "0.6", features = ["trace"] }
5456tracing = "0.1.44"
5557figment = { version = "0.10", features = ["toml", "env"] }
5658dotenvy = "0.15"
@@ -69,7 +71,7 @@
6971uuid = { version = "1", features = ["v7", "serde"] }
7072argon2 = "0.5"
7173aes-gcm = "0.10"
72time = { version = "0.3", features = ["serde"] }
74time = { version = "0.3", features = ["serde", "serde-human-readable"] }
7375ssh-key = "0.6"
7476bytes = "1"
7577futures = "0.3"
Added openapi.json openapi.json View openapi.json
Diff too large to display.