diff --git a/server/src/commands/serve.rs b/server/src/commands/serve.rs index 622ff3f2..788de68c 100644 --- a/server/src/commands/serve.rs +++ b/server/src/commands/serve.rs @@ -26,12 +26,6 @@ pub fn serve(project_path: &PathBuf, port: Option) { }, }; - let web_config = web::WebConfig { - port: port.unwrap_or(project.serve_port), - server_id, - start_time: Instant::now(), - }; - let mut partitions = HashMap::new(); for (partition_name, partition) in project.partitions.iter() { @@ -54,6 +48,13 @@ pub fn serve(project_path: &PathBuf, port: Option) { let mut session = Session::new(config.clone()); session.start(); + let web_config = web::WebConfig { + port: port.unwrap_or(project.serve_port), + server_id, + start_time: Instant::now(), + rbx_session: session.get_rbx_session(), + }; + println!("Server listening on port {}", web_config.port); web::start(web_config); diff --git a/server/src/rbx.rs b/server/src/rbx.rs index c24ff0a0..b85a7319 100644 --- a/server/src/rbx.rs +++ b/server/src/rbx.rs @@ -5,7 +5,8 @@ use id::Id; // TODO: Switch to enum to represent more value types pub type RbxValue = String; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] pub struct RbxInstance { /// Maps to the `Name` property on Instance. pub name: String, diff --git a/server/src/rbx_session.rs b/server/src/rbx_session.rs index dd9dd467..fadc3ab2 100644 --- a/server/src/rbx_session.rs +++ b/server/src/rbx_session.rs @@ -64,7 +64,7 @@ pub struct RbxSession { partition_instances: HashMap, /// The store of all instances in the session. - instances: HashMap, + pub instances: HashMap, /// A map from files in the VFS to instances loaded in the session. instances_by_route: HashMap, diff --git a/server/src/web.rs b/server/src/web.rs index 11b51599..38cb4237 100644 --- a/server/src/web.rs +++ b/server/src/web.rs @@ -1,25 +1,39 @@ +use std::collections::HashMap; use std::time::Instant; +use std::sync::{RwLock, Arc}; use rouille; use web_util::json_response; +use id::Id; +use rbx::RbxInstance; +use rbx_session::RbxSession; /// The set of configuration the web server needs to start. pub struct WebConfig { pub port: u64, pub server_id: u64, pub start_time: Instant, + pub rbx_session: Arc>, } #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] -struct ServerInfo<'a> { +struct ServerInfoResponse<'a> { server_version: &'static str, protocol_version: u64, server_id: &'a str, current_time: f64, } +#[derive(Debug, Serialize)] +#[serde(rename_all = "camelCase")] +struct ReadAllResponse<'a> { + server_id: &'a str, + current_time: f64, + instances: &'a HashMap, +} + /// Start the Rojo web server and park our current thread. pub fn start(config: WebConfig) { let address = format!("localhost:{}", config.port); @@ -38,7 +52,7 @@ pub fn start(config: WebConfig) { elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1_000_000_000.0 }; - json_response(ServerInfo { + json_response(ServerInfoResponse { server_version, protocol_version: 2, server_id: &server_id, @@ -46,6 +60,22 @@ pub fn start(config: WebConfig) { }) }, + (GET) (/read_all) => { + let rbx_session = config.rbx_session.read().unwrap(); + + let current_time = { + let elapsed = config.start_time.elapsed(); + + elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1_000_000_000.0 + }; + + json_response(ReadAllResponse { + server_id: &server_id, + current_time, + instances: &rbx_session.instances, + }) + }, + _ => rouille::Response::empty_404() ) });