From 93ebca5f3cbac803086a16c3fac517805cf33675 Mon Sep 17 00:00:00 2001 From: James Lonie Date: Thu, 3 May 2018 13:39:55 +1000 Subject: [PATCH] Add REST API for getting installation status --- src/installer.rs | 21 +++++++++++++++++++-- src/rest.rs | 18 +++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/installer.rs b/src/installer.rs index b006b62..65a64be 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -52,14 +52,22 @@ pub struct InstallerFramework { preexisting_install: bool } +// Contains basic properties on the status of the session. Subset of InstallationFramework. +#[derive(Serialize)] +pub struct InstallationStatus { + database: Vec, + install_path: Option, + preexisting_install: bool +} + /// Used to track the amount of data that has been downloaded during a HTTP request. struct DownloadProgress { downloaded: usize, } /// Tracks the state of a local installation -#[derive(Debug, Serialize, Deserialize)] -struct LocalInstallation { +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct LocalInstallation { name: String, version: Version, files: Vec, @@ -398,6 +406,15 @@ impl InstallerFramework { self.install_path = Some(dir.to_owned()); } + /// Returns metadata on the current status of the installation. + pub fn get_installation_status(&self) -> InstallationStatus { + InstallationStatus { + database: self.database.clone(), + install_path: self.install_path.clone(), + preexisting_install: self.preexisting_install + } + } + /// Creates a new instance of the Installer Framework with a specified Config. pub fn new(config: Config) -> Self { InstallerFramework { diff --git a/src/rest.rs b/src/rest.rs index 6e7b9b8..6453f15 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -24,6 +24,7 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::thread::{self, JoinHandle}; use std::process::exit; use std::sync::Arc; +use std::sync::RwLock; use std::sync::mpsc::channel; use std::collections::HashMap; @@ -31,13 +32,14 @@ use assets; use installer::InstallerFramework; use installer::InstallMessage; -use std::sync::RwLock; +use installer::LocalInstallation; #[derive(Serialize)] struct FileSelection { path: Option, } + /// Acts as a communication mechanism between the Hyper WebService and the rest of the /// application. pub struct WebServer { @@ -152,6 +154,20 @@ impl Service for WebService { (&Get, "/api/exit") => { exit(0); } + // Gets properties such as if the application is in maintenance mode + (&Get, "/api/installation-status") => { + let framework = self.framework.read().unwrap(); + + let response = framework.get_installation_status(); + + let file = serde_json::to_string(&response).unwrap(); + + Response::::new() + .with_header(ContentLength(file.len() as u64)) + .with_header(ContentType::json()) + .with_body(file) + } + // Streams the installation of a particular set of packages (&Post, "/api/start-install") => { // We need to bit of pipelining to get this to work let framework = self.framework.clone();