Add REST API for getting installation status

This commit is contained in:
James Lonie 2018-05-03 13:39:55 +10:00
parent dae36a15b9
commit 93ebca5f3c
2 changed files with 36 additions and 3 deletions

View File

@ -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<LocalInstallation>,
install_path: Option<String>,
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<String>,
@ -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 {

View File

@ -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<String>,
}
/// 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::<hyper::Body>::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();