| 39 | } |
| 40 | impl Peer { |
| 41 | pub fn start(root: &Path) -> std::io::Result<Self> { |
| 42 | let listener = TcpListener::bind("127.0.0.1:0")?; |
| 43 | listener.set_nonblocking(true)?; |
| 44 | let address = listener.local_addr()?.to_string(); |
| 45 | let store = Arc::new(Mutex::new(Store::default())); |
| 46 | let endpoint = Endpoint { |
| 47 | store: store.clone(), |
| 48 | root: root.to_path_buf(), |
| 49 | }; |
| 50 | let runtime = tokio::runtime::Builder::new_current_thread() |
| 51 | .enable_all() |
| 52 | .build()?; |
| 53 | let (tx, rx) = tokio::sync::oneshot::channel(); |
| 54 | let task = thread::spawn(move || { |
| 55 | runtime.block_on(async move { |
| 56 | let listener = |
| 57 | tokio::net::TcpListener::from_std(listener).expect("loopback listener"); |
| 58 | let router = Router::new() |
| 59 | .route( |
| 60 | "/workspaces/test/projects/acceptance/code", |
| 61 | get(pull).post(push), |
| 62 | ) |
| 63 | .layer(DefaultBodyLimit::max(64 * 1024 * 1024)) |
| 64 | .with_state(endpoint); |
| 65 | axum::serve(listener, router) |
| 66 | .with_graceful_shutdown(async { |
| 67 | let _ = rx.await; |
| 68 | }) |
| 69 | .await |
| 70 | .expect("acceptance endpoint"); |
| 71 | }) |
| 72 | }); |
| 73 | Ok(Self { |
| 74 | address, |
| 75 | store, |
| 76 | stop: Some(tx), |
| 77 | task: Some(task), |
| 78 | }) |
| 79 | } |
| 80 | pub fn url(&self) -> String { |
| 81 | format!( |
| 82 | "http://{}/workspaces/test/projects/acceptance/code", |