ready-to-test
This commit is contained in:
112
src/main.rs
112
src/main.rs
@@ -1,113 +1,81 @@
|
||||
use actix_web::{web, App, HttpResponse, HttpServer, Responder, web::Bytes};
|
||||
use actix_web::{web, App, HttpResponse, HttpServer, web::Bytes};
|
||||
use redis::{Client, AsyncCommands};
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use futures::StreamExt;
|
||||
use tokio::sync::broadcast;
|
||||
use actix_web::error::{ErrorUnauthorized, ErrorInternalServerError as ServerError};
|
||||
|
||||
mod data;
|
||||
|
||||
async fn sse_handler(
|
||||
token: web::Path<String>,
|
||||
redis: web::Data<Client>,
|
||||
) -> impl Responder {
|
||||
) -> Result<HttpResponse, actix_web::Error> {
|
||||
let listener_id = data::get_auth_id(&token).await.map_err(|e| {
|
||||
eprintln!("TOKEN check failed: {}", e);
|
||||
ErrorUnauthorized("Unauthorized")
|
||||
})?;
|
||||
|
||||
let author_id = match data::get_auth_id(&token).await {
|
||||
Ok(id) => id,
|
||||
Err(e) => {
|
||||
eprintln!("TOKEN check failed: {}", e);
|
||||
return HttpResponse::Unauthorized().finish();
|
||||
}
|
||||
};
|
||||
let mut con = redis.get_async_connection().await.map_err(|e| {
|
||||
eprintln!("Failed to get async connection: {}", e);
|
||||
ServerError("Internal Server Error")
|
||||
})?;
|
||||
|
||||
let mut con = match redis.get_async_connection().await {
|
||||
Ok(con) => con,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to get async connection: {}", e);
|
||||
return HttpResponse::InternalServerError().finish();
|
||||
}
|
||||
};
|
||||
con.sadd::<&str, &i32, usize>("authors-online", &listener_id).await.map_err(|e| {
|
||||
eprintln!("Failed to add author to online list: {}", e);
|
||||
ServerError("Internal Server Error")
|
||||
})?;
|
||||
|
||||
let _ = match con.sadd::<&str, &i32, usize>("authors-online", &author_id).await {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
eprintln!("Failed to add author to online list: {}", e);
|
||||
return HttpResponse::InternalServerError().finish();
|
||||
}
|
||||
};
|
||||
|
||||
let chats: Vec<String> = match con.smembers::<String, Vec<String>>(format!("chats_by_author/{}", author_id)).await {
|
||||
Ok(chats) => {
|
||||
if chats.is_empty() {
|
||||
match data::create_first_chat(author_id, &mut con).await {
|
||||
Ok(chat) => chat,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create first chat: {}", e);
|
||||
return HttpResponse::InternalServerError().finish();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
chats
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("Failed to get chats by author: {}", e);
|
||||
match data::create_first_chat(author_id, &mut con).await {
|
||||
Ok(chat) => chat,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create first chat: {}", e);
|
||||
return HttpResponse::InternalServerError().finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
let chats: Vec<String> = con.smembers::<String, Vec<String>>(format!("chats_by_author/{}", listener_id)).await.map_err(|e| {
|
||||
eprintln!("Failed to get chats by author: {}", e);
|
||||
ServerError("Internal Server Error")
|
||||
})?;
|
||||
|
||||
let (tx, mut rx) = broadcast::channel(100);
|
||||
let _handle = tokio::spawn(async move {
|
||||
let conn = redis.get_async_connection().await.expect("Failed to get async connection");
|
||||
let conn = redis.get_async_connection().await.unwrap();
|
||||
let mut pubsub = conn.into_pubsub();
|
||||
|
||||
pubsub.subscribe("new_follower").await.expect("Failed to subscribe to new_follower");
|
||||
pubsub.subscribe("new_shout").await.expect("Failed to subscribe to new_shout");
|
||||
pubsub.subscribe("new_reaction").await.expect("Failed to subscribe to new_reaction");
|
||||
pubsub.subscribe("new_follower").await.unwrap();
|
||||
pubsub.subscribe("new_shout").await.unwrap();
|
||||
pubsub.subscribe("new_reaction").await.unwrap();
|
||||
|
||||
for chat_id in &chats {
|
||||
let channel_name = format!("chat:{}", chat_id);
|
||||
pubsub
|
||||
.subscribe(channel_name.clone())
|
||||
.await
|
||||
.expect(&format!("Failed to subscribe to {}", channel_name));
|
||||
pubsub.subscribe(&channel_name).await.unwrap();
|
||||
}
|
||||
|
||||
while let Some(msg) = pubsub.on_message().next().await {
|
||||
let payload: HashMap<String, String> = msg.get_payload().expect("Failed to get payload");
|
||||
tx.clone().send(serde_json::to_string(&payload).expect("Failed to serialize payload")).expect("Failed to send payload");
|
||||
let payload: HashMap<String, String> = msg.get_payload().unwrap();
|
||||
if data::is_fitting(listener_id, payload.clone()).await.is_ok() {
|
||||
let _ = tx.send(serde_json::to_string(&payload).unwrap());
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
let server_event = match rx.recv().await {
|
||||
Ok(event) => event,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to receive server event: {}", e);
|
||||
return HttpResponse::InternalServerError().finish();
|
||||
}
|
||||
};
|
||||
let server_event = rx.recv().await.map_err(|e| {
|
||||
eprintln!("Failed to receive server event: {}", e);
|
||||
ServerError("Internal Server Error")
|
||||
})?;
|
||||
|
||||
let server_event_stream = futures::stream::once(async move { Ok::<_, actix_web::Error>(Bytes::from(server_event)) });
|
||||
|
||||
HttpResponse::Ok()
|
||||
Ok(HttpResponse::Ok()
|
||||
.append_header(("content-type", "text/event-stream"))
|
||||
.streaming(server_event_stream)
|
||||
.streaming(server_event_stream))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let redis_url = env::var("REDIS_URL").expect("REDIS_URL must be set");
|
||||
let client = redis::Client::open(redis_url).expect("Failed to open Redis client");
|
||||
let redis_url = env::var("REDIS_URL").unwrap_or_else(|_| String::from("redis://127.0.0.1/"));
|
||||
let client = redis::Client::open(redis_url).unwrap();
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(web::Data::new(client.clone()))
|
||||
.route("/presence/{token}", web::get().to(sse_handler))
|
||||
.route("/connect", web::get().to(sse_handler))
|
||||
.route("/disconnect", web::get().to(sse_handler))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
|
Reference in New Issue
Block a user