Skip to content

Commit

Permalink
Fixed actix-web support
Browse files Browse the repository at this point in the history
  • Loading branch information
katyo committed Oct 27, 2018
1 parent 683e581 commit e981400
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 37 deletions.
2 changes: 1 addition & 1 deletion ledb-actix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ledb-actix"
version = "0.2.0"
version = "0.2.1"
authors = ["Kayo <kayo@illumium.org>"]
license = "MIT"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion ledb-actix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ __GET__ /collection/_$collection_name_/index

#### create new index for collection

__POST__ /collection/_$collection_name_/index?name=_$field_name_&kind=_$index_kind_&type=_$key_type_
__POST__ /collection/_$collection_name_/index?path=_$field_name_&kind=_$index_kind_&key=_$key_type_

#### drop index of collection

Expand Down
4 changes: 2 additions & 2 deletions ledb-actix/examples/client.rc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ Content-Type: application/json
GET :url/collection/user/document/1

# Ensure index (unique name)
POST :url/collection/user/index?name=name&kind=uni&type=string
POST :url/collection/user/index?path=name&kind=uni&key=string

# Ensure index (prefs)
POST :url/collection/user/index?name=prefs&type=string
POST :url/collection/user/index?path=prefs&key=string

# Drop index
DELETE :url/collection/user/index/name
Expand Down
2 changes: 1 addition & 1 deletion ledb-actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ __GET__ /collection/_$collection_name_/index
#### create new index for collection
__POST__ /collection/_$collection_name_/index?name=_$field_name_&kind=_$index_kind_&type=_$key_type_
__POST__ /collection/_$collection_name_/index?path=_$field_name_&kind=_$index_kind_&key=_$key_type_
#### drop index of collection
Expand Down
58 changes: 26 additions & 32 deletions ledb-actix/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::usize;

use super::{
Delete, Document, DropCollection, DropIndex, EnsureCollection, EnsureIndex, Filter, Find, Get,
GetCollections, GetIndexes, GetInfo, GetStats, IndexKind, Info, Insert, KeyType,
ListCollections, Modify, Order, Primary, Put, Remove, Stats, Storage, Update, Value,
GetCollections, GetIndexes, GetInfo, GetStats, Info, Insert, KeyField, ListCollections, Modify,
Order, Primary, Put, Remove, Stats, Storage, Update, Value,
};

/// Storage actor address type
Expand Down Expand Up @@ -105,7 +105,7 @@ Index API:
# get indexes of collection
GET {indexes}
# create new index for collection
POST {indexes}?name=$field_path&kind=$index_kind&type=$key_type
POST {indexes}?path=$field_path&kind=$index_kind&key=$key_type
# drop index of collection
DELETE {index}
Expand Down Expand Up @@ -242,49 +242,29 @@ pub fn drop_collection(
})
}

/// Index parameters
#[derive(Serialize, Deserialize)]
pub struct IndexParams {
pub name: String,
#[serde(default)]
pub kind: IndexKind,
#[serde(rename = "type")]
pub key: KeyType,
}

/// Get indexes handler
pub fn get_indexes(
(addr, coll): (State<StorageAddr>, Path<String>),
) -> impl Future<Item = Json<Vec<IndexParams>>, Error = Error> {
) -> impl Future<Item = Json<Vec<KeyField>>, Error = Error> {
addr.send(GetIndexes(coll.into_inner()))
.map_err(ErrorServiceUnavailable)
.and_then(|res| res.map_err(ErrorInternalServerError))
.map(|indexes| {
Json(
indexes
.into_iter()
.map(|(name, kind, key)| IndexParams {
name: String::from(name.as_ref()),
kind,
key,
}).collect(),
)
})
.map(|indexes| Json(indexes.into_iter().collect()))
}

/// Ensure index handler
pub fn ensure_index(
(addr, coll, params, req): (
State<StorageAddr>,
Path<String>,
Query<IndexParams>,
Query<KeyField>,
HttpRequest<StorageAddr>,
),
) -> impl Future<Item = HttpResponse, Error = Error> {
let IndexParams { name, kind, key } = params.into_inner();
if let Ok(url) = req.url_for("index", &[&coll, &name]) {
let KeyField { path, kind, key } = params.into_inner();
if let Ok(url) = req.url_for("index", &[&coll, &path]) {
Either::A(
addr.send(EnsureIndex(coll.into_inner(), name, kind, key))
addr.send(EnsureIndex(coll.into_inner(), path, kind, key))
.map_err(ErrorServiceUnavailable)
.and_then(|res| res.map_err(ErrorInternalServerError))
.map(move |res| {
Expand Down Expand Up @@ -358,7 +338,7 @@ pub struct FindParams {
/// Find documents query handler
pub fn find_documents(
(addr, coll, query): (State<StorageAddr>, Path<String>, Query<FindParams>),
) -> impl Future<Item = Json<Vec<Document<Value>>>, Error = Error> {
) -> impl Future<Item = Json<Vec<Value>>, Error = Error> {
let FindParams {
filter,
order,
Expand Down Expand Up @@ -427,7 +407,7 @@ pub fn remove_documents(
/// Get document handler
pub fn get_document(
(addr, path): (State<StorageAddr>, Path<(String, Primary)>),
) -> impl Future<Item = Json<Document<Value>>, Error = Error> {
) -> impl Future<Item = Json<Value>, Error = Error> {
let (coll, id) = path.into_inner();
addr.send(Get(coll, id))
.map_err(ErrorServiceUnavailable)
Expand All @@ -438,12 +418,26 @@ pub fn get_document(
})
}

#[derive(Serialize)]
pub struct DocumentWithId {
#[serde(rename = "$")]
id: Primary,
#[serde(flatten)]
val: Value,
}

impl Document for DocumentWithId {}

/// Put document handler
pub fn put_document(
(addr, path, data): (State<StorageAddr>, Path<(String, Primary)>, Json<Value>),
) -> impl Future<Item = HttpResponse, Error = Error> {
let (coll, id) = path.into_inner();
addr.send(Put(coll, Document::new(data.into_inner()).with_id(id)))
let doc = DocumentWithId {
id,
val: data.into_inner(),
};
addr.send(Put(coll, doc))
.map_err(ErrorServiceUnavailable)
.and_then(|res| res.map_err(ErrorInternalServerError))
.map(|_| HttpResponse::NoContent().finish())
Expand Down

0 comments on commit e981400

Please sign in to comment.