diff --git a/peach-web/Cargo.toml b/peach-web/Cargo.toml index 4183c91..74ba710 100644 --- a/peach-web/Cargo.toml +++ b/peach-web/Cargo.toml @@ -35,7 +35,9 @@ travis-ci = { repository = "peachcloud/peach-web", branch = "master" } maintenance = { status = "actively-developed" } [dependencies] +dirs = "4.0.0" env_logger = "0.8" +#golgi = "0.1.0" golgi = { path = "../../../playground/rust/golgi" } lazy_static = "1.4.0" log = "0.4" diff --git a/peach-web/src/error.rs b/peach-web/src/error.rs index 10d532d..e20608d 100644 --- a/peach-web/src/error.rs +++ b/peach-web/src/error.rs @@ -9,21 +9,25 @@ use serde_yaml::Error as YamlError; /// Custom error type encapsulating all possible errors for the web application. #[derive(Debug)] pub enum PeachWebError { - Golgi(GolgiError), - Json(JsonError), - Yaml(YamlError), FailedToRegisterDynDomain(String), + Golgi(GolgiError), + HomeDir, + Json(JsonError), + OsString, PeachLib { source: PeachError, msg: String }, + Yaml(YamlError), } impl std::error::Error for PeachWebError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match *self { - PeachWebError::Golgi(ref source) => Some(source), - PeachWebError::Json(ref source) => Some(source), - PeachWebError::Yaml(ref source) => Some(source), PeachWebError::FailedToRegisterDynDomain(_) => None, + PeachWebError::Golgi(ref source) => Some(source), + PeachWebError::HomeDir => None, + PeachWebError::Json(ref source) => Some(source), + PeachWebError::OsString => None, PeachWebError::PeachLib { ref source, .. } => Some(source), + PeachWebError::Yaml(ref source) => Some(source), } } } @@ -31,13 +35,21 @@ impl std::error::Error for PeachWebError { impl std::fmt::Display for PeachWebError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match *self { - PeachWebError::Golgi(ref source) => write!(f, "Golgi error: {}", source), - PeachWebError::Json(ref source) => write!(f, "Serde JSON error: {}", source), - PeachWebError::Yaml(ref source) => write!(f, "Serde YAML error: {}", source), PeachWebError::FailedToRegisterDynDomain(ref msg) => { write!(f, "DYN DNS error: {}", msg) } + PeachWebError::Golgi(ref source) => write!(f, "Golgi error: {}", source), + PeachWebError::HomeDir => write!( + f, + "Filesystem error: failed to determine home directory path" + ), + PeachWebError::Json(ref source) => write!(f, "Serde JSON error: {}", source), + PeachWebError::OsString => write!( + f, + "Filesystem error: failed to convert OsString to String for go-ssb directory path" + ), PeachWebError::PeachLib { ref source, .. } => write!(f, "{}", source), + PeachWebError::Yaml(ref source) => write!(f, "Serde YAML error: {}", source), } } } @@ -54,12 +66,6 @@ impl From for PeachWebError { } } -impl From for PeachWebError { - fn from(err: YamlError) -> PeachWebError { - PeachWebError::Yaml(err) - } -} - impl From for PeachWebError { fn from(err: PeachError) -> PeachWebError { PeachWebError::PeachLib { @@ -68,3 +74,9 @@ impl From for PeachWebError { } } } + +impl From for PeachWebError { + fn from(err: YamlError) -> PeachWebError { + PeachWebError::Yaml(err) + } +} diff --git a/peach-web/src/router.rs b/peach-web/src/router.rs index 1c2c84d..fd990d4 100644 --- a/peach-web/src/router.rs +++ b/peach-web/src/router.rs @@ -1,13 +1,16 @@ use rocket::{catchers, fs::FileServer, routes, Build, Rocket}; use rocket_dyn_templates::Template; -use crate::routes::{ - authentication::*, - catchers::*, - index::*, - scuttlebutt::*, - settings::{admin::*, dns::*, menu::*, network::*, scuttlebutt::*, theme::*}, - status::{device::*, network::*, scuttlebutt::*}, +use crate::{ + routes::{ + authentication::*, + catchers::*, + index::*, + scuttlebutt::*, + settings::{admin::*, dns::*, menu::*, network::*, scuttlebutt::*, theme::*}, + status::{device::*, network::*, scuttlebutt::*}, + }, + utils, }; /// Create a Rocket instance and mount PeachPub routes, fileserver and @@ -15,6 +18,10 @@ use crate::routes::{ /// settings and status routes related to networking and the device (memory, /// hard disk, CPU etc.). pub fn mount_peachpub_routes(rocket: Rocket) -> Rocket { + // set the `.ssb-go` path in order to mount the blob fileserver + let ssb_path = utils::get_go_ssb_path().expect("define ssb-go dir path"); + let blobstore = format!("{}/blobs/sha256", ssb_path); + rocket .mount( "/", @@ -81,6 +88,7 @@ pub fn mount_peachpub_routes(rocket: Rocket) -> Rocket { ) .mount("/status", routes![scuttlebutt_status]) .mount("/", FileServer::from("static")) + .mount("/blob", FileServer::from(blobstore).rank(-1)) .register("/", catchers![not_found, internal_error, forbidden]) .attach(Template::fairing()) } diff --git a/peach-web/src/utils.rs b/peach-web/src/utils.rs index fee26fa..dea8150 100644 --- a/peach-web/src/utils.rs +++ b/peach-web/src/utils.rs @@ -1,11 +1,36 @@ pub mod monitor; +use dirs; use log::info; +use peach_lib::sbot::SbotConfig; use rocket::response::{Redirect, Responder}; use rocket::serde::Serialize; use rocket_dyn_templates::Template; -use crate::THEME; +use crate::{error::PeachWebError, THEME}; + +// FILEPATH FUNCTIONS + +// return the path of the ssb-go directory +pub fn get_go_ssb_path() -> Result { + let go_ssb_path = match SbotConfig::read() { + Ok(conf) => conf.repo, + // return the default path if unable to read `config.toml` + Err(_) => { + // determine the home directory + let mut home_path = dirs::home_dir().ok_or_else(|| PeachWebError::HomeDir)?; + // add the go-ssb subdirectory + home_path.push(".ssb-go"); + // convert the PathBuf to a String + home_path + .into_os_string() + .into_string() + .map_err(|_| PeachWebError::OsString)? + } + }; + + Ok(go_ssb_path) +} // THEME FUNCTIONS