aboutsummaryrefslogtreecommitdiff
path: root/lockchain-http/src/lib.rs
blob: 1721eee7b08b8f1368daa0622a40a2c424a51d8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! 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 lockchain_core as lockchain;

use actix::{server, App};
use lockchain::traits::{Body, Vault};

mod handlers;
mod model;

pub use model::CarrierMessage;

/// 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()
            .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", |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("/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();
}