surrealpatch/src/net/key.rs

412 lines
13 KiB
Rust
Raw Normal View History

use crate::cli::CF;
use crate::dbs::DB;
use crate::err::Error;
2022-02-09 15:55:04 +00:00
use crate::net::output;
use crate::net::session;
use bytes::Bytes;
2020-06-29 15:36:01 +00:00
use serde::Deserialize;
2021-03-29 15:43:37 +00:00
use std::str;
use surrealdb::sql::Value;
use surrealdb::Session;
2020-06-29 15:36:01 +00:00
use warp::path;
use warp::Filter;
const MAX: u64 = 1024 * 16; // 16 KiB
2020-06-29 15:36:01 +00:00
#[derive(Default, Deserialize, Debug, Clone)]
struct Query {
2020-06-29 15:36:01 +00:00
pub limit: Option<String>,
pub start: Option<String>,
}
pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
// ------------------------------
// Routes for OPTIONS
// ------------------------------
2020-06-29 15:36:01 +00:00
let base = warp::path("key");
// Set opts method
let opts = base.and(warp::options()).map(warp::reply);
// ------------------------------
// Routes for a table
// ------------------------------
// Set select method
let select = warp::any()
.and(warp::get())
.and(session::build())
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
.and(path!("key" / String).and(warp::path::end()))
.and(warp::query())
.and_then(select_all);
2020-06-29 15:36:01 +00:00
// Set create method
let create = warp::any()
2020-06-29 15:36:01 +00:00
.and(warp::post())
.and(session::build())
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
.and(path!("key" / String).and(warp::path::end()))
.and(warp::body::content_length_limit(MAX))
2020-06-29 15:36:01 +00:00
.and(warp::body::bytes())
.and_then(create_all);
// Set delete method
let delete = warp::any()
.and(warp::delete())
.and(session::build())
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
.and(path!("key" / String).and(warp::path::end()))
.and_then(delete_all);
2020-06-29 15:36:01 +00:00
// Specify route
let all = select.or(create).or(delete);
// ------------------------------
// Routes for a thing
// ------------------------------
// Set select method
let select = warp::any()
.and(warp::get())
.and(session::build())
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
.and(path!("key" / String / String).and(warp::path::end()))
.and_then(select_one);
2020-06-29 15:36:01 +00:00
// Set create method
let create = warp::any()
2020-06-29 15:36:01 +00:00
.and(warp::post())
.and(session::build())
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
.and(path!("key" / String / String).and(warp::path::end()))
.and(warp::body::content_length_limit(MAX))
2020-06-29 15:36:01 +00:00
.and(warp::body::bytes())
.and_then(create_one);
// Set update method
let update = warp::any()
2020-06-29 15:36:01 +00:00
.and(warp::put())
.and(session::build())
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
.and(path!("key" / String / String).and(warp::path::end()))
.and(warp::body::content_length_limit(MAX))
2020-06-29 15:36:01 +00:00
.and(warp::body::bytes())
.and_then(update_one);
// Set modify method
let modify = warp::any()
2020-06-29 15:36:01 +00:00
.and(warp::patch())
.and(session::build())
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
.and(path!("key" / String / String).and(warp::path::end()))
.and(warp::body::content_length_limit(MAX))
2020-06-29 15:36:01 +00:00
.and(warp::body::bytes())
.and_then(modify_one);
// Set delete method
let delete = warp::any()
.and(warp::delete())
.and(session::build())
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
.and(path!("key" / String / String).and(warp::path::end()))
.and_then(delete_one);
2020-06-29 15:36:01 +00:00
// Specify route
let one = select.or(create).or(update).or(modify).or(delete);
// ------------------------------
// All routes
// ------------------------------
// Specify route
opts.or(all).or(one)
2020-06-29 15:36:01 +00:00
}
// ------------------------------
// Routes for a table
// ------------------------------
async fn select_all(
session: Session,
2022-01-14 17:13:44 +00:00
output: String,
table: String,
query: Query,
) -> Result<impl warp::Reply, warp::Rejection> {
// Get the datastore reference
let db = DB.get().unwrap();
// Get local copy of options
let opt = CF.get().unwrap();
// Specify the request statement
2020-06-29 15:36:01 +00:00
let sql = format!(
2021-03-29 15:43:37 +00:00
"SELECT * FROM type::table($table) LIMIT {l} START {s}",
l = query.limit.unwrap_or_else(|| String::from("100")),
s = query.start.unwrap_or_else(|| String::from("0")),
2020-06-29 15:36:01 +00:00
);
// Specify the request variables
2022-03-07 18:14:37 +00:00
let vars = map! {
2022-02-09 15:55:04 +00:00
String::from("table") => Value::from(table),
};
// Execute the query and return the result
match db.execute(sql.as_str(), &session, Some(vars), opt.strict).await {
Ok(ref res) => match output.as_ref() {
"application/json" => Ok(output::json(res)),
"application/cbor" => Ok(output::cbor(res)),
"application/msgpack" => Ok(output::pack(&res)),
// An incorrect content-type was requested
_ => Err(warp::reject::custom(Error::InvalidType)),
2022-01-14 17:13:44 +00:00
},
// There was an error when executing the query
Err(err) => Err(warp::reject::custom(Error::from(err))),
2022-01-14 17:13:44 +00:00
}
2020-06-29 15:36:01 +00:00
}
async fn create_all(
session: Session,
2022-01-14 17:13:44 +00:00
output: String,
2020-06-29 15:36:01 +00:00
table: String,
body: Bytes,
2020-06-29 15:36:01 +00:00
) -> Result<impl warp::Reply, warp::Rejection> {
// Get the datastore reference
let db = DB.get().unwrap();
// Get local copy of options
let opt = CF.get().unwrap();
// Convert the HTTP request body
let data = str::from_utf8(&body).unwrap();
// Parse the request body as JSON
match surrealdb::sql::json(data) {
Ok(data) => {
// Specify the request statement
let sql = "CREATE type::table($table) CONTENT $data";
// Specify the request variables
2022-03-07 18:14:37 +00:00
let vars = map! {
2022-02-13 23:37:30 +00:00
String::from("table") => Value::from(table),
String::from("data") => data,
2022-02-13 23:37:30 +00:00
};
// Execute the query and return the result
match db.execute(sql, &session, Some(vars), opt.strict).await {
2022-01-14 17:13:44 +00:00
Ok(res) => match output.as_ref() {
"application/json" => Ok(output::json(&res)),
"application/cbor" => Ok(output::cbor(&res)),
"application/msgpack" => Ok(output::pack(&res)),
// An incorrect content-type was requested
_ => Err(warp::reject::custom(Error::InvalidType)),
2022-01-14 17:13:44 +00:00
},
// There was an error when executing the query
Err(err) => Err(warp::reject::custom(Error::from(err))),
2022-01-14 17:13:44 +00:00
}
}
2022-03-25 20:31:45 +00:00
Err(_) => Err(warp::reject::custom(Error::Request)),
}
2020-06-29 15:36:01 +00:00
}
2022-01-14 17:13:44 +00:00
async fn delete_all(
session: Session,
output: String,
table: String,
) -> Result<impl warp::Reply, warp::Rejection> {
// Get the datastore reference
let db = DB.get().unwrap();
// Get local copy of options
let opt = CF.get().unwrap();
// Specify the request statement
2021-03-29 15:43:37 +00:00
let sql = "DELETE type::table($table)";
// Specify the request variables
2022-03-07 18:14:37 +00:00
let vars = map! {
2022-02-13 23:37:30 +00:00
String::from("table") => Value::from(table),
};
// Execute the query and return the result
match db.execute(sql, &session, Some(vars), opt.strict).await {
2022-01-14 17:13:44 +00:00
Ok(res) => match output.as_ref() {
"application/json" => Ok(output::json(&res)),
"application/cbor" => Ok(output::cbor(&res)),
"application/msgpack" => Ok(output::pack(&res)),
// An incorrect content-type was requested
_ => Err(warp::reject::custom(Error::InvalidType)),
2022-01-14 17:13:44 +00:00
},
// There was an error when executing the query
Err(err) => Err(warp::reject::custom(Error::from(err))),
2022-01-14 17:13:44 +00:00
}
2020-06-29 15:36:01 +00:00
}
// ------------------------------
// Routes for a thing
// ------------------------------
async fn select_one(
session: Session,
2022-01-14 17:13:44 +00:00
output: String,
table: String,
id: String,
) -> Result<impl warp::Reply, warp::Rejection> {
// Get the datastore reference
let db = DB.get().unwrap();
// Get local copy of options
let opt = CF.get().unwrap();
// Specify the request statement
2021-03-29 15:43:37 +00:00
let sql = "SELECT * FROM type::thing($table, $id)";
// Specify the request variables
2022-03-07 18:14:37 +00:00
let vars = map! {
2022-02-13 23:37:30 +00:00
String::from("table") => Value::from(table),
String::from("id") => Value::from(id),
};
// Execute the query and return the result
match db.execute(sql, &session, Some(vars), opt.strict).await {
2022-01-14 17:13:44 +00:00
Ok(res) => match output.as_ref() {
"application/json" => Ok(output::json(&res)),
"application/cbor" => Ok(output::cbor(&res)),
"application/msgpack" => Ok(output::pack(&res)),
// An incorrect content-type was requested
_ => Err(warp::reject::custom(Error::InvalidType)),
2022-01-14 17:13:44 +00:00
},
// There was an error when executing the query
Err(err) => Err(warp::reject::custom(Error::from(err))),
2022-01-14 17:13:44 +00:00
}
2020-06-29 15:36:01 +00:00
}
async fn create_one(
session: Session,
2022-01-14 17:13:44 +00:00
output: String,
2020-06-29 15:36:01 +00:00
table: String,
id: String,
body: Bytes,
2020-06-29 15:36:01 +00:00
) -> Result<impl warp::Reply, warp::Rejection> {
// Get the datastore reference
let db = DB.get().unwrap();
// Get local copy of options
let opt = CF.get().unwrap();
// Convert the HTTP request body
let data = str::from_utf8(&body).unwrap();
// Parse the request body as JSON
match surrealdb::sql::json(data) {
Ok(data) => {
// Specify the request statement
let sql = "CREATE type::thing($table, $id) CONTENT $data";
// Specify the request variables
2022-03-07 18:14:37 +00:00
let vars = map! {
2022-02-13 23:37:30 +00:00
String::from("table") => Value::from(table),
String::from("id") => Value::from(id),
String::from("data") => data,
2022-02-13 23:37:30 +00:00
};
// Execute the query and return the result
match db.execute(sql, &session, Some(vars), opt.strict).await {
2022-01-14 17:13:44 +00:00
Ok(res) => match output.as_ref() {
"application/json" => Ok(output::json(&res)),
"application/cbor" => Ok(output::cbor(&res)),
"application/msgpack" => Ok(output::pack(&res)),
// An incorrect content-type was requested
_ => Err(warp::reject::custom(Error::InvalidType)),
2022-01-14 17:13:44 +00:00
},
// There was an error when executing the query
Err(err) => Err(warp::reject::custom(Error::from(err))),
2022-01-14 17:13:44 +00:00
}
}
2022-03-25 20:31:45 +00:00
Err(_) => Err(warp::reject::custom(Error::Request)),
}
2020-06-29 15:36:01 +00:00
}
async fn update_one(
session: Session,
2022-01-14 17:13:44 +00:00
output: String,
2020-06-29 15:36:01 +00:00
table: String,
id: String,
body: Bytes,
2020-06-29 15:36:01 +00:00
) -> Result<impl warp::Reply, warp::Rejection> {
// Get the datastore reference
let db = DB.get().unwrap();
// Get local copy of options
let opt = CF.get().unwrap();
// Convert the HTTP request body
let data = str::from_utf8(&body).unwrap();
// Parse the request body as JSON
match surrealdb::sql::json(data) {
Ok(data) => {
// Specify the request statement
let sql = "UPDATE type::thing($table, $id) CONTENT $data";
// Specify the request variables
2022-03-07 18:14:37 +00:00
let vars = map! {
2022-02-13 23:37:30 +00:00
String::from("table") => Value::from(table),
String::from("id") => Value::from(id),
String::from("data") => data,
2022-02-13 23:37:30 +00:00
};
// Execute the query and return the result
match db.execute(sql, &session, Some(vars), opt.strict).await {
2022-01-14 17:13:44 +00:00
Ok(res) => match output.as_ref() {
"application/json" => Ok(output::json(&res)),
"application/cbor" => Ok(output::cbor(&res)),
"application/msgpack" => Ok(output::pack(&res)),
// An incorrect content-type was requested
_ => Err(warp::reject::custom(Error::InvalidType)),
2022-01-14 17:13:44 +00:00
},
// There was an error when executing the query
Err(err) => Err(warp::reject::custom(Error::from(err))),
2022-01-14 17:13:44 +00:00
}
}
2022-03-25 20:31:45 +00:00
Err(_) => Err(warp::reject::custom(Error::Request)),
}
2020-06-29 15:36:01 +00:00
}
async fn modify_one(
session: Session,
2022-01-14 17:13:44 +00:00
output: String,
2020-06-29 15:36:01 +00:00
table: String,
id: String,
body: Bytes,
2020-06-29 15:36:01 +00:00
) -> Result<impl warp::Reply, warp::Rejection> {
// Get the datastore reference
let db = DB.get().unwrap();
// Get local copy of options
let opt = CF.get().unwrap();
// Convert the HTTP request body
let data = str::from_utf8(&body).unwrap();
// Parse the request body as JSON
match surrealdb::sql::json(data) {
Ok(data) => {
// Specify the request statement
let sql = "UPDATE type::thing($table, $id) MERGE $data";
// Specify the request variables
2022-03-07 18:14:37 +00:00
let vars = map! {
2022-02-13 23:37:30 +00:00
String::from("table") => Value::from(table),
String::from("id") => Value::from(id),
String::from("data") => data,
2022-02-13 23:37:30 +00:00
};
// Execute the query and return the result
match db.execute(sql, &session, Some(vars), opt.strict).await {
2022-01-14 17:13:44 +00:00
Ok(res) => match output.as_ref() {
"application/json" => Ok(output::json(&res)),
"application/cbor" => Ok(output::cbor(&res)),
"application/msgpack" => Ok(output::pack(&res)),
// An incorrect content-type was requested
_ => Err(warp::reject::custom(Error::InvalidType)),
2022-01-14 17:13:44 +00:00
},
// There was an error when executing the query
Err(err) => Err(warp::reject::custom(Error::from(err))),
2022-01-14 17:13:44 +00:00
}
}
2022-03-25 20:31:45 +00:00
Err(_) => Err(warp::reject::custom(Error::Request)),
}
2020-06-29 15:36:01 +00:00
}
async fn delete_one(
session: Session,
2022-01-14 17:13:44 +00:00
output: String,
table: String,
id: String,
) -> Result<impl warp::Reply, warp::Rejection> {
// Get the datastore reference
let db = DB.get().unwrap();
// Get local copy of options
let opt = CF.get().unwrap();
// Specify the request statement
2021-03-29 15:43:37 +00:00
let sql = "DELETE type::thing($table, $id)";
// Specify the request variables
2022-03-07 18:14:37 +00:00
let vars = map! {
2022-02-13 23:37:30 +00:00
String::from("table") => Value::from(table),
String::from("id") => Value::from(id),
};
// Execute the query and return the result
match db.execute(sql, &session, Some(vars), opt.strict).await {
2022-01-14 17:13:44 +00:00
Ok(res) => match output.as_ref() {
"application/json" => Ok(output::json(&res)),
"application/cbor" => Ok(output::cbor(&res)),
"application/msgpack" => Ok(output::pack(&res)),
// An incorrect content-type was requested
_ => Err(warp::reject::custom(Error::InvalidType)),
2022-01-14 17:13:44 +00:00
},
// There was an error when executing the query
Err(err) => Err(warp::reject::custom(Error::from(err))),
2022-01-14 17:13:44 +00:00
}
2020-06-29 15:36:01 +00:00
}