summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: fd14adc1366cb8e5bb211c5f37b834f9878473c7 (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
use std::path::PathBuf;
use std::str::FromStr;

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;

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()?)?
    //    .await?;

    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);
            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);
            Ok(())
        },

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