Implement read_all endpoint, pass RbxSession into web process

This commit is contained in:
Lucien Greathouse 2018-05-17 01:27:11 -07:00
parent 59e3881895
commit 9f31b1233f
4 changed files with 42 additions and 10 deletions

View file

@ -26,12 +26,6 @@ pub fn serve(project_path: &PathBuf, port: Option<u64>) {
},
};
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<u64>) {
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);

View file

@ -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,

View file

@ -64,7 +64,7 @@ pub struct RbxSession {
partition_instances: HashMap<String, Id>,
/// The store of all instances in the session.
instances: HashMap<Id, RbxInstance>,
pub instances: HashMap<Id, RbxInstance>,
/// A map from files in the VFS to instances loaded in the session.
instances_by_route: HashMap<FileRoute, Id>,

View file

@ -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<RwLock<RbxSession>>,
}
#[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<Id, RbxInstance>,
}
/// 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()
)
});