quoter/src/main.rs

89 lines
2.7 KiB
Rust
Raw Normal View History

2023-09-28 10:12:07 +00:00
use serde_json::Value;
use sse_actix_web::{broadcast, Broadcaster};
2023-09-27 23:08:48 +00:00
use std::collections::HashMap;
use std::env;
2023-09-28 10:12:07 +00:00
use tokio::sync::broadcast::Receiver;
2023-09-27 23:08:48 +00:00
2023-09-28 10:12:07 +00:00
#[derive(Debug, Deserialize)]
struct Payload {
reader_id: i32,
2023-09-27 23:08:48 +00:00
}
2023-09-28 10:12:07 +00:00
async fn get_author_id(token: &str) -> Result<i32, Box<dyn std::error::Error>> {
2023-09-27 23:08:48 +00:00
let client = Client::new();
2023-09-28 10:12:07 +00:00
let gql = r#"mutation { getSession { user { id } } }"#;
let api_base = env::var("API_BASE")?;
let response = client
.post(api_base)
2023-09-27 23:08:48 +00:00
.bearer_auth(token)
2023-09-28 10:12:07 +00:00
.body(gql)
2023-09-27 23:08:48 +00:00
.send()
.await?;
2023-09-28 10:12:07 +00:00
let response_body: Value = response
.json()
.await
.map_err(|e| format!("Failed to parse response body: {}", e))?;
let id = response_body["data"]["getSession"]["user"]["id"]
.as_i64()
.ok_or("Failed to get user id by token")? as i32;
Ok(id)
2023-09-27 23:08:48 +00:00
}
2023-09-28 10:12:07 +00:00
async fn sse_handler(
broadcaster: web::Data<Broadcaster>,
token: web::Path<String>,
rx: web::Data<Receiver<String>>,
) -> impl Responder {
let author_id = match get_author_id(&token).await {
2023-09-27 23:08:48 +00:00
Ok(id) => id,
Err(e) => {
eprintln!("Failed to validate token: {}", e);
return actix_web::HttpResponse::Unauthorized().finish();
}
};
2023-09-28 10:12:07 +00:00
let mut stream = rx.into_inner().into_stream();
while let Some(Ok(payload)) = stream.next().await {
let payload: Payload = serde_json::from_str(&payload).unwrap();
if payload.reader_id == author_id {
broadcast(&broadcaster, &payload);
}
}
"Subscribed to SSE"
2023-09-27 23:08:48 +00:00
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
2023-09-28 10:12:07 +00:00
let broadcaster = Broadcaster::new();
// Create a single Redis Pub/Sub connection and broadcast channel
let (tx, rx) = broadcast::channel(100);
let redis_url = env::var("REDIS_URL").unwrap();
let _handle = tokio::spawn(async move {
let client = redis::Client::open(redis_url).unwrap();
let mut conn = client.get_async_connection().await.unwrap();
let mut pubsub = conn.into_pubsub();
pubsub.subscribe("new_follower").await.unwrap();
pubsub.subscribe("new_reaction").await.unwrap();
pubsub.subscribe("new_shout").await.unwrap();
pubsub.subscribe("new_approval").await.unwrap();
while let Some(msg) = pubsub.on_message().next().await {
let payload: HashMap<String, String> = msg.get_payload().unwrap();
tx.send(serde_json::to_string(&payload).unwrap()).unwrap();
}
});
2023-09-27 23:08:48 +00:00
HttpServer::new(move || {
App::new()
2023-09-28 10:12:07 +00:00
.data(broadcaster.clone())
.data(rx.clone())
2023-09-27 23:08:48 +00:00
.route("/sse/{token}", web::get().to(sse_handler))
})
.bind("127.0.0.1:8080")?
.run()
.await
}