|
| 1 | +use rwf::controller::TurboStream; |
| 2 | +use rwf::model::migrate; |
| 3 | +use rwf::model::pool::Transaction; |
| 4 | +use rwf::model::prelude::*; |
| 5 | +use rwf::prelude::*; |
| 6 | +use std::collections::BTreeMap; |
| 7 | +use std::str::FromStr; |
| 8 | +use tokio::sync::Mutex; |
| 9 | + |
| 10 | +#[derive(Debug, Clone, Serialize, Deserialize, macros::Model)] |
| 11 | +pub struct AppLog { |
| 12 | + id: Option<i64>, |
| 13 | + ts: OffsetDateTime, |
| 14 | + data: String, |
| 15 | +} |
| 16 | +impl AppLog { |
| 17 | + pub fn q() -> Scope<Self> { |
| 18 | + Self::all().order(("id", "asc")) |
| 19 | + } |
| 20 | + pub async fn cursor(name: impl ToString, tx: &mut Transaction) -> ModelCursor<Self> { |
| 21 | + Self::q() |
| 22 | + .declare_cursor(name) |
| 23 | + .expect("Static Query is correct") |
| 24 | + .scroll() |
| 25 | + .hold() |
| 26 | + .create_model_cursor(tx) |
| 27 | + .await |
| 28 | + .expect("Database is able to serve the request") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Debug, Default)] |
| 33 | +struct TxModelController { |
| 34 | + cursors: Mutex<BTreeMap<SessionId, ModelCursor<AppLog>>>, |
| 35 | +} |
| 36 | +enum Direction { |
| 37 | + Next, |
| 38 | + Prev, |
| 39 | +} |
| 40 | +impl FromStr for Direction { |
| 41 | + type Err = String; |
| 42 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 43 | + match s.to_lowercase().as_str() { |
| 44 | + "next" => Ok(Self::Next), |
| 45 | + "prev" => Ok(Self::Prev), |
| 46 | + s => Err(format!("{} is not a valid direction", s)), |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | +static FETCH_CHUNK: i64 = 3; |
| 51 | +#[async_trait] |
| 52 | +impl Controller for TxModelController { |
| 53 | + async fn handle(&self, request: &Request) -> Result<Response, rwf::controller::Error> { |
| 54 | + let mut tx = Pool::begin().await?; |
| 55 | + let sess_id = request.session_id(); |
| 56 | + let cur_name = format!("sess_cur_{}", sess_id.to_string()); |
| 57 | + match request.query().get::<Direction>("direction") { |
| 58 | + None => { |
| 59 | + if let Some(old) = self.cursors.lock().await.remove(&sess_id) { |
| 60 | + tx.query_cached(old.close_stmt().as_str(), &[]).await?; |
| 61 | + } |
| 62 | + self.cursors |
| 63 | + .lock() |
| 64 | + .await |
| 65 | + .insert(sess_id, AppLog::cursor(cur_name, &mut tx).await); |
| 66 | + tx.commit().await?; |
| 67 | + render!(request, "templates/index.html") |
| 68 | + } |
| 69 | + Some(direction) => { |
| 70 | + let mut cur = match self.cursors.lock().await.remove(&sess_id) { |
| 71 | + None => return Ok(Response::new().redirect(request.path().path())), |
| 72 | + Some(cursor) => cursor, |
| 73 | + } |
| 74 | + .to_transaction_cursor(tx); |
| 75 | + let entries = match direction { |
| 76 | + Direction::Next => cur.fetch_optional(cur.fetch_stmt().forward(FETCH_CHUNK)), |
| 77 | + Direction::Prev => cur.fetch_optional(cur.fetch_stmt().backward(FETCH_CHUNK)), |
| 78 | + } |
| 79 | + .await? |
| 80 | + .unwrap_or(Vec::new()); |
| 81 | + let (cur, tx) = cur.decouple(); |
| 82 | + tx.commit().await?; |
| 83 | + self.cursors.lock().await.insert(sess_id, cur); |
| 84 | + Ok(Response::new().turbo_stream(&[turbo_stream!(request, "templates/entries.html", "entries", "entries" => entries).action("replace")])) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[tokio::main] |
| 91 | +async fn main() -> Result<(), rwf::http::Error> { |
| 92 | + migrate().await?; |
| 93 | + rwf::http::server::Server::new(vec![ |
| 94 | + route!("/" => TxModelController), |
| 95 | + route!("/ws" => TurboStream), |
| 96 | + ]) |
| 97 | + .launch() |
| 98 | + .await |
| 99 | +} |
0 commit comments