summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 8f0d9387921205514c8e77b56577cc87b4073eb2 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::path::PathBuf;
use std::str::FromStr;

use anyhow::anyhow;
use anyhow::Result;
use daglib::DagBackend;
use rand_os::OsRng;
use rand_core::CryptoRng;
use rand_core::RngCore;
use ed25519_dalek::Keypair;
use ed25519_dalek::Signature;
use futures::stream::StreamExt;

extern crate clap_v3 as clap;

mod backend;
mod cli;
mod consts;
mod profile;

#[tokio::main]
async fn main() -> Result<()> {
    let app = crate::cli::app();

    let mut backend = {
        // Testing configuration for the IPFS node in the backend.

        let tmp = PathBuf::from("/tmp/distrox.tmp");
        let sconf = ipfs_embed::StorageConfig {
            path: Some(tmp),
            cache_size_blocks: 100_000, // blocks kepts before GC
            cache_size_bytes: 1024 * 1024 * 1024, // 1GB before GC
            gc_interval: std::time::Duration::from_secs(60 * 60), // hourly
            gc_min_blocks: 0,
            gc_target_duration: std::time::Duration::from_secs(60), // 1 minute
        };

        let mut csprng = OsRng{};
        let nconf = ipfs_embed::NetworkConfig {
            node_name: String::from("distrox-devel"),
            node_key: ipfs_embed::Keypair::generate(&mut csprng),

            quic: ipfs_embed::TransportConfig::default(),
            psk: None,
            dns: None,
            mdns: None,
            kad: None,
            ping: None,
            identify: None,
            gossipsub: None,
            broadcast: None,
            bitswap: None,

        };

        let ipfs_configuration = ipfs_embed::Config {
            storage: sconf,
            network: nconf,
        };
        crate::backend::IpfsEmbedBackend::new_with_config(ipfs_configuration).await?
    };

    backend.ipfs()
        .listen_on("/ip4/127.0.0.1/tcp/0".parse()?)?
        .next()
        .await
        .unwrap();

    match app.get_matches().subcommand() {
        ("create-profile", Some(mtch)) => {
            let payload = mtch
                .value_of("content")
                .map(String::from)
                .map(crate::backend::Payload::now_from_text)
                .unwrap(); // Safe by clap

            let payload_cid = backend.write_payload(&payload).await?;
            let node = crate::backend::Node::new(crate::consts::v1(), vec![], payload_cid);

            let id = backend.put(node).await?;

            println!("id = {}", id.as_ref());
            Ok(())
        },

        ("post", Some(mtch)) => {
            let payload = mtch
                .value_of("content")
                .map(String::from)
                .map(crate::backend::Payload::now_from_text)
                .unwrap(); // Safe by clap
            let parent = mtch
                .value_of("head")
                .map(cid::Cid::from_str)
                .transpose()?
                .map(crate::backend::Id::from)
                .unwrap(); // Safe by clap

            let payload_cid = backend.write_payload(&payload).await?;
            let node = crate::backend::Node::new(crate::consts::v1(), vec![parent], payload_cid);

            let id = backend.put(node).await?;

            println!("id = {}", id.as_ref());
            Ok(())
        },

        ("get", Some(mtch)) => {
            let head = mtch
                .value_of("head")
                .map(cid::Cid::from_str)
                .transpose()?
                .map(crate::backend::Id::from)
                .unwrap(); // Safe by clap

            let (id, node) = backend
                .get(head.clone())
                .await?
                .ok_or_else(|| anyhow!("Not found: {:?}", head))?;

            let payload = backend.ipfs().fetch(node.payload_id(), backend.ipfs().peers()).await?;
            let payload = payload.decode::<libipld::cbor::DagCborCodec, crate::backend::Payload>()?;

            println!("id      = {}", id.as_ref());
            println!("node    = {:?}", node);
            println!("payload = {:?}", payload);
            Ok(())
        },

        (other, _) => {
            unimplemented!()
        },
    }
}