68 lines
2.0 KiB
Rust
68 lines
2.0 KiB
Rust
use warp::{Filter, Reply, filters::BoxedFilter, http::Uri};
|
|
use crate::app::{
|
|
State,
|
|
web::{
|
|
CustomRejection,
|
|
template::events::EventsTemplate,
|
|
},
|
|
};
|
|
use std::{
|
|
collections::HashMap,
|
|
convert::Infallible,
|
|
sync::Arc,
|
|
};
|
|
|
|
pub fn events_routes(state: Arc<State>) -> BoxedFilter<(impl Reply, )> {
|
|
warp::get()
|
|
.and(
|
|
events_get(Arc::clone(&state))
|
|
)
|
|
.or(warp::post().and(
|
|
events_edit_post(Arc::clone(&state))
|
|
))
|
|
.boxed()
|
|
}
|
|
|
|
fn events_get(state: Arc<State>) -> BoxedFilter<(impl Reply, )> {
|
|
warp::path("events")
|
|
.and(warp::path::end())
|
|
.and_then(move || {
|
|
let state = Arc::clone(&state);
|
|
async move {
|
|
Result::<EventsTemplate, Infallible>::Ok(EventsTemplate {
|
|
events: state.config.read().await.events.clone(),
|
|
})
|
|
}
|
|
})
|
|
.boxed()
|
|
}
|
|
|
|
fn events_edit_post(state: Arc<State>) -> BoxedFilter<(impl Reply, )> {
|
|
warp::path("events")
|
|
.and(warp::path::end())
|
|
.and(warp::body::content_length_limit(1024 * 5))
|
|
.and(warp::body::form())
|
|
.and_then(move |mut form: HashMap<String, String>| {
|
|
let state = Arc::clone(&state);
|
|
async move {
|
|
let event_name = match form.remove("event") {
|
|
Some(name) => name,
|
|
None => return Err(warp::reject::custom(CustomRejection::InvalidForm)),
|
|
};
|
|
|
|
let script = match form.remove("script") {
|
|
Some(script) => script,
|
|
None => return Err(warp::reject::custom(CustomRejection::InvalidForm)),
|
|
};
|
|
|
|
match &*event_name {
|
|
"stream_status" => state.config.write().await.events.stream_status = Some(script),
|
|
_ => return Err(warp::reject::custom(CustomRejection::InvalidForm)),
|
|
}
|
|
|
|
Ok(warp::redirect(Uri::from_static("/events")))
|
|
}
|
|
})
|
|
.boxed()
|
|
}
|