diff options
author | Katharina Fey <kookie@spacekookie.de> | 2018-06-12 11:47:19 +0200 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2018-06-12 11:47:19 +0200 |
commit | e47c2198c8f92003eb95f15a4311420e3b06c878 (patch) | |
tree | 8493bf7c7ccd64942071374d51850a6037ee928b | |
parent | 5e4a2c1df2aceef1ad736f6ddd9d3170711a8510 (diff) |
Starting work on lockchain-http scope files
-rw-r--r-- | lockchain-core/src/prelude.rs | 0 | ||||
-rw-r--r-- | lockchain-http/src/handlers.rs | 26 | ||||
-rw-r--r-- | lockchain-http/src/lib.rs | 21 | ||||
-rw-r--r-- | lockchain-http/src/state.rs | 58 |
4 files changed, 90 insertions, 15 deletions
diff --git a/lockchain-core/src/prelude.rs b/lockchain-core/src/prelude.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lockchain-core/src/prelude.rs diff --git a/lockchain-http/src/handlers.rs b/lockchain-http/src/handlers.rs index 4fc3c27..c943cef 100644 --- a/lockchain-http/src/handlers.rs +++ b/lockchain-http/src/handlers.rs @@ -7,6 +7,7 @@ use lockchain::{ use model::*; +use std::intrinsics; use std::sync::{Arc, Mutex}; type HttpRequestState<T> = HttpRequest<Arc<Mutex<T>>>; @@ -25,7 +26,11 @@ pub fn get_vaults<B: Body>(req: HttpRequestState<impl Vault<B>>) -> impl Respond /// PUT /vault /// /// Check the documentation for more information about how to provide payloads -pub fn create_vault<B: Body>(_req: HttpRequestState<impl Vault<B>>) -> impl Responder { +pub fn create_vault<B, V>(_req: HttpRequestState<V>) -> impl Responder +where + B: Body, + V: Vault<B>, +{ format!("Unimplemented!") } @@ -40,10 +45,14 @@ pub fn delete_vault<B: Body>(_req: HttpRequestState<impl Vault<B>>) -> impl Resp } /// GET /vault/{vault-id}/records/{record-id} -pub fn get_record<B: Body>( - _req: HttpRequestState<impl Vault<B>>, -) -> Result<Json<CarrierMessage<Record<B>>>> { - unimplemented!() +pub fn get_record<B, V>(req: HttpRequestState<V>) -> impl Responder +where + B: Body, + V: Vault<B>, +{ + format!("Unimplemented!") + + // Ok(Json(CarrierMessage { // error: Ok(()), @@ -79,10 +88,13 @@ pub fn deauthenticate<B: Body>(_req: HttpRequestState<impl Vault<B>>) -> impl Re /// GET /api /// /// Check the documentation for more information about how to provide payloads -pub fn api_data<B: Body>(_: HttpRequestState<impl Vault<B>>) -> impl Responder { +pub fn api_data<B: Body, V: Vault<B>>(_: HttpRequestState<V>) -> impl Responder { Json(ApiInformation { version: "1.0".into(), - providers: vec!["FileVault".into(), "EncryptedBody".into()], + providers: vec![ + unsafe { intrinsics::type_name::<V>() }.into(), + unsafe { intrinsics::type_name::<B>() }.into(), + ], hostname: None, supported: "1.0".into(), }) diff --git a/lockchain-http/src/lib.rs b/lockchain-http/src/lib.rs index 6468967..382ee86 100644 --- a/lockchain-http/src/lib.rs +++ b/lockchain-http/src/lib.rs @@ -14,6 +14,8 @@ //! **Note**: API endpoint documentation can be found //! [here](https://github.com/spacekookie/lockchain/tree/master/lockchain-http#api-reference) +#![feature(core_intrinsics)] + #[macro_use] extern crate serde_derive; extern crate env_logger; @@ -23,6 +25,7 @@ extern crate actix_web; extern crate lockchain_core as lockchain; mod handlers; +mod state; mod model; pub use model::CarrierMessage; @@ -61,22 +64,24 @@ pub fn create_server<B: Body + 'static>( server::new(move || { vec![ App::with_state(Arc::clone(&state)) - .resource("/vault", |r| { + .resource("/vaults", |r| { r.method(http::Method::GET).with(handlers::get_vaults) }) - .resource("/vault", |r| { + .resource("/vaults", |r| { r.method(http::Method::PUT).with(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| { + .resource("/vaults/{vaultid}", |r| r.f(handlers::update_vault)) + .resource("/vaults/{vaultid}", |r| r.f(handlers::delete_vault)) + .resource("/vaults/{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| { + .resource("/vaults/{vaultid}/records", |r| { + r.f(handlers::create_record) + }) + .resource("/vaults/{vaultid}/records/{recordid}", |r| { r.f(handlers::update_record) }) - .resource("/vault/{vaultid}/records/{recordid}", |r| { + .resource("/vaults/{vaultid}/records/{recordid}", |r| { r.f(handlers::delete_record) }) .resource("/authenticate", |r| r.f(handlers::authenticate)) diff --git a/lockchain-http/src/state.rs b/lockchain-http/src/state.rs new file mode 100644 index 0000000..45d92b1 --- /dev/null +++ b/lockchain-http/src/state.rs @@ -0,0 +1,58 @@ +use lockchain::traits::{AutoEncoder, Body, Vault}; +use std::collections::HashMap; +use std::marker::PhantomData; + +pub struct ApiState<B, V> +where + B: Body, + V: Vault<B>, +{ + vaults: HashMap<String, Option<V>>, + _phantom: PhantomData<B>, +} + +#[derive(Serialize, Deserialize)] +struct SerializedState { + vaults: Vec<String>, +} + +impl AutoEncoder for SerializedState {} + +/// Implements the transform from in-memory to on-disk +impl<B, V> From<ApiState<B, V>> for SerializedState +where + B: Body, + V: Vault<B>, +{ + fn from(me: ApiState<B, V>) -> SerializedState { + SerializedState { + vaults: me + .vaults + .into_iter() + .fold(Vec::new(), |mut acc: Vec<String>, (k, v)| { + acc.push(k); + acc + }), + } + } +} + +/// Implements the transform from on-disk to in-memory +impl<B, V> From<SerializedState> for ApiState<B, V> +where + B: Body, + V: Vault<B>, +{ + fn from(me: SerializedState) -> ApiState<B, V> { + ApiState { + vaults: me.vaults.into_iter().fold( + HashMap::new(), + |mut acc: HashMap<String, Option<V>>, k| { + acc.insert(k, None); + acc + }, + ), + _phantom: PhantomData, + } + } +} |