diff options
author | Katharina Fey <kookie@spacekookie.de> | 2018-06-07 17:10:06 +0200 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2018-06-07 17:10:06 +0200 |
commit | 257bc9bf0ed79ac1e9631e585f39f9af778d87c4 (patch) | |
tree | fd664f6ac4daaa0bc983a190914fe2fb5a810dae | |
parent | 71cd4e6133e31d79339d82a73555d6c92d2a75bd (diff) |
Starting work on the lockchain-http interface library
-rw-r--r-- | lockchain-http/src/handlers.rs | 55 | ||||
-rw-r--r-- | lockchain-http/src/impl.rs | 1 | ||||
-rw-r--r-- | lockchain-http/src/impl/base.rs | 0 | ||||
-rw-r--r-- | lockchain-http/src/lib.rs | 49 | ||||
-rw-r--r-- | lockchain-http/src/main.rs | 116 | ||||
-rw-r--r-- | lockchain-http/src/model.rs | 11 |
6 files changed, 115 insertions, 117 deletions
diff --git a/lockchain-http/src/handlers.rs b/lockchain-http/src/handlers.rs new file mode 100644 index 0000000..d4f707e --- /dev/null +++ b/lockchain-http/src/handlers.rs @@ -0,0 +1,55 @@ +//! Definition of the core lockchain API + +use actix::{HttpRequest, Json, Responder, Result}; +use lockchain::{traits::Body, Record}; + +use model::CarrierMessage; + +/// PUT /vault +pub fn create_vault(_req: HttpRequest) -> impl Responder { + format!("Unimplemented!") +} + +/// POST /vault/{vault-id} +pub fn update_vault(_req: HttpRequest) -> impl Responder { + format!("Unimplemented!") +} + +/// DELETE /vault/{vault-id} +pub fn delete_vault(_req: HttpRequest) -> impl Responder { + format!("Unimplemented!") +} + +/// GET /vault/{vault-id}/records/{record-id} +pub fn get_record<T: Body>(_req: HttpRequest) -> Result<Json<CarrierMessage<Record<T>>>> { + unimplemented!() + // Ok(Json(CarrierMessage { + // error: Ok(()), + // data: Some(Record::new("name", "category", vec!["test", "foo"])), + // })) +} + +/// PUT /vault/{vault-id}/records +pub fn create_record(_req: HttpRequest) -> impl Responder { + format!("Unimplemented!") +} + +/// POST /vault/{vault-id}/records/{record-id} +pub fn update_record(_req: HttpRequest) -> impl Responder { + format!("Unimplemented!") +} + +/// DELETE /vault/{vault-id}/records/{record-id} +pub fn delete_record(_req: HttpRequest) -> impl Responder { + format!("Unimplemented!") +} + +/// PUT /authenticate +pub fn authenticate(_req: HttpRequest) -> impl Responder { + format!("Unimplemented!") +} + +/// PUT /de-authenticate +pub fn deauthenticate(_req: HttpRequest) -> impl Responder { + format!("Unimplemented!") +} diff --git a/lockchain-http/src/impl.rs b/lockchain-http/src/impl.rs deleted file mode 100644 index 3a0e0f6..0000000 --- a/lockchain-http/src/impl.rs +++ /dev/null @@ -1 +0,0 @@ -//! Definition of the core lockchain API diff --git a/lockchain-http/src/impl/base.rs b/lockchain-http/src/impl/base.rs deleted file mode 100644 index e69de29..0000000 --- a/lockchain-http/src/impl/base.rs +++ /dev/null diff --git a/lockchain-http/src/lib.rs b/lockchain-http/src/lib.rs new file mode 100644 index 0000000..a808c1b --- /dev/null +++ b/lockchain-http/src/lib.rs @@ -0,0 +1,49 @@ +// //! A plug and play http interface layer for various lockchain components +// #![feature(external_doc)] +// #![doc(include = "../README.md")] +// #![feature(non_modrs_mods)] + +#[macro_use] +extern crate serde_derive; +extern crate serde; + +extern crate actix_web as actix; +extern crate lockchain_core as lockchain; + +use actix::{server, App}; +use lockchain::traits::Body; + +mod model; +pub use model::CarrierMessage; + +mod handlers; + +/// 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<T: 'static + Body>(iface: &str, port: &str) { + server::new(|| { + App::new() + .resource("/vault", |r| r.f(handlers::create_vault)) + .resource("/vault/{vault-id}", |r| r.f(handlers::update_vault)) + .resource("/vault/{vault-id}", |r| r.f(handlers::delete_vault)) + .resource("/vault/{vault-id}/records/{record-id}", |r| { + r.f(handlers::get_record::<T>) + }) + .resource("/vault/{vault-id}/records", |r| { + r.f(handlers::create_record) + }) + .resource("/vault/{vault-id}/records/{record-id}", |r| { + r.f(handlers::update_record) + }) + .resource("/vault/{vault-id}/records/{record-id}", |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(); +} diff --git a/lockchain-http/src/main.rs b/lockchain-http/src/main.rs deleted file mode 100644 index 53ab9e3..0000000 --- a/lockchain-http/src/main.rs +++ /dev/null @@ -1,116 +0,0 @@ -//! A plug and play http interface layer for various lockchain components -#![feature(external_doc)] -#![doc(include = "../README.md")] -#![feature(non_modrs_mods)] - -extern crate serde; - -#[macro_use] -extern crate serde_derive; - -extern crate actix_web as actix; -extern crate lockchain_core as lockchain; - -use actix::{http::Method, server, App, HttpRequest, Json, Responder, Result}; -use lockchain::{errors::Error as LockError, traits::Body, Record}; -use serde::{Serialize, de::DeserializeOwned}; - -#[derive(Serialize)] -struct CarrierMessage<T: Serialize + DeserializeOwned> { - error: Result<(), LockError>, - data: Option<T>, -} - -/// PUT /vault -fn create_vault(_req: HttpRequest) -> impl Responder { - format!("Not Implemented!") -} - -fn index<T: Body>(_req: HttpRequest) -> Result<Json<CarrierMessage<T>>> { - unimplemented!() -} - -/// POST /vault/{vault-id} -fn update_vault(_req: HttpRequest) -> impl Responder { - format!("Not Implemented!") -} - -/// DELETE /vault/{vault-id} -fn delete_vault(_req: HttpRequest) -> impl Responder { - format!("Not Implemented!") -} - -/// GET /vault/{vault-id}/records/{record-id} -fn get_record(_req: HttpRequest) -> impl Responder { - format!("Not Implemented!") -} - -/// PUT /vault/{vault-id}/records -fn create_record(_req: HttpRequest) -> impl Responder { - format!("Not Implemented!") -} - -/// POST /vault/{vault-id}/records/{record-id} -fn update_record(_req: HttpRequest) -> impl Responder { - format!("Not Implemented!") -} - -/// DELETE /vault/{vault-id}/records/{record-id} -fn delete_record(_req: HttpRequest) -> impl Responder { - format!("Not Implemented!") -} - -/// PUT /authenticate -fn authenticate(_req: HttpRequest) -> impl Responder { - format!("Not Implemented!") -} - -/// PUT /de-authenticate -fn deauthenticate(_req: HttpRequest) -> impl Responder { - format!("Not Implemented!") -} - -fn main() { - server::new(|| { - App::new() - .resource("/vault", |r| r.f(create_vault)) - .resource("/vault/{vault-id}", |r| r.f(update_vault)) - .resource("/vault/{vault-id}", |r| r.f(delete_vault)) - .resource("/vault/{vault-id}/records/{record-id}", |r| { - r.f(get_record) - }) - .resource("/vault/{vault-id}/records", |r| r.f(create_record)) - .resource("/vault/{vault-id}/records/{record-id}", |r| { - r.f(update_record) - }) - .resource("/vault/{vault-id}/records/{record-id}", |r| { - r.f(delete_record) - }) - .resource("/authenticate", |r| r.f(authenticate)) - .resource("/deauthenticate", |r| r.f(deauthenticate)) - }).bind("127.0.0.1:8000") - .expect("Can not bind to port 8000") - .run(); -} - -/// The REST API only have major/ incompatible versions -const API_VERSION: &'static str = "v1"; - -/// Contains API internal state and metadata -pub struct Server {} - -impl Server {} - -/// An enum that represents optional features. At least -/// one flag needs to be provided to initialise [[Server]] -/// in order to make a working lockchain-http interface. -pub enum ApiFeature { - /// Basic functionality for record I/O - Base, - /// Enables user access management - Users, - /// Allows management of user identities - UserManagement, - /// Allows management of filestorage scopes & loading - VaultManagement, -} diff --git a/lockchain-http/src/model.rs b/lockchain-http/src/model.rs new file mode 100644 index 0000000..bb3eefa --- /dev/null +++ b/lockchain-http/src/model.rs @@ -0,0 +1,11 @@ +//! API data models + +use lockchain::errors::Error as LockError; +use serde::{Serialize, de::DeserializeOwned}; + +#[derive(Serialize, Deserialize)] +pub struct CarrierMessage<T: Serialize + DeserializeOwned> { + pub error: Result<(), LockError>, + #[serde(bound(deserialize = "T: Serialize + DeserializeOwned"))] + pub data: Option<T>, +}
\ No newline at end of file |