mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
refactor: rewrite remaining test server to Hyper 1.1 (#21708)
Ref https://github.com/denoland/deno/issues/21578
This commit is contained in:
parent
0efe438f7c
commit
4f4dcf5291
4 changed files with 442 additions and 300 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -5969,15 +5969,15 @@ dependencies = [
|
|||
"base64 0.21.5",
|
||||
"bytes",
|
||||
"console_static_text",
|
||||
"deno_unsync 0.3.0",
|
||||
"denokv_proto",
|
||||
"fastwebsockets",
|
||||
"flate2",
|
||||
"futures",
|
||||
"glob",
|
||||
"h2 0.3.22",
|
||||
"h2 0.4.0",
|
||||
"http 1.0.0",
|
||||
"hyper 0.14.27",
|
||||
"http-body-util",
|
||||
"hyper 1.1.0",
|
||||
"hyper-util",
|
||||
"lazy-regex",
|
||||
|
|
|
@ -19,15 +19,15 @@ async-stream = "0.3.3"
|
|||
base64.workspace = true
|
||||
bytes.workspace = true
|
||||
console_static_text.workspace = true
|
||||
deno_unsync = "0.3.0"
|
||||
denokv_proto.workspace = true
|
||||
fastwebsockets.workspace = true
|
||||
flate2 = { workspace = true, features = ["default"] }
|
||||
futures.workspace = true
|
||||
glob.workspace = true
|
||||
h2.workspace = true
|
||||
h2_04 = { package = "h2", version = "0.4" }
|
||||
http-body-util = "0.1"
|
||||
http_1 = { package = "http", version = "1.0" }
|
||||
hyper = { workspace = true, features = ["server", "http1", "http2", "runtime"] }
|
||||
hyper-util.workspace = true
|
||||
hyper1.workspace = true
|
||||
lazy-regex.workspace = true
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,37 +1,38 @@
|
|||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use hyper::server::Server;
|
||||
use hyper::service::make_service_fn;
|
||||
use hyper::service::service_fn;
|
||||
use hyper::Body;
|
||||
use hyper::Request;
|
||||
use hyper::Response;
|
||||
use hyper::StatusCode;
|
||||
use super::run_hyper1_server;
|
||||
use bytes::Bytes;
|
||||
use http_body_util::combinators::UnsyncBoxBody;
|
||||
use http_body_util::Empty;
|
||||
use http_body_util::Full;
|
||||
use hyper1::body::Incoming;
|
||||
use hyper1::Request;
|
||||
use hyper1::Response;
|
||||
use hyper1::StatusCode;
|
||||
use serde_json::json;
|
||||
use std::convert::Infallible;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
pub async fn registry_server(port: u16) {
|
||||
let registry_server_addr = SocketAddr::from(([127, 0, 0, 1], port));
|
||||
let registry_server_svc = make_service_fn(|_| async {
|
||||
Ok::<_, Infallible>(service_fn(registry_server_handler))
|
||||
});
|
||||
let registry_server =
|
||||
Server::bind(®istry_server_addr).serve(registry_server_svc);
|
||||
if let Err(e) = registry_server.await {
|
||||
eprintln!("Registry server error: {:?}", e);
|
||||
}
|
||||
|
||||
run_hyper1_server(
|
||||
registry_server_addr,
|
||||
registry_server_handler,
|
||||
"Registry server error",
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn registry_server_handler(
|
||||
req: Request<Body>,
|
||||
) -> Result<Response<Body>, hyper::http::Error> {
|
||||
req: Request<Incoming>,
|
||||
) -> Result<Response<UnsyncBoxBody<Bytes, Infallible>>, anyhow::Error> {
|
||||
let path = req.uri().path();
|
||||
|
||||
// TODO(bartlomieju): add a proper router here
|
||||
if path.starts_with("/api/scope/") {
|
||||
let body = serde_json::to_string_pretty(&json!({})).unwrap();
|
||||
let res = Response::new(Body::from(body));
|
||||
let res = Response::new(UnsyncBoxBody::new(Full::from(body)));
|
||||
return Ok(res);
|
||||
} else if path.starts_with("/api/scopes/") {
|
||||
let body = serde_json::to_string_pretty(&json!({
|
||||
|
@ -40,7 +41,7 @@ async fn registry_server_handler(
|
|||
"error": null
|
||||
}))
|
||||
.unwrap();
|
||||
let res = Response::new(Body::from(body));
|
||||
let res = Response::new(UnsyncBoxBody::new(Full::from(body)));
|
||||
return Ok(res);
|
||||
} else if path.starts_with("/api/publish_status/") {
|
||||
let body = serde_json::to_string_pretty(&json!({
|
||||
|
@ -49,11 +50,13 @@ async fn registry_server_handler(
|
|||
"error": null
|
||||
}))
|
||||
.unwrap();
|
||||
let res = Response::new(Body::from(body));
|
||||
let res = Response::new(UnsyncBoxBody::new(Full::from(body)));
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
Response::builder()
|
||||
let empty_body = UnsyncBoxBody::new(Empty::new());
|
||||
let res = Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(Body::empty())
|
||||
.body(empty_body)?;
|
||||
Ok(res)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue