-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from pubky/feat/feed
Feat/feed
- Loading branch information
Showing
11 changed files
with
391 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//! Server events (Put and Delete entries) | ||
//! | ||
//! Useful as a realtime sync with Indexers until | ||
//! we implement more self-authenticated merkle data. | ||
use heed::{ | ||
types::{Bytes, Str}, | ||
Database, | ||
}; | ||
use postcard::{from_bytes, to_allocvec}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Event [Timestamp] base32 => Encoded event. | ||
pub type EventsTable = Database<Str, Bytes>; | ||
|
||
pub const EVENTS_TABLE: &str = "events"; | ||
|
||
#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq)] | ||
pub enum Event { | ||
Put(String), | ||
Delete(String), | ||
} | ||
|
||
impl Event { | ||
pub fn put(url: &str) -> Self { | ||
Self::Put(url.to_string()) | ||
} | ||
|
||
pub fn delete(url: &str) -> Self { | ||
Self::Delete(url.to_string()) | ||
} | ||
|
||
pub fn serialize(&self) -> Vec<u8> { | ||
to_allocvec(self).expect("Session::serialize") | ||
} | ||
|
||
pub fn deserialize(bytes: &[u8]) -> core::result::Result<Self, postcard::Error> { | ||
if bytes[0] > 1 { | ||
panic!("Unknown Event version"); | ||
} | ||
|
||
from_bytes(bytes) | ||
} | ||
|
||
pub fn url(&self) -> &str { | ||
match self { | ||
Event::Put(url) => url, | ||
Event::Delete(url) => url, | ||
} | ||
} | ||
|
||
pub fn operation(&self) -> &str { | ||
match self { | ||
Event::Put(_) => "PUT", | ||
Event::Delete(_) => "DEL", | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::collections::HashMap; | ||
|
||
use axum::{ | ||
body::Body, | ||
extract::{Query, State}, | ||
http::{header, Response, StatusCode}, | ||
response::IntoResponse, | ||
}; | ||
|
||
use crate::{ | ||
database::{tables::events::Event, MAX_LIST_LIMIT}, | ||
error::Result, | ||
server::AppState, | ||
}; | ||
|
||
pub async fn feed( | ||
State(state): State<AppState>, | ||
Query(params): Query<HashMap<String, String>>, | ||
) -> Result<impl IntoResponse> { | ||
let txn = state.db.env.read_txn()?; | ||
|
||
let limit = params | ||
.get("limit") | ||
.and_then(|l| l.parse::<u16>().ok()) | ||
.unwrap_or(MAX_LIST_LIMIT) | ||
.min(MAX_LIST_LIMIT); | ||
|
||
let mut cursor = params | ||
.get("cursor") | ||
.map(|c| c.as_str()) | ||
.unwrap_or("0000000000000"); | ||
|
||
// Guard against bad cursor | ||
if cursor.len() < 13 { | ||
cursor = "0000000000000" | ||
} | ||
|
||
let mut result: Vec<String> = vec![]; | ||
let mut next_cursor = cursor.to_string(); | ||
|
||
for _ in 0..limit { | ||
match state | ||
.db | ||
.tables | ||
.events | ||
.get_greater_than(&txn, &next_cursor)? | ||
{ | ||
Some((timestamp, event_bytes)) => { | ||
let event = Event::deserialize(event_bytes)?; | ||
|
||
let line = format!("{} {}", event.operation(), event.url()); | ||
next_cursor = timestamp.to_string(); | ||
|
||
result.push(line); | ||
} | ||
None => break, | ||
}; | ||
} | ||
|
||
if !result.is_empty() { | ||
result.push(format!("cursor: {next_cursor}")) | ||
} | ||
|
||
txn.commit()?; | ||
|
||
Ok(Response::builder() | ||
.status(StatusCode::OK) | ||
.header(header::CONTENT_TYPE, "text/plain") | ||
.body(Body::from(result.join("\n"))) | ||
.unwrap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.