aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2018-06-13 12:20:41 +0200
committerKatharina Fey <kookie@spacekookie.de>2018-06-13 12:20:41 +0200
commitf5615091071c2a4a969c1be9fe53e36fbda73c9a (patch)
treeceb094a093ac1c807ac34acf590de73d691b6e5f
parent8b8b287384660ea22282241d99559020a691307c (diff)
Refactoring http API and making "not working" routes return a more descriptive json
-rw-r--r--lockchain-http/src/handlers.rs40
-rw-r--r--lockchain-http/src/lib.rs8
-rw-r--r--lockchain-http/src/model.rs7
-rw-r--r--lockchain-http/src/state.rs6
-rw-r--r--lockchain-server/src/main.rs14
5 files changed, 55 insertions, 20 deletions
diff --git a/lockchain-http/src/handlers.rs b/lockchain-http/src/handlers.rs
index 93649a9..47beb59 100644
--- a/lockchain-http/src/handlers.rs
+++ b/lockchain-http/src/handlers.rs
@@ -59,7 +59,10 @@ where
B: Body,
V: Vault<B>,
{
- format!("Unimplemented!")
+ Json(OperationFailed {
+ reason: "Not implemented".into(),
+ code: 255,
+ })
}
/// DELETE /vault/{vault-id}
@@ -68,7 +71,10 @@ where
B: Body,
V: Vault<B>,
{
- format!("Unimplemented!")
+ Json(OperationFailed {
+ reason: "Not implemented".into(),
+ code: 255,
+ })
}
/// GET /vault/{vault-id}/records/{record-id}
@@ -77,7 +83,10 @@ where
B: Body,
V: Vault<B>,
{
- format!("Unimplemented!")
+ Json(OperationFailed {
+ reason: "Not implemented".into(),
+ code: 255,
+ })
// Ok(Json(CarrierMessage {
// error: Ok(()),
@@ -91,7 +100,10 @@ where
B: Body,
V: Vault<B>,
{
- format!("Unimplemented!")
+ Json(OperationFailed {
+ reason: "Not implemented".into(),
+ code: 255,
+ })
}
/// POST /vault/{vault-id}/records/{record-id}
@@ -100,7 +112,10 @@ where
B: Body,
V: Vault<B>,
{
- format!("Unimplemented!")
+ Json(OperationFailed {
+ reason: "Not implemented".into(),
+ code: 255,
+ })
}
/// DELETE /vault/{vault-id}/records/{record-id}
@@ -109,7 +124,10 @@ where
B: Body,
V: Vault<B>,
{
- format!("Unimplemented!")
+ Json(OperationFailed {
+ reason: "Not implemented".into(),
+ code: 255,
+ })
}
/// PUT /authenticate
@@ -118,7 +136,10 @@ where
B: Body,
V: Vault<B>,
{
- format!("Unimplemented!")
+ Json(OperationFailed {
+ reason: "Not implemented".into(),
+ code: 255,
+ })
}
/// PUT /de-authenticate
@@ -127,7 +148,10 @@ where
B: Body,
V: Vault<B>,
{
- format!("Unimplemented!")
+ Json(OperationFailed {
+ reason: "Not implemented".into(),
+ code: 255,
+ })
}
/// GET /api
diff --git a/lockchain-http/src/lib.rs b/lockchain-http/src/lib.rs
index bd0720f..85198b8 100644
--- a/lockchain-http/src/lib.rs
+++ b/lockchain-http/src/lib.rs
@@ -68,11 +68,11 @@ where
vec![
App::with_state(Arc::clone(&state))
.resource("/vaults", |r| {
- r.method(http::Method::GET).with(handlers::get_vaults)
- })
- .resource("/vaults", |r| {
- r.method(http::Method::PUT).with(handlers::create_vault)
+ r.method(http::Method::GET).with(handlers::get_vaults);
+ r.method(http::Method::PUT).with(handlers::create_vault);
})
+ // .resource("/vaults", |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| {
diff --git a/lockchain-http/src/model.rs b/lockchain-http/src/model.rs
index e16a4b0..63a71c3 100644
--- a/lockchain-http/src/model.rs
+++ b/lockchain-http/src/model.rs
@@ -15,6 +15,13 @@ pub struct CarrierMessage<T: Serialize + DeserializeOwned> {
pub data: Option<T>,
}
+/// A simple message that describes an invalid operation
+#[derive(Serialize, Deserialize)]
+pub struct OperationFailed {
+ pub reason: String,
+ pub code: u32,
+}
+
/// **Returns** Api information
#[derive(Serialize, Deserialize)]
pub struct ApiInformation {
diff --git a/lockchain-http/src/state.rs b/lockchain-http/src/state.rs
index 80cd2b7..72a34af 100644
--- a/lockchain-http/src/state.rs
+++ b/lockchain-http/src/state.rs
@@ -29,8 +29,10 @@ where
B: Body,
V: Vault<B>,
{
- vaults: HashMap<String, Option<V>>,
- _phantom: PhantomData<B>,
+ #[doc(hidden)]
+ pub vaults: HashMap<String, Option<V>>,
+ #[doc(hidden)]
+ pub _phantom: PhantomData<B>,
/// Signal if the API handlers are allowed outside their working dir
pub bound_scope: bool,
diff --git a/lockchain-server/src/main.rs b/lockchain-server/src/main.rs
index 6e22b53..8d3f3fd 100644
--- a/lockchain-server/src/main.rs
+++ b/lockchain-server/src/main.rs
@@ -8,14 +8,16 @@ extern crate lockchain_http as http;
use core::{traits::*, EncryptedBody};
use files::DataVault;
-use http::create_server;
+use http::{create_server, state::ApiState};
fn main() {
- let server = create_server(
- "localhost",
- "8080",
- DataVault::<EncryptedBody>::new("name", "location"),
- );
+ let state = ApiState::<EncryptedBody, DataVault<EncryptedBody>> {
+ bound_scope: true,
+ working_dir: ".".into(),
+ ..Default::default()
+ };
+
+ let server = create_server("localhost", "9999", state);
server.run();
// println!("After the server died!");