aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2018-06-09 13:05:59 +0200
committerKatharina Fey <kookie@spacekookie.de>2018-06-09 13:05:59 +0200
commit7ca8460f324387a28b991342cdae1ab1a3b70050 (patch)
tree681e2cf5868bdf8c71939b6862870b6237159db3
parenta7a4df2dc1a8df878db19abe9c6ee894a3342532 (diff)
Tweaking the lockchain-http API, adding some constraints on vault traits
-rw-r--r--Cargo.lock51
-rw-r--r--lockchain-core/src/traits.rs4
-rw-r--r--lockchain-files/src/lib.rs2
-rw-r--r--lockchain-http/examples/simple-http.rs6
-rw-r--r--lockchain-http/src/handlers.rs33
-rw-r--r--lockchain-http/src/lib.rs73
-rw-r--r--lockchain-server/Cargo.toml6
-rw-r--r--lockchain-server/src/main.rs13
8 files changed, 84 insertions, 104 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 59ff947..a0871b4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -22,7 +22,7 @@ dependencies = [
[[package]]
name = "actix-web"
-version = "0.6.11"
+version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"actix 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -801,21 +801,6 @@ dependencies = [
]
[[package]]
-name = "lockchain-core"
-version = "0.7.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "bcrypt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "blake2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "chrono 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
name = "lockchain-crypto"
version = "0.7.0"
dependencies = [
@@ -833,19 +818,11 @@ dependencies = [
]
[[package]]
-name = "lockchain-files"
-version = "0.7.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "lockchain-core 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
name = "lockchain-http"
version = "0.1.1"
dependencies = [
"actix 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)",
- "actix-web 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "actix-web 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
"lockchain-core 0.7.1",
@@ -854,26 +831,13 @@ dependencies = [
]
[[package]]
-name = "lockchain-http"
-version = "0.1.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "actix 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)",
- "actix-web 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
- "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
- "lockchain-core 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
name = "lockchain-server"
version = "0.1.0"
dependencies = [
"clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "lockchain-core 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "lockchain-files 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "lockchain-http 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lockchain-core 0.7.1",
+ "lockchain-files 0.7.0",
+ "lockchain-http 0.1.1",
]
[[package]]
@@ -1840,7 +1804,7 @@ dependencies = [
[metadata]
"checksum actix 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7f0b2daad36916ccd2b162dbc5a04a74df642a29391b1a341c8ee3e82026cb16"
-"checksum actix-web 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "65954d5806f6e8f6424923e21e2f3f881528b6851f6c64233bb8efb7b3f88959"
+"checksum actix-web 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "39511e3e552addb839a2db972f5a8096d43c1c4699cd1fa83e39f664c969a033"
"checksum actix_derive 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c4b1dc922654b9aca7a8a31eab875fde804fa9fbd67f220f2e457787b23590f2"
"checksum aesni 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5e88c3698cd61460af7bdbcc747d0e37b61255492fcfc81845dd4666f3bf6714"
"checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4"
@@ -1927,9 +1891,6 @@ dependencies = [
"checksum lazycell 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a6f08839bc70ef4a3fe1d566d5350f519c5912ea86be0df1740a7d247c7fc0ef"
"checksum libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)" = "b685088df2b950fccadf07a7187c8ef846a959c142338a48f9dc0b94517eb5f1"
"checksum linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7860ec297f7008ff7a1e3382d7f7e1dcd69efc94751a2284bafc3d013c2aa939"
-"checksum lockchain-core 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "986baeef299b8deb0e1312f434e537a3d1764921719e3bab3d0f6944a7967fcd"
-"checksum lockchain-files 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "38e9e6701d6a0282a6910c26f3f465e9456658362a13836dcc798014f55d1e9d"
-"checksum lockchain-http 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "57005eff1d4019f6afdd27145550599a100b56962b592fc47da395502a85ff9b"
"checksum log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6fddaa003a65722a7fb9e26b0ce95921fe4ba590542ced664d8ce2fa26f9f3ac"
"checksum lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4d06ff7ff06f729ce5f4e227876cb88d10bc59cd4ae1e09fbb2bde15c850dc21"
"checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376"
diff --git a/lockchain-core/src/traits.rs b/lockchain-core/src/traits.rs
index 76c028a..5e9ab13 100644
--- a/lockchain-core/src/traits.rs
+++ b/lockchain-core/src/traits.rs
@@ -22,7 +22,7 @@ use serde_json::{self, Error as SerdeError};
/// data module.
///
/// This allows working with both encrypted and cleartext data bodies.
-pub trait Body: DeserializeOwned + Serialize {
+pub trait Body: DeserializeOwned + Serialize + Send {
///Get the value of a field from this body
fn get_field(&self, key: &str) -> Option<&Payload>;
/// Set the value of a field
@@ -87,7 +87,7 @@ pub trait Loading {
/// crate.
///
/// The body backend is being being generic with the `Body` trait.
-pub trait Vault<T>
+pub trait Vault<T>: Send
where
T: Body,
{
diff --git a/lockchain-files/src/lib.rs b/lockchain-files/src/lib.rs
index fbedc34..eaa57c7 100644
--- a/lockchain-files/src/lib.rs
+++ b/lockchain-files/src/lib.rs
@@ -27,7 +27,7 @@ impl<T: Body> DataVault<T> {
}
impl<T: Body> Vault<T> for DataVault<T> {
- fn new(name: &str, location: &str) -> Self {
+ fn new(name: &str, location: &str) -> DataVault<T> {
Self {
records: HashMap::new(),
fs: Filesystem::create(location, name),
diff --git a/lockchain-http/examples/simple-http.rs b/lockchain-http/examples/simple-http.rs
index 1811c08..893cc0e 100644
--- a/lockchain-http/examples/simple-http.rs
+++ b/lockchain-http/examples/simple-http.rs
@@ -2,8 +2,8 @@ extern crate lockchain_core as lockchain;
extern crate lockchain_http;
fn main() {
- use lockchain::EncryptedBody;
- use lockchain_http::start_server;
+ // use lockchain::EncryptedBody;
+ // use lockchain_http::start_server;
- start_server::<EncryptedBody>("localhost", "8888");
+ // start_server::<EncryptedBody>("localhost", "8888");
}
diff --git a/lockchain-http/src/handlers.rs b/lockchain-http/src/handlers.rs
index 6b9e641..64d0b8a 100644
--- a/lockchain-http/src/handlers.rs
+++ b/lockchain-http/src/handlers.rs
@@ -1,30 +1,37 @@
//! Definition of the core lockchain API
-use actix::{HttpRequest, Json, Responder, Result};
-use lockchain::{traits::Body, Record};
+use actix_web::{HttpRequest, Json, Responder, Result};
+use lockchain::{
+ traits::{Body, Vault}, Record,
+};
use model::CarrierMessage;
+use std::sync::{Arc, Mutex};
+
+type HttpRequestState<T> = HttpRequest<Arc<Mutex<T>>>;
+
/// PUT /vault
-///
-///
-pub fn create_vault(_req: HttpRequest) -> impl Responder {
+pub fn create_vault<B: Body>(_req: HttpRequestState<impl Vault<B>>) -> impl Responder {
format!("Unimplemented!")
}
/// POST /vault/{vault-id}
-pub fn update_vault(_req: HttpRequest) -> impl Responder {
+pub fn update_vault<B: Body>(_req: HttpRequestState<impl Vault<B>>) -> impl Responder {
format!("Unimplemented!")
}
/// DELETE /vault/{vault-id}
-pub fn delete_vault(_req: HttpRequest) -> impl Responder {
+pub fn delete_vault<B: Body>(_req: HttpRequestState<impl Vault<B>>) -> impl Responder {
format!("Unimplemented!")
}
/// GET /vault/{vault-id}/records/{record-id}
-pub fn get_record<T: Body>(_req: HttpRequest) -> Result<Json<CarrierMessage<Record<T>>>> {
+pub fn get_record<B: Body>(
+ _req: HttpRequestState<impl Vault<B>>,
+) -> Result<Json<CarrierMessage<Record<B>>>> {
unimplemented!()
+
// Ok(Json(CarrierMessage {
// error: Ok(()),
// data: Some(Record::new("name", "category", vec!["test", "foo"])),
@@ -32,26 +39,26 @@ pub fn get_record<T: Body>(_req: HttpRequest) -> Result<Json<CarrierMessage<Reco
}
/// PUT /vault/{vault-id}/records
-pub fn create_record(_req: HttpRequest) -> impl Responder {
+pub fn create_record<B: Body>(_req: HttpRequestState<impl Vault<B>>) -> impl Responder {
format!("Unimplemented!")
}
/// POST /vault/{vault-id}/records/{record-id}
-pub fn update_record(_req: HttpRequest) -> impl Responder {
+pub fn update_record<B: Body>(_req: HttpRequestState<impl Vault<B>>) -> impl Responder {
format!("Unimplemented!")
}
/// DELETE /vault/{vault-id}/records/{record-id}
-pub fn delete_record(_req: HttpRequest) -> impl Responder {
+pub fn delete_record<B: Body>(_req: HttpRequestState<impl Vault<B>>) -> impl Responder {
format!("Unimplemented!")
}
/// PUT /authenticate
-pub fn authenticate(_req: HttpRequest) -> impl Responder {
+pub fn authenticate<B: Body>(_req: HttpRequestState<impl Vault<B>>) -> impl Responder {
format!("Unimplemented!")
}
/// PUT /de-authenticate
-pub fn deauthenticate(_req: HttpRequest) -> impl Responder {
+pub fn deauthenticate<B: Body>(_req: HttpRequestState<impl Vault<B>>) -> impl Responder {
format!("Unimplemented!")
}
diff --git a/lockchain-http/src/lib.rs b/lockchain-http/src/lib.rs
index 1721eee..2ad73ba 100644
--- a/lockchain-http/src/lib.rs
+++ b/lockchain-http/src/lib.rs
@@ -1,60 +1,61 @@
-//! A plug and play http interface layer for various lockchain components
-//!
-//! The way this is done is by shimming a common REST interface (via actix-web) in
-//! between common `lockchain-core` types and the `lockchain-client` library which
-//! is a base wrapper around `reqwest` which uses this API.
-//!
-//! You can of course also use whatever other library, in whatever language you want
-//! to access this API. Doing so via the *official* client gives you the ability to
-//! negotiate version numbers and have more advanced error handling built-in.
-//!
-//! Ideally this shim-layer version should be the same as the `lockchain-core` it binds
-//! against, however especially during development this won't always be the case.
+// //! A plug and play http interface layer for various lockchain components
+// //!
+// //! The way this is done is by shimming a common REST interface (via actix-web) in
+// //! between common `lockchain-core` types and the `lockchain-client` library which
+// //! is a base wrapper around `reqwest` which uses this API.
+// //!
+// //! You can of course also use whatever other library, in whatever language you want
+// //! to access this API. Doing so via the *official* client gives you the ability to
+// //! negotiate version numbers and have more advanced error handling built-in.
+// //!
+// //! Ideally this shim-layer version should be the same as the `lockchain-core` it binds
+// //! against, however especially during development this won't always be the case.
#[macro_use]
extern crate serde_derive;
extern crate env_logger;
extern crate serde;
-extern crate actix_web as actix;
+extern crate actix_web;
extern crate lockchain_core as lockchain;
-use actix::{server, App};
-use lockchain::traits::{Body, Vault};
-
mod handlers;
mod model;
+// pub use model::CarrierMessage;
-pub use model::CarrierMessage;
+
+use actix_web::{server, App};
+use lockchain::traits::{Body, Vault};
+use std::sync::{Arc, Mutex};
/// Starts a new lockchain server for a certain payload type
///
/// The payload type is defined by the generic parameter provided and can
/// either be just the encrypted message body, or the decrypted message
/// body which is available via the lockchain-crypto crate
-pub fn start_server<B, V>(iface: &str, port: &str)
-where
- B: 'static + Body,
- V: 'static + Vault<B>,
-{
- server::new(|| {
- App::new()
+///
+/// Create a new lockchain-http server for a vault state
+///
+///
+pub fn create_server<B: Body + 'static>(
+ state: impl Vault<B> + 'static,
+) -> server::HttpServer<App<Arc<Mutex<impl Vault<B> + 'static>>>> {
+ let state = Arc::new(Mutex::new(state));
+
+ server::new(move || {
+ vec![App::with_state(Arc::clone(&state))
+
.resource("/vault", |r| r.f(handlers::create_vault))
.resource("/vault/{vaultid}", |r| r.f(handlers::update_vault))
.resource("/vault/{vaultid}", |r| r.f(handlers::delete_vault))
- .resource("/vault/{vaultid}/records/{recordid}", |r| {
- r.f(handlers::get_record::<B>)
- })
+ .resource("/vault/{vaultid}/records/{recordid}", |r| { r.f(handlers::get_record) })
.resource("/vault/{vaultid}/records", |r| r.f(handlers::create_record))
- .resource("/vault/{vaultid}/records/{recordid}", |r| {
- r.f(handlers::update_record)
- })
- .resource("/vault/{vaultid}/records/{recordid}", |r| {
- r.f(handlers::delete_record)
- })
+ .resource("/vault/{vaultid}/records/{recordid}", |r| { r.f(handlers::update_record) })
+ .resource("/vault/{vaultid}/records/{recordid}", |r| { r.f(handlers::delete_record) })
.resource("/authenticate", |r| r.f(handlers::authenticate))
.resource("/deauthenticate", |r| r.f(handlers::deauthenticate))
- }).bind(format!("{}:{}", iface, port))
- .expect("Can not bind to port 8000")
- .run();
+
+ ]
+ }).bind("localhost:8080")
+ .expect("Oh no!")
}
diff --git a/lockchain-server/Cargo.toml b/lockchain-server/Cargo.toml
index d9c8cb1..6faeba2 100644
--- a/lockchain-server/Cargo.toml
+++ b/lockchain-server/Cargo.toml
@@ -4,8 +4,8 @@ version = "0.1.0"
authors = ["Katharina Fey <kookie@spacekookie.de>"]
[dependencies]
-lockchain-core = "*"
-lockchain-files = "*"
-lockchain-http = "*"
+lockchain-core = { path = "../lockchain-core" }
+lockchain-files ={ path = "../lockchain-files" }
+lockchain-http = { path = "../lockchain-http" }
clap = "*" \ No newline at end of file
diff --git a/lockchain-server/src/main.rs b/lockchain-server/src/main.rs
index 00fc430..748fcc0 100644
--- a/lockchain-server/src/main.rs
+++ b/lockchain-server/src/main.rs
@@ -4,7 +4,18 @@ extern crate lockchain_http as http;
extern crate clap;
+use files::*;
+use http::*;
+use lockchain::traits::*;
+use lockchain::EncryptedBody;
+
+fn foo() -> DataVault<EncryptedBody> {
+ DataVault::new("name", "location")
+}
fn main() {
- println!("Hello, world!");
+ let server = create_server(foo());
+ server.run();
+
+ println!("After the server died!");
}