| use crate::http::error::CustomError; |
| use crate::http::jwt::json_web_token::Identity; |
| use crate::http::shared::AppState; |
| use crate::streaming; |
| use crate::streaming::polling_consumer::PollingConsumer; |
| use crate::streaming::session::Session; |
| use crate::streaming::systems::messages::PollingArgs; |
| use axum::extract::{Path, Query, State}; |
| use axum::http::StatusCode; |
| use axum::routing::get; |
| use axum::{Extension, Json, Router}; |
| use iggy::identifier::Identifier; |
| use iggy::messages::append_messages::AppendMessages; |
| use iggy::messages::poll_messages::PollMessages; |
| use iggy::validatable::Validatable; |
| use std::sync::Arc; |
| |
| pub fn router(state: Arc<AppState>) -> Router { |
| Router::new() |
| .route( |
| "/streams/:stream_id/topics/:topic_id/messages", |
| get(poll_messages).post(send_messages), |
| ) |
| .with_state(state) |
| } |
| |
| async fn poll_messages( |
| State(state): State<Arc<AppState>>, |
| Extension(identity): Extension<Identity>, |
| Path((stream_id, topic_id)): Path<(String, String)>, |
| mut query: Query<PollMessages>, |
| ) -> Result<Json<streaming::models::messages::PolledMessages>, CustomError> { |
| query.stream_id = Identifier::from_str_value(&stream_id)?; |
| query.topic_id = Identifier::from_str_value(&topic_id)?; |
| query.validate()?; |
| |
| let partition_id = query.partition_id.unwrap_or(0); |
| let consumer_id = PollingConsumer::resolve_consumer_id(&query.consumer.id); |
| let consumer = PollingConsumer::Consumer(consumer_id, partition_id); |
| let system = state.system.read(); |
| let polled_messages = system |
| .poll_messages( |
| &Session::stateless(identity.user_id, identity.ip_address), |
| consumer, |
| &query.stream_id, |
| &query.topic_id, |
| PollingArgs::new(query.strategy, query.count, query.auto_commit), |
| ) |
| .await?; |
| Ok(Json(polled_messages)) |
| } |
| |
| async fn send_messages( |
| State(state): State<Arc<AppState>>, |
| Extension(identity): Extension<Identity>, |
| Path((stream_id, topic_id)): Path<(String, String)>, |
| Json(mut command): Json<AppendMessages>, |
| ) -> Result<StatusCode, CustomError> { |
| command.stream_id = Identifier::from_str_value(&stream_id)?; |
| command.topic_id = Identifier::from_str_value(&topic_id)?; |
| command.partitioning.length = command.partitioning.value.len() as u8; |
| command.validate()?; |
| |
| let system = state.system.read(); |
| system |
| .append_messages( |
| &Session::stateless(identity.user_id, identity.ip_address), |
| &command.stream_id, |
| &command.topic_id, |
| &command.partitioning, |
| &command.messages, |
| ) |
| .await?; |
| Ok(StatusCode::CREATED) |
| } |