refactor: bump axum 0.6, use recommended way to nest routes (#668)

This commit is contained in:
Dongxu Wang
2022-11-30 20:04:33 +08:00
committed by GitHub
parent 68c2de8e45
commit 6d24f7ebb6
5 changed files with 88 additions and 60 deletions

48
Cargo.lock generated
View File

@@ -51,11 +51,11 @@ dependencies = [
[[package]]
name = "aide"
version = "0.6.2"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a47c350c121222a7d8cc7d2efad0856ddd3a903eb62f0f5e982efb27d811c94c"
checksum = "befdff0b4683a0824fc8719ce639a252d9d62cd89c8d0004c39e2417128c1eb8"
dependencies = [
"axum 0.6.0-rc.2",
"axum 0.6.1",
"bytes",
"cfg-if",
"http",
@@ -407,12 +407,12 @@ dependencies = [
[[package]]
name = "axum"
version = "0.6.0-rc.2"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2628a243073c55aef15a1c1fe45c87f21b84f9e89ca9e7b262a180d3d03543d"
checksum = "08b108ad2665fa3f6e6a517c3d80ec3e77d224c47d605167aefaa5d7ef97fa48"
dependencies = [
"async-trait",
"axum-core 0.3.0-rc.2",
"axum-core 0.3.0",
"bitflags",
"bytes",
"futures-util",
@@ -420,13 +420,15 @@ dependencies = [
"http-body",
"hyper",
"itoa 1.0.3",
"matchit 0.6.0",
"matchit 0.7.0",
"memchr",
"mime",
"percent-encoding",
"pin-project-lite",
"rustversion",
"serde",
"serde_json",
"serde_path_to_error",
"serde_urlencoded",
"sync_wrapper",
"tokio",
@@ -454,9 +456,9 @@ dependencies = [
[[package]]
name = "axum-core"
version = "0.3.0-rc.2"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "473bd0762170028bb6b5068be9e97de2a9f0af3bf2084498d840498f47194d3d"
checksum = "79b8558f5a0581152dc94dcd289132a1d377494bdeafcd41869b3258e3e2ad92"
dependencies = [
"async-trait",
"bytes",
@@ -464,15 +466,16 @@ dependencies = [
"http",
"http-body",
"mime",
"rustversion",
"tower-layer",
"tower-service",
]
[[package]]
name = "axum-macros"
version = "0.3.0-rc.1"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "247a599903eb2e02abbaf2facc6396140df7af6dcc84e64ce3b71d117702fa22"
checksum = "e4df0fc33ada14a338b799002f7e8657711422b25d4e16afb032708d6b185621"
dependencies = [
"heck 0.4.0",
"proc-macro2",
@@ -1798,7 +1801,7 @@ version = "0.1.0"
dependencies = [
"api",
"async-trait",
"axum 0.6.0-rc.2",
"axum 0.6.1",
"axum-macros",
"axum-test-helper",
"backon",
@@ -3108,9 +3111,9 @@ checksum = "73cbba799671b762df5a175adf59ce145165747bb891505c43d09aefbbf38beb"
[[package]]
name = "matchit"
version = "0.6.0"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dfc802da7b1cf80aefffa0c7b2f77247c8b32206cc83c270b61264f5b360a80"
checksum = "b87248edafb776e59e6ee64a79086f65890d3510f2c656c000bf2a7e8a0aea40"
[[package]]
name = "matrixmultiply"
@@ -5460,6 +5463,15 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_path_to_error"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "184c643044780f7ceb59104cef98a5a6f12cb2288a7bc701ab93a362b49fd47d"
dependencies = [
"serde",
]
[[package]]
name = "serde_urlencoded"
version = "0.7.1"
@@ -5479,7 +5491,7 @@ dependencies = [
"aide",
"api",
"async-trait",
"axum 0.6.0-rc.2",
"axum 0.6.1",
"axum-macros",
"axum-test-helper",
"bytes",
@@ -6178,7 +6190,7 @@ name = "tests-integration"
version = "0.1.0"
dependencies = [
"api",
"axum 0.6.0-rc.2",
"axum 0.6.1",
"axum-test-helper",
"catalog",
"client",
@@ -6611,9 +6623,9 @@ dependencies = [
[[package]]
name = "tower-layer"
version = "0.3.1"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "343bc9466d3fe6b0f960ef45960509f84480bf4fd96f92901afe7ff3df9d3a62"
checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0"
[[package]]
name = "tower-service"

View File

@@ -11,8 +11,8 @@ python = ["dep:script"]
[dependencies]
api = { path = "../api" }
async-trait = "0.1"
axum = "0.6.0-rc.2"
axum-macros = "0.3.0-rc.1"
axum = "0.6"
axum-macros = "0.3"
backon = "0.2"
catalog = { path = "../catalog" }
common-base = { path = "../common/base" }

View File

@@ -5,11 +5,11 @@ edition = "2021"
license = "Apache-2.0"
[dependencies]
aide = { version = "0.6", features = ["axum"] }
aide = { version = "0.9", features = ["axum"] }
api = { path = "../api" }
async-trait = "0.1"
axum = "0.6.0-rc.2"
axum-macros = "0.3.0-rc.1"
axum = "0.6"
axum-macros = "0.3"
bytes = "1.2"
common-base = { path = "../common/base" }
common-catalog = { path = "../common/catalog" }

View File

@@ -344,55 +344,38 @@ impl HttpServer {
url: format!("/{}", HTTP_API_VERSION),
..OpenAPIServer::default()
}],
..OpenApi::default()
};
// TODO(LFC): Use released Axum.
// Axum version 0.6 introduces state within router, making router methods far more elegant
// to write. Though version 0.6 is rc, I think it's worth to upgrade.
// Prior to version 0.6, we only have a single "Extension" to share all query
// handlers amongst router methods. That requires us to pack all query handlers in a shared
// state, and check-then-get the desired query handler in different router methods, which
// is a lot of tedious work.
let sql_router = ApiRouter::with_state(ApiState {
sql_handler: self.sql_handler.clone(),
script_handler: self.script_handler.clone(),
})
.api_route(
"/sql",
apirouting::get_with(handler::sql, handler::sql_docs)
.post_with(handler::sql, handler::sql_docs),
)
.api_route("/scripts", apirouting::post(script::scripts))
.api_route("/run-script", apirouting::post(script::run_script))
.route("/private/api.json", apirouting::get(serve_api))
.route("/private/docs", apirouting::get(serve_docs))
.finish_api(&mut api)
.layer(Extension(Arc::new(api)));
let sql_router = self
.route_sql(ApiState {
sql_handler: self.sql_handler.clone(),
script_handler: self.script_handler.clone(),
})
.finish_api(&mut api)
.layer(Extension(Arc::new(api)));
let mut router = Router::new().nest(&format!("/{}", HTTP_API_VERSION), sql_router);
if let Some(opentsdb_handler) = self.opentsdb_handler.clone() {
let opentsdb_router = Router::with_state(opentsdb_handler)
.route("/api/put", routing::post(opentsdb::put));
router = router.nest(&format!("/{}/opentsdb", HTTP_API_VERSION), opentsdb_router);
router = router.nest(
&format!("/{}/opentsdb", HTTP_API_VERSION),
self.route_opentsdb(opentsdb_handler),
);
}
if let Some(influxdb_handler) = self.influxdb_handler.clone() {
let influxdb_router =
Router::with_state(influxdb_handler).route("/write", routing::post(influxdb_write));
router = router.nest(&format!("/{}/influxdb", HTTP_API_VERSION), influxdb_router);
router = router.nest(
&format!("/{}/influxdb", HTTP_API_VERSION),
self.route_influxdb(influxdb_handler),
);
}
if let Some(prom_handler) = self.prom_handler.clone() {
let prom_router = Router::with_state(prom_handler)
.route("/write", routing::post(prometheus::remote_write))
.route("/read", routing::post(prometheus::remote_read));
router = router.nest(&format!("/{}/prometheus", HTTP_API_VERSION), prom_router);
router = router.nest(
&format!("/{}/prometheus", HTTP_API_VERSION),
self.route_prom(prom_handler),
);
}
router = router.route("/metrics", routing::get(handler::metrics));
@@ -408,6 +391,39 @@ impl HttpServer {
.layer(middleware::from_fn(context::build_ctx)),
)
}
fn route_sql<S>(&self, api_state: ApiState) -> ApiRouter<S> {
ApiRouter::new()
.api_route(
"/sql",
apirouting::get_with(handler::sql, handler::sql_docs)
.post_with(handler::sql, handler::sql_docs),
)
.api_route("/scripts", apirouting::post(script::scripts))
.api_route("/run-script", apirouting::post(script::run_script))
.route("/private/api.json", apirouting::get(serve_api))
.route("/private/docs", apirouting::get(serve_docs))
.with_state(api_state)
}
fn route_prom<S>(&self, prom_handler: PrometheusProtocolHandlerRef) -> Router<S> {
Router::new()
.route("/write", routing::post(prometheus::remote_write))
.route("/read", routing::post(prometheus::remote_read))
.with_state(prom_handler)
}
fn route_influxdb<S>(&self, influxdb_handler: InfluxdbLineProtocolHandlerRef) -> Router<S> {
Router::new()
.route("/write", routing::post(influxdb_write))
.with_state(influxdb_handler)
}
fn route_opentsdb<S>(&self, opentsdb_handler: OpentsdbProtocolHandlerRef) -> Router<S> {
Router::new()
.route("/api/put", routing::post(opentsdb::put))
.with_state(opentsdb_handler)
}
}
#[async_trait]

View File

@@ -6,7 +6,7 @@ license = "Apache-2.0"
[dependencies]
api = { path = "../src/api" }
axum = "0.6.0-rc.2"
axum = "0.6"
axum-test-helper = { git = "https://github.com/sunng87/axum-test-helper.git", branch = "patch-1" }
catalog = { path = "../src/catalog" }
client = { path = "../src/client" }