Skip to content

Commit

Permalink
Updated web interface
Browse files Browse the repository at this point in the history
  • Loading branch information
katyo committed Sep 19, 2018
1 parent 6a7875c commit 644487e
Show file tree
Hide file tree
Showing 9 changed files with 381 additions and 73 deletions.
4 changes: 3 additions & 1 deletion ledb-actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ appveyor = { repository = "katyo/ledb" }
[dependencies]
serde = "1"
serde_derive = { version = "1", optional = true }
serde_with = { version = "0.2", optional = true, features = ["json"] }
ledb = { path = "../ledb" }
futures = { version = "0.1", optional = true }
actix = "0.7"
Expand All @@ -29,7 +30,8 @@ serde_json = "1"
futures = "0.1"
tokio = "0.1"
actix-web = "0.7"
pretty_logger = "0.1"

[features]
default = ["web"]
web = ["futures", "serde_derive", "actix-web"]
web = ["futures", "serde_derive", "serde_with", "actix-web"]
10 changes: 8 additions & 2 deletions ledb-actix/examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
extern crate ledb_actix;
extern crate actix;
extern crate actix_web;
extern crate pretty_logger;

use ledb_actix::{Storage, storage};
use actix::{System};
use actix_web::{App, server};
use actix_web::{App, server, middleware::Logger};

fn main() {
pretty_logger::init_to_defaults().unwrap();

System::run(|| {
let addr = Storage::new("database")
.unwrap()
.start(4);

let bind = "127.0.0.1:8888";

server::new(move || App::with_state(addr.clone())
.middleware(Logger::default())
.scope("/", storage))
.bind("127.0.0.1:8888")
.bind(&bind)
.unwrap()
.start();
});
Expand Down
61 changes: 58 additions & 3 deletions ledb-actix/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::marker::PhantomData;
use std::sync::Arc;
use std::path::Path;
use serde::{Serialize, de::DeserializeOwned};
use ledb::{Storage as LeStorage, Result as LeResult, Identifier};
use ledb::{Storage as LeStorage, Result as LeResult};
use actix::{Actor, Addr, Message, SyncContext, SyncArbiter, Handler};

pub use ledb::{Filter, Comp, Cond, Order, OrderKind, IndexKind, KeyType, KeyData, Modify, Action, Primary, Document, DocumentsIterator, Value};
pub use ledb::{Filter, Comp, Cond, Order, OrderKind, IndexKind, KeyType, KeyData, Modify, Action, Identifier, Primary, Document, DocumentsIterator, Value, Stats, Info};

#[derive(Clone)]
pub struct Storage {
Expand All @@ -26,6 +26,36 @@ impl Actor for Storage {
type Context = SyncContext<Self>;
}

/// Get database stats
pub struct GetStats;

impl Message for GetStats {
type Result = LeResult<Stats>;
}

impl Handler<GetStats> for Storage {
type Result = <GetStats as Message>::Result;

fn handle(&mut self, _: GetStats, _: &mut Self::Context) -> Self::Result {
self.db.get_stats()
}
}

/// Get database info
pub struct GetInfo;

impl Message for GetInfo {
type Result = LeResult<Info>;
}

impl Handler<GetInfo> for Storage {
type Result = <GetInfo as Message>::Result;

fn handle(&mut self, _: GetInfo, _: &mut Self::Context) -> Self::Result {
self.db.get_info()
}
}

/// Get collections request
pub struct GetCollections;

Expand All @@ -43,6 +73,31 @@ impl Handler<GetCollections> for Storage {
}
}

/// Ensure collection for collection
#[allow(non_snake_case)]
pub fn EnsureCollection<C: Into<Identifier>>(coll: C) -> EnsureCollectionMsg {
EnsureCollectionMsg(coll.into())
}

pub struct EnsureCollectionMsg(Identifier);

impl Message for EnsureCollectionMsg {
type Result = LeResult<bool>;
}

impl Handler<EnsureCollectionMsg> for Storage {
type Result = <EnsureCollectionMsg as Message>::Result;

fn handle(&mut self, EnsureCollectionMsg(name): EnsureCollectionMsg, _: &mut Self::Context) -> Self::Result {
Ok(if self.db.has_collection(&name)? {
false
} else {
self.db.collection(name)?;
true
})
}
}

/// Drop collection
#[allow(non_snake_case)]
pub fn DropCollection<C: Into<Identifier>>(coll: C) -> DropCollectionMsg {
Expand All @@ -69,7 +124,7 @@ pub fn GetIndexes<C: Into<Identifier>>(coll: C) -> GetIndexesMsg {
GetIndexesMsg(coll.into())
}

pub type ListIndexes = Vec<(String, IndexKind, KeyType)>;
pub type ListIndexes = Vec<(Identifier, IndexKind, KeyType)>;

pub struct GetIndexesMsg(Identifier);

Expand Down
3 changes: 3 additions & 0 deletions ledb-actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ extern crate serde_derive;
#[macro_use]
extern crate serde_json;

#[cfg(feature = "web")]
extern crate serde_with;

#[cfg(feature = "web")]
extern crate actix_web;

Expand Down
Loading

0 comments on commit 644487e

Please sign in to comment.