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

@ -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()
)
});