diff options
author | Katharina Fey <kookie@spacekookie.de> | 2018-06-12 13:52:23 +0200 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2018-06-12 13:52:23 +0200 |
commit | f33840c1fff697a8467a78941687a71036b3c395 (patch) | |
tree | 1bf5740f8c95b12812b3622841b4c67d278adf73 /lockchain-http/src/state.rs | |
parent | e47c2198c8f92003eb95f15a4311420e3b06c878 (diff) |
Working on the http layer, adding 'create' route
Many things here are not ideal. There is very little checking that's
being done, for this we need to tweak the core API a bit further. There
are some corner cases that aren't caught. Authentication isn't being
done yet. And it's not 100% clear yet where those scopes should live.
But...there is progress being made. The API will develop and hopefully
soon-ish we'll also be able to add versioning via a new serde-versioning
crate being written (hint hint).
The basic problem is that we need to build a system that works for two kinds
of settings: remotely, administered via a few users, on a different computer
with limited access and local, used by a single user and application stack
(http interface for browser plugins, file API for everything else).
The latter is the focus of development for now, but before we hit 1.0 (aka
stable for production) there are more issues to be resolved.
Also...if lockchain is to be used as a keystore in poke, we need to figure
out how to allow easier integration of the core storage components into other
applications (maybe some guides).
Also...all of this should be documented in other places than just my ramblings
in commit messages 😅
Diffstat (limited to '')
-rw-r--r-- | lockchain-http/src/state.rs | 70 |
1 files changed, 66 insertions, 4 deletions
diff --git a/lockchain-http/src/state.rs b/lockchain-http/src/state.rs index 45d92b1..80cd2b7 100644 --- a/lockchain-http/src/state.rs +++ b/lockchain-http/src/state.rs @@ -1,7 +1,29 @@ use lockchain::traits::{AutoEncoder, Body, Vault}; use std::collections::HashMap; use std::marker::PhantomData; +use std::path::PathBuf; +/// An in-memory API state object which is delegated to all handlers +/// +/// This mechanism serves two purposes +/// +/// 1. Configuration of the API, beyond simple paramters provided to +/// the server_start call +/// 2. Buffering and pre-loading of certain vault components that need +/// to be accessed via the handlers +/// +/// It provides some simple query functions for handlers to work on, +/// as well as expose raw configuration fields to be written +/// +/// ``` +/// let state: ApiState<B, V> = ApiState { +/// bound_scope: false, +/// working_dir: ".".into(), +/// .. +/// }; +/// ``` +/// +/// (Replace `B` and `V` with your generics 🙂) pub struct ApiState<B, V> where B: Body, @@ -9,6 +31,45 @@ where { vaults: HashMap<String, Option<V>>, _phantom: PhantomData<B>, + + /// Signal if the API handlers are allowed outside their working dir + pub bound_scope: bool, + /// Provide a working directory + pub working_dir: PathBuf, +} + +impl<B, V> ApiState<B, V> +where + B: Body, + V: Vault<B>, +{ + /// Return a list of string slices for each vault in scope + pub fn vaults(&self) -> Vec<&str> { + self.vaults.iter().map(|(k, _)| k.as_str()).collect() + } + /// Simply return the number of known vaults + pub fn count(&self) -> usize { + self.vaults.len() + } + + pub fn add_vault(&mut self, name: &str, vault: V) { + self.vaults.insert(name.into(), Some(vault)); + } +} + +impl<B, V> Default for ApiState<B, V> +where + B: Body, + V: Vault<B>, +{ + fn default() -> Self { + Self { + vaults: Default::default(), + _phantom: PhantomData, + bound_scope: true, + working_dir: Default::default(), + } + } } #[derive(Serialize, Deserialize)] @@ -24,8 +85,8 @@ where B: Body, V: Vault<B>, { - fn from(me: ApiState<B, V>) -> SerializedState { - SerializedState { + fn from(me: ApiState<B, V>) -> Self { + Self { vaults: me .vaults .into_iter() @@ -43,8 +104,8 @@ where B: Body, V: Vault<B>, { - fn from(me: SerializedState) -> ApiState<B, V> { - ApiState { + fn from(me: SerializedState) -> Self { + Self { vaults: me.vaults.into_iter().fold( HashMap::new(), |mut acc: HashMap<String, Option<V>>, k| { @@ -53,6 +114,7 @@ where }, ), _phantom: PhantomData, + ..Default::default() } } } |