|
| 1 | +mod stream_entity; |
| 2 | + |
| 3 | +use actix_web::{guard, web, App, HttpRequest, HttpResponse, HttpServer, Responder}; |
| 4 | +use eventsource_client as es; |
| 5 | +use futures::executor; |
| 6 | +use serde::{self, Deserialize, Serialize}; |
| 7 | +use std::collections::HashMap; |
| 8 | +use std::sync::{mpsc, Mutex}; |
| 9 | +use std::thread; |
| 10 | +use stream_entity::StreamEntity; |
| 11 | + |
| 12 | +#[derive(Serialize)] |
| 13 | +struct Status { |
| 14 | + capabilities: Vec<String>, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Deserialize, Debug)] |
| 18 | +#[serde(rename_all = "camelCase")] |
| 19 | +struct Config { |
| 20 | + /// The URL of an SSE endpoint created by the test harness. |
| 21 | + stream_url: String, |
| 22 | + /// The URL of a callback endpoint created by the test harness . |
| 23 | + callback_url: String, |
| 24 | + /// An optional integer specifying the initial reconnection delay parameter, in |
| 25 | + /// milliseconds. Not all SSE client implementations allow this to be configured, but the |
| 26 | + /// test harness will send a value anyway in an attempt to avoid having reconnection tests |
| 27 | + /// run unnecessarily slowly. |
| 28 | + initial_delay_ms: Option<u64>, |
| 29 | + /// A JSON object containing additional HTTP header names and string values. The SSE |
| 30 | + /// client should be configured to add these headers to its HTTP requests; the test harness |
| 31 | + /// will then verify that it receives those headers. The test harness will only set this |
| 32 | + /// property if the test service has the "headers" capability. Header names can be assumed |
| 33 | + /// to all be lowercase. |
| 34 | + headers: Option<HashMap<String, String>>, |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Serialize, Debug)] |
| 38 | +#[serde(tag = "kind")] |
| 39 | +enum EventType { |
| 40 | + #[serde(rename = "event")] |
| 41 | + Event { event: Event }, |
| 42 | + #[serde(rename = "error")] |
| 43 | + Error { error: String }, |
| 44 | +} |
| 45 | + |
| 46 | +impl From<es::Event> for EventType { |
| 47 | + fn from(event: es::Event) -> Self { |
| 48 | + Self::Event { |
| 49 | + event: Event { |
| 50 | + event_type: event.event_type.clone(), |
| 51 | + data: String::from_utf8(event.field("data").unwrap_or_default().to_vec()).unwrap(), |
| 52 | + id: String::from_utf8(event.field("id").unwrap_or_default().to_vec()).unwrap(), |
| 53 | + }, |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[derive(Serialize, Debug)] |
| 59 | +struct Event { |
| 60 | + #[serde(rename = "type")] |
| 61 | + event_type: String, |
| 62 | + data: String, |
| 63 | + id: String, |
| 64 | +} |
| 65 | + |
| 66 | +async fn status() -> impl Responder { |
| 67 | + web::Json(Status { |
| 68 | + capabilities: vec![ |
| 69 | + "comments".to_string(), |
| 70 | + "post".to_string(), |
| 71 | + "report".to_string(), |
| 72 | + "headers".to_string(), |
| 73 | + "last-event-id".to_string(), |
| 74 | + ], |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +async fn stream( |
| 79 | + req: HttpRequest, |
| 80 | + config: web::Json<Config>, |
| 81 | + app_state: web::Data<AppState>, |
| 82 | +) -> HttpResponse { |
| 83 | + let mut stream_entity = match StreamEntity::new(config.into_inner()) { |
| 84 | + Ok(se) => se, |
| 85 | + Err(e) => return HttpResponse::InternalServerError().body(e), |
| 86 | + }; |
| 87 | + |
| 88 | + let mut counter = match app_state.counter.lock() { |
| 89 | + Ok(c) => c, |
| 90 | + Err(_) => return HttpResponse::InternalServerError().body("Unable to retrieve counter"), |
| 91 | + }; |
| 92 | + |
| 93 | + let mut entities = match app_state.stream_entities.lock() { |
| 94 | + Ok(h) => h, |
| 95 | + Err(_) => return HttpResponse::InternalServerError().body("Unable to retrieve handles"), |
| 96 | + }; |
| 97 | + |
| 98 | + let stream_resource = match req.url_for("stop_stream", &[counter.to_string()]) { |
| 99 | + Ok(sr) => sr, |
| 100 | + Err(_) => { |
| 101 | + return HttpResponse::InternalServerError() |
| 102 | + .body("Unable to generate stream response URL") |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + *counter += 1; |
| 107 | + stream_entity.start(); |
| 108 | + entities.insert(*counter, stream_entity); |
| 109 | + |
| 110 | + let mut response = HttpResponse::Ok(); |
| 111 | + response.insert_header(("Location", stream_resource.to_string())); |
| 112 | + response.finish() |
| 113 | +} |
| 114 | + |
| 115 | +async fn shutdown(stopper: web::Data<mpsc::Sender<()>>) -> HttpResponse { |
| 116 | + match stopper.send(()) { |
| 117 | + Ok(_) => HttpResponse::NoContent().finish(), |
| 118 | + Err(_) => HttpResponse::InternalServerError().body("Unable to send shutdown signal"), |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +async fn stop_stream(req: HttpRequest, app_state: web::Data<AppState>) -> HttpResponse { |
| 123 | + if let Some(stream_id) = req.match_info().get("id") { |
| 124 | + let stream_id: u32 = match stream_id.parse() { |
| 125 | + Ok(id) => id, |
| 126 | + Err(_) => return HttpResponse::BadRequest().body("Unable to parse stream id"), |
| 127 | + }; |
| 128 | + |
| 129 | + match app_state.stream_entities.lock() { |
| 130 | + Ok(mut entities) => { |
| 131 | + if let Some(mut entity) = entities.remove(&stream_id) { |
| 132 | + entity.stop(); |
| 133 | + } |
| 134 | + } |
| 135 | + Err(_) => { |
| 136 | + return HttpResponse::InternalServerError().body("Unable to retrieve handles") |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + HttpResponse::NoContent().finish() |
| 141 | + } else { |
| 142 | + HttpResponse::BadRequest().body("No stream id was provided in the URL") |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +struct AppState { |
| 147 | + counter: Mutex<u32>, |
| 148 | + stream_entities: Mutex<HashMap<u32, StreamEntity>>, |
| 149 | +} |
| 150 | + |
| 151 | +#[actix_web::main] |
| 152 | +async fn main() -> std::io::Result<()> { |
| 153 | + env_logger::init(); |
| 154 | + |
| 155 | + let (tx, rx) = mpsc::channel::<()>(); |
| 156 | + |
| 157 | + let state = web::Data::new(AppState { |
| 158 | + counter: Mutex::new(0), |
| 159 | + stream_entities: Mutex::new(HashMap::new()), |
| 160 | + }); |
| 161 | + |
| 162 | + let server = HttpServer::new(move || { |
| 163 | + App::new() |
| 164 | + .app_data(web::Data::new(tx.clone())) |
| 165 | + .app_data(state.clone()) |
| 166 | + .route("/", web::get().to(status)) |
| 167 | + .route("/", web::post().to(stream)) |
| 168 | + .route("/", web::delete().to(shutdown)) |
| 169 | + .service( |
| 170 | + web::resource("/stream/{id}") |
| 171 | + .name("stop_stream") |
| 172 | + .guard(guard::Delete()) |
| 173 | + .to(stop_stream), |
| 174 | + ) |
| 175 | + }) |
| 176 | + .bind("127.0.0.1:8080")? |
| 177 | + .run(); |
| 178 | + |
| 179 | + let handle = server.handle(); |
| 180 | + |
| 181 | + thread::spawn(move || { |
| 182 | + // wait for shutdown signal |
| 183 | + if let Ok(()) = rx.recv() { |
| 184 | + executor::block_on(handle.stop(true)) |
| 185 | + } |
| 186 | + }); |
| 187 | + |
| 188 | + // run server |
| 189 | + server.await |
| 190 | +} |
0 commit comments