Hastic standalone https://hastic.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

91 lines
2.6 KiB

pub mod filters {
use super::handlers;
use super::models::{Db, ListOptions};
use warp::Filter;
/// The 4 REST API filters combined.
pub fn filters(
db: Db,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
list(db.clone()).or(create(db.clone()))
// .or(update(db.clone()))
// .or(delete(db.clone()))
}
/// GET /segments?from=3&to=5
pub fn list(
db: Db,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path!("segments")
.and(warp::get())
.and(warp::query::<ListOptions>())
.and(with_db(db))
.and_then(handlers::list)
}
/// POST /segments with JSON body
pub fn create(
db: Db,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path!("segments")
.and(warp::post())
.and(warp::body::json())
.and(with_db(db))
.and_then(handlers::create)
}
fn with_db(db: Db) -> impl Filter<Extract = (Db,), Error = std::convert::Infallible> + Clone {
warp::any().map(move || db.clone())
}
}
mod handlers {
use hastic::services::data_service;
use super::models::{ Db, ListOptions, CreateResponse };
use crate::api::BadQuery;
use crate::api::API;
pub async fn list(opts: ListOptions, db: Db) -> Result<impl warp::Reply, warp::Rejection> {
// Just return a JSON array of todos, applying the limit and offset.
match db.read().get_segments(opts.from, opts.to) {
Ok(segments) => Ok(API::json(&segments)),
Err(e) => Err(warp::reject::custom(BadQuery)),
}
}
pub async fn create(
segment: data_service::Segment,
db: Db,
) -> Result<impl warp::Reply, warp::Rejection> {
// Just return a JSON array of todos, applying the limit and offset.
match db.write().insert_segment(&segment) {
Ok(id) => Ok(API::json(&CreateResponse { id })),
Err(e) => {
println!("{:?}", e);
Err(warp::reject::custom(BadQuery))
}
}
}
}
mod models {
use hastic::services::data_service::{self, SegmentId};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
pub type Db = Arc<RwLock<data_service::DataService>>;
// The query parameters for list_todos.
#[derive(Debug, Deserialize)]
pub struct ListOptions {
pub from: u64,
pub to: u64,
}
#[derive(Debug, Serialize )]
pub struct CreateResponse {
pub id: SegmentId,
}
}