Commit 562d16d0 authored by Stavros Korokithakis's avatar Stavros Korokithakis

Add hash-fetching API call

parent 6289c334
......@@ -12,6 +12,8 @@ use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use views::start_api;
use std::fs;
use std::io;
use std::sync::Mutex;
use std::collections::HashMap;
use std::env;
use std::time::Duration;
use std::sync::mpsc::channel;
......@@ -19,6 +21,10 @@ use std::thread;
use std::process;
use std::process::Command;
lazy_static! {
static ref IPFS_HASHES: Mutex<HashMap<String, String>> = Mutex::new(HashMap::new());
}
lazy_static! {
static ref HEARTH_DIR: std::path::PathBuf = {
let mut dir = env::home_dir().expect("Could not get home directory.");
......@@ -43,7 +49,7 @@ fn add_files_to_ipfs() -> String {
println!("Adding files to IPFS...");
let output = Command::new(IPFS_EXECUTABLE)
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["add", "-r", "-Q", HEARTH_DIR.to_str().unwrap()])
.args(&["add", "-r", HEARTH_DIR.to_str().unwrap()])
.output()
.expect("Could not add directory.");
let hash = String::from_utf8(output.stdout).unwrap();
......@@ -58,8 +64,21 @@ fn notify_ipfs(event: notify::DebouncedEvent) {
notify::DebouncedEvent::Write(_) => {}
_ => return,
}
let hash = add_files_to_ipfs();
println!("https://www.eternum.io/ipfs/{}", hash);
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, " ");
let _mode = words.next().unwrap();
let hash = words.next().unwrap();
let path = words.next().unwrap();
let clean_path = path.splitn(2, "/").nth(1).unwrap_or(""); // Remove the "Hearth" component from paths.
(clean_path.to_string(), hash.to_string())
}));
println!("https://www.eternum.io/ipfs/{}", hashes.get("").unwrap());
}
fn start_ipfs() {
......
......@@ -3,23 +3,41 @@ use rouille::Response;
use std::process;
use std::{thread, time};
use IPFS_HASHES;
#[derive(RustcEncodable)]
struct ExitResponse {
result: String,
}
#[derive(RustcEncodable)]
struct HashResponse {
result: String,
hash: String,
path: String,
}
pub fn start_api() {
let interface = "127.0.0.1:26484";
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));
process::exit(1);
});
Response::json(&ExitResponse{ result: "ok".to_owned()})
Response::json(&ExitResponse{result: "ok".to_owned()})
},
(GET) (/api/path/{path: String}) => {
let hashes = IPFS_HASHES.lock().unwrap();
if hashes.contains_key(&path) {
let hash = hashes.get(&path).unwrap();
Response::json(&HashResponse{hash: hash.to_string(), path: path.to_string(), result: "ok".to_owned()})
} else {
Response::empty_404()
}
},
_ => rouille::Response::empty_404()
)
......
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