summaryrefslogtreecommitdiff
path: root/test_util/src/servers/registry.rs
blob: 56a9e31b0e29a14ecfae353b06b29357dca3b555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 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 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(&registry_server_addr).serve(registry_server_svc);
  if let Err(e) = registry_server.await {
    eprintln!("Registry server error: {:?}", e);
  }
}

async fn registry_server_handler(
  req: Request<Body>,
) -> Result<Response<Body>, hyper::http::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));
    return Ok(res);
  } else if path.starts_with("/api/scopes/") {
    let body = serde_json::to_string_pretty(&json!({
      "id": "sdfwqer-sffg-qwerasdf",
      "status": "success",
      "error": null
    }))
    .unwrap();
    let res = Response::new(Body::from(body));
    return Ok(res);
  } else if path.starts_with("/api/publish_status/") {
    let body = serde_json::to_string_pretty(&json!({
      "id": "sdfwqer-qwer-qwerasdf",
      "status": "success",
      "error": null
    }))
    .unwrap();
    let res = Response::new(Body::from(body));
    return Ok(res);
  }

  Response::builder()
    .status(StatusCode::NOT_FOUND)
    .body(Body::empty())
}