Browse Source

Add an emoji list endpoint

votekiss
Charlotte Som 6 months ago
parent
commit
1f78f03b68
  1. 2
      Cargo.toml
  2. 21
      src/main.rs

2
Cargo.toml

@ -10,6 +10,6 @@ once_cell = "1.8.0"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.68"
tokio = { version = "1.12.0", features = ["full"] }
tokio-stream = "0.1.7"
tokio-stream = { version = "0.1.7", features = ["fs"] }
uuid = { version = "0.8.2", features = ["v4"] }
warp = "0.3.1"

21
src/main.rs

@ -6,9 +6,9 @@ use warb::{hyper::StatusCode, Filter, Reply};
use warp as warb; // i think it's funny
mod events;
mod utils;
mod viewer_connection;
mod watch_session;
mod utils;
use serde::Deserialize;
@ -30,6 +30,22 @@ struct SubscribeQuery {
colour: String,
}
async fn get_emoji_list() -> Result<impl warb::Reply, warb::Rejection> {
use tokio_stream::{wrappers::ReadDirStream, StreamExt};
let dir = tokio::fs::read_dir("frontend/emojis")
.await
.expect("Couldn't read emojis directory!");
let files = ReadDirStream::new(dir)
.filter_map(|r| r.ok())
.map(|e| e.file_name().to_string_lossy().to_string())
.collect::<Vec<_>>()
.await;
Ok(warb::reply::json(&files))
}
#[tokio::main]
async fn main() {
let start_session_route = warb::path!("start_session")
@ -46,6 +62,8 @@ async fn main() {
warb::reply::json(&json!({ "id": session_uuid.to_string(), "session": session_view }))
});
let get_emoji_route = warb::path!("emojis").and_then(get_emoji_list);
enum RequestedSession {
Session(Uuid, WatchSession),
Error(warb::reply::WithStatus<warb::reply::Json>),
@ -96,6 +114,7 @@ async fn main() {
let routes = start_session_route
.or(get_status_route)
.or(ws_subscribe_route)
.or(get_emoji_route)
.or(warb::path::end().and(warb::fs::file("frontend/index.html")))
.or(warb::fs::dir("frontend"));

Loading…
Cancel
Save