server: add notification handlers for refreshing mail and news
This commit is contained in:
parent
221b4f10df
commit
94f1e84857
@ -8,10 +8,10 @@ use async_graphql_axum::{GraphQL, GraphQLSubscription};
|
|||||||
//allows to extract the IP of connecting user
|
//allows to extract the IP of connecting user
|
||||||
use axum::extract::connect_info::ConnectInfo;
|
use axum::extract::connect_info::ConnectInfo;
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{self, ws::WebSocketUpgrade, State},
|
extract::{self, ws::WebSocketUpgrade, Query, State},
|
||||||
http::{header, StatusCode},
|
http::{header, StatusCode},
|
||||||
response::{self, IntoResponse, Response},
|
response::{self, IntoResponse, Response},
|
||||||
routing::{any, get},
|
routing::{any, get, post},
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use cacher::FilesystemCacher;
|
use cacher::FilesystemCacher;
|
||||||
@ -25,6 +25,7 @@ use letterbox_server::{
|
|||||||
ws::ConnectionTracker,
|
ws::ConnectionTracker,
|
||||||
};
|
};
|
||||||
use letterbox_shared::WebsocketMessage;
|
use letterbox_shared::WebsocketMessage;
|
||||||
|
use serde::Deserialize;
|
||||||
use sqlx::postgres::PgPool;
|
use sqlx::postgres::PgPool;
|
||||||
use tokio::{net::TcpListener, sync::Mutex};
|
use tokio::{net::TcpListener, sync::Mutex};
|
||||||
use tower_http::trace::{DefaultMakeSpan, TraceLayer};
|
use tower_http::trace::{DefaultMakeSpan, TraceLayer};
|
||||||
@ -159,18 +160,29 @@ async fn start_ws(
|
|||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
ws.on_upgrade(async move |socket| connection_tracker.lock().await.add_peer(socket, addr).await)
|
ws.on_upgrade(async move |socket| connection_tracker.lock().await.add_peer(socket, addr).await)
|
||||||
}
|
}
|
||||||
#[axum_macros::debug_handler]
|
|
||||||
async fn test_handler(
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct NotificationParams {
|
||||||
|
delay_ms: Option<u64>,
|
||||||
|
}
|
||||||
|
async fn send_refresh_websocket_handler(
|
||||||
State(AppState {
|
State(AppState {
|
||||||
connection_tracker, ..
|
connection_tracker, ..
|
||||||
}): State<AppState>,
|
}): State<AppState>,
|
||||||
|
params: Query<NotificationParams>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
|
info!("send_refresh_websocket_handler params {params:?}");
|
||||||
|
if let Some(delay_ms) = params.delay_ms {
|
||||||
|
let delay = Duration::from_millis(delay_ms);
|
||||||
|
info!("sleeping {delay:?}");
|
||||||
|
tokio::time::sleep(delay).await;
|
||||||
|
}
|
||||||
connection_tracker
|
connection_tracker
|
||||||
.lock()
|
.lock()
|
||||||
.await
|
.await
|
||||||
.send_message_all(WebsocketMessage::RefreshMessages)
|
.send_message_all(WebsocketMessage::RefreshMessages)
|
||||||
.await;
|
.await;
|
||||||
"test triggered"
|
"refresh triggered"
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn watch_new(
|
async fn watch_new(
|
||||||
@ -240,23 +252,26 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let poll_time = Duration::from_secs(10);
|
let poll_time = Duration::from_secs(10);
|
||||||
let _h = tokio::spawn(watch_new(nm.clone(), pool, ct, poll_time));
|
let _h = tokio::spawn(watch_new(nm.clone(), pool, ct, poll_time));
|
||||||
|
|
||||||
let app = Router::new()
|
let api_routes = Router::new()
|
||||||
.route("/test", get(test_handler))
|
|
||||||
.route(
|
.route(
|
||||||
"/api/download/attachment/{id}/{idx}/{*rest}",
|
"/download/attachment/{id}/{idx}/{*rest}",
|
||||||
get(download_attachment),
|
get(download_attachment),
|
||||||
)
|
)
|
||||||
|
.route("/view/attachment/{id}/{idx}/{*rest}", get(view_attachment))
|
||||||
|
.route("/cid/{id}/{cid}", get(view_cid))
|
||||||
|
.route("/ws", any(start_ws))
|
||||||
|
.route_service("/graphql/ws", GraphQLSubscription::new(schema.clone()))
|
||||||
.route(
|
.route(
|
||||||
"/api/view/attachment/{id}/{idx}/{*rest}",
|
"/graphql/",
|
||||||
get(view_attachment),
|
|
||||||
)
|
|
||||||
.route("/api/cid/{id}/{cid}", get(view_cid))
|
|
||||||
.route("/api/ws", any(start_ws))
|
|
||||||
.route_service("/api/graphql/ws", GraphQLSubscription::new(schema.clone()))
|
|
||||||
.route(
|
|
||||||
"/api/graphql/",
|
|
||||||
get(graphiql).post_service(GraphQL::new(schema.clone())),
|
get(graphiql).post_service(GraphQL::new(schema.clone())),
|
||||||
)
|
);
|
||||||
|
|
||||||
|
let notification_routes = Router::new()
|
||||||
|
.route("/mail", post(send_refresh_websocket_handler))
|
||||||
|
.route("/news", post(send_refresh_websocket_handler));
|
||||||
|
let app = Router::new()
|
||||||
|
.nest("/api", api_routes)
|
||||||
|
.nest("/notification", notification_routes)
|
||||||
.with_state(AppState {
|
.with_state(AppState {
|
||||||
nm,
|
nm,
|
||||||
connection_tracker,
|
connection_tracker,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user