Commit a6574725 authored by Stavros Korokithakis's avatar Stavros Korokithakis

Add files to IPFS when Hearth starts up

parent 562d16d0
......@@ -45,28 +45,18 @@ lazy_static! {
static IPFS_EXECUTABLE: &str = "ipfs";
fn add_files_to_ipfs() -> String {
fn add_files_to_ipfs() {
println!("Adding files to IPFS...");
let output = Command::new(IPFS_EXECUTABLE)
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["add", "-r", HEARTH_DIR.to_str().unwrap()])
.output()
.expect("Could not add directory.");
let hash = String::from_utf8(output.stdout).unwrap();
hash
}
let ipfs_out = String::from_utf8(output.stdout).unwrap();
fn notify_ipfs(event: notify::DebouncedEvent) {
match event {
notify::DebouncedEvent::Create(_) => {}
notify::DebouncedEvent::Remove(_) => {}
notify::DebouncedEvent::Rename(_, _) => {}
notify::DebouncedEvent::Write(_) => {}
_ => return,
}
let ipfs_out = add_files_to_ipfs();
let mut hashes = IPFS_HASHES.lock().unwrap();
hashes.clear();
// Parse the IPFS output and add it to the dictionary.
hashes.extend(ipfs_out.lines().map(|l| {
let mut words = l.splitn(3, " ");
......@@ -81,6 +71,17 @@ fn notify_ipfs(event: notify::DebouncedEvent) {
println!("https://www.eternum.io/ipfs/{}", hashes.get("").unwrap());
}
fn notify_ipfs(event: notify::DebouncedEvent) {
match event {
notify::DebouncedEvent::Create(_) => {}
notify::DebouncedEvent::Remove(_) => {}
notify::DebouncedEvent::Rename(_, _) => {}
notify::DebouncedEvent::Write(_) => {}
_ => return,
}
add_files_to_ipfs();
}
fn start_ipfs() {
println!("Initializing IPFS...");
......@@ -95,6 +96,13 @@ fn start_ipfs() {
println!("Starting IPFS daemon...");
// Add everything, because files might have been changed while we weren't looking.
thread::spawn(|| {
// Wait for the daemon to start up...
thread::sleep(Duration::from_millis(5000));
add_files_to_ipfs();
});
Command::new(IPFS_EXECUTABLE)
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["daemon", "--writable", "--enable-gc"])
......
......@@ -22,7 +22,7 @@ pub fn start_api() {
println!("Starting API server on {}...", interface);
rouille::start_server(interface, move |request| {
router!(request,
(POST) (/api/exit) => {
(POST) (/api/exit/) => {
// Terminate the server after one second.
thread::spawn(|| {
thread::sleep(time::Duration::from_secs(1));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment