Commit 6289c334 authored by Stavros Korokithakis's avatar Stavros Korokithakis

Add API server and "exit" command

parent 5fcba46b
......@@ -7,4 +7,5 @@ authors = ["Stavros Korokithakis <hi@stavros.io>"]
notify = "4.0.0"
xdg = "^2.1"
lazy_static = "0.2"
rocket = "^0.3"
rouille = "^1.0"
rustc-serialize = "0.3.24"
#[macro_use]
extern crate lazy_static;
extern crate notify;
#[macro_use]
extern crate rouille;
extern crate rustc_serialize;
extern crate xdg;
mod views;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use views::start_api;
use std::fs;
use std::io;
use std::env;
......@@ -56,36 +62,35 @@ fn notify_ipfs(event: notify::DebouncedEvent) {
println!("https://www.eternum.io/ipfs/{}", hash);
}
fn spawn_ipfs_thread() {
thread::spawn(move || {
println!("Initializing IPFS...");
Command::new(IPFS_EXECUTABLE)
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["init"])
.output()
.unwrap_or_else(|_| {
println!("Could not initialize IPFS.");
process::exit(1)
});
println!("Starting IPFS daemon...");
Command::new(IPFS_EXECUTABLE)
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["daemon", "--writable", "--enable-gc"])
.output()
.unwrap_or_else(|_| {
println!("Could not launch IPFS daemon.");
process::exit(1)
});
});
fn start_ipfs() {
println!("Initializing IPFS...");
Command::new(IPFS_EXECUTABLE)
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["init"])
.output()
.unwrap_or_else(|_| {
println!("Could not initialize IPFS.");
process::exit(1)
});
println!("Starting IPFS daemon...");
Command::new(IPFS_EXECUTABLE)
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["daemon", "--writable", "--enable-gc"])
.output()
.unwrap_or_else(|_| {
println!("Could not launch IPFS daemon.");
process::exit(1)
});
}
fn main() {
println!("Starting hearth...");
spawn_ipfs_thread();
thread::spawn(start_ipfs);
thread::spawn(start_api);
match fs::create_dir(&*HEARTH_DIR) {
Err(ref e) if e.kind() != io::ErrorKind::AlreadyExists => {
......
use rouille;
use rouille::Response;
use std::process;
use std::{thread, time};
#[derive(RustcEncodable)]
struct ExitResponse {
result: 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/) => {
// 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()})
},
_ => 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